记录·ThinkPHP5中使用swoole

(备注:这只是 swoole 和 TP5 结合的始端,想必二者的深入融合会有更多的坑需要踩!)
 
首先去 TP 官网下载框架
 

总体概览图:
 
记录·ThinkPHP5中使用swoole

在项目根目录下新建 server 文件夹,
http_server.php 内容如下(可以直接拷贝过去使用):

<?php
/**
 * Created by PhpStorm.
 * User: baidu
 * Date: 18/2/28
 * Time: 上午1:39
 */
$http = new swoole_http_server("0.0.0.0", 8811);

$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => "/home/work/swoole/thinkphpcore/public",
        'worker_num'    => 5,
        'content-type' => 'text/html; charset=utf-8',
    ]
);
$http->on('WorkerStart', function (swoole_server $server, $worker_id) {
    // 定义应用目录
    define('APP_PATH', __DIR__ . '/../application/');
    // 1. 加载基础文件
    require __DIR__ . '/../thinkphpcore/base.php';
    // 这里不能使用这种方式加载框架内容,不信你可以打开试试
    // require __DIR__ . '/../thinkphpcore/start.php';
});
$http->on('request', function($request, $response) use ($http) {
    //print_r($request->get);
    $content = [
        'date:' => date("Ymd H:i:s"),
        'get:' => $request->get,
        'post:' => $request->post,
        'header:' => $request->header,
    ];

    swoole_async_writefile(__DIR__."/access.log", json_encode($content).PHP_EOL, function($filename) {
        // todo
    }, FILE_APPEND);

    $_SERVER = [];
    if (isset($request->server)) {
        foreach ($request->server as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }

    $_HEADER = [];
    if (isset($request->header)) {
        foreach ($request->header as $k => $v) {
            $_HEADER[strtoupper($k)] = $v;
        }
    }

    $_GET = [];
    if (isset($request->get)) {
        foreach ($request->get as $k => $v) {
            $_GET[$k] = $v;
        }
    }

    $_POST = [];
    if (isset($request->post)) {
        foreach ($request->post as $k => $v) {
            $_POST[$k] = $v;
        }
    }

    // 2. 执行应用
    ob_start();
    try {
        think\App::run()->send();
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    $res = ob_get_contents();
    ob_end_clean();
    $response->end($res);
    // 这是种简单粗暴的销毁进程、重新加载框架内容的方式
    // $http->close($request);
});

$http->start();

一:开启 URL 普通模式
找到 thinkphp5-thinkphpcore-library-think-Request.php 的 pathinfo() 和 path() 方法
把这两处的 if (is_null) 判断语句注释掉.
因为如果不注释变量 pathinfo 只会存储框架第一次运行保存下来的值.

/**
     * 获取当前请求URL的pathinfo信息(含URL后缀)
     * @access public
     * @return string
     */
    public function pathinfo()
    {
//        if (is_null($this->pathinfo)) {
            ...
                        ...
                        ...
//        }
        return $this->pathinfo;
    }

    /**
     * 获取当前请求URL的pathinfo信息(不含URL后缀)
     * @access public
     * @return string
     */
    public function path()
    {
//        if (is_null($this->path)) {
           ...
                     ...
                     ...
//        }
        return $this->path;
    }

效果访问如下:
记录·ThinkPHP5中使用swoole

二:开启 pathinfo 模式:

/**
     * 获取当前请求URL的pathinfo信息(含URL后缀)
     * @access public
     * @return string
     */
    public function pathinfo()
    {
                    // 配置 pathinfo 
                    if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/') {
                            return ltrim($_SERVER['PATH_INFO'], '/');
                    }
                    // 配置 普通访问模式
//        if (is_null($this->pathinfo)) {
            ...
                        ...
                        ...
//        }
        return $this->pathinfo;
    }

    /**
     * 获取当前请求URL的pathinfo信息(不含URL后缀)
     * @access public
     * @return string
     */
    public function path()
    {
//        if (is_null($this->path)) {
           ...
                     ...
                     ...
//        }
        return $this->path;
    }

访问效果如下:
记录·ThinkPHP5中使用swoole

记录·ThinkPHP5中使用swoole

Index.php 示例代码:

<?php

namespace app\index\controller;

use think\request;

class Index
{
    public function index(Request $request)
    {
        return 'hello-swoole';
    }

    public function check()
    {
        print_r($_GET);
        return time();
    }

    public function getClientIp()
    {
        $list = swoole_get_local_ip();
        print_r($list);
    }
}

 
Swoole 官方是这样介绍的:HttpServer

  • swoole_http_server对Http协议的支持并不完整,建议仅作为应用服务器。并且在前端增加Nginx作为代理
  • swoole-1.7.7增加了内置Http服务器的支持,通过几行代码即可写出一个异步非阻塞多进程的Http服务器。

    $http = new swoole_http_server("127.0.0.1", 9501);
    $http->on('request', function ($request, $response) {
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
    });
    $http->start();

通过使用apache bench工具进行压力测试,在Inter Core-I5 4核 + 8G内存的普通PC机器上,swoole_http_server可以达到近11万QPS。远远超过php-fpm,golang自带http服务器,node.js自带http服务器。性能几乎接近与Nginx的静态文件处理。

ab -c 200 -n 200000 -k http://127.0.0.1:9501

转载于:https://blog.51cto.com/laok8/2314816

ThinkPHP8使用Swoole时定义间件的步骤如下: 1. 首先,确保你的项目已经安装并正确配置了Swoole扩展。 2. 创建间件类,你可以使用命令行工具生成间件,例如: ```bash php think make:middleware YourMiddleware ``` 这个命令会在`app/middleware`目录下创建一个名为`YourMiddleware`的间件类文件。 3. 在间件类文件,你需要实现`handle`方法,该方法会在请求到达控制器之前被调用。例如: ```php namespace app\middleware; use Closure; class YourMiddleware { public function handle($request, Closure $next) { // 在这里可以添加你的逻辑代码 // ... // 必须调用$next($request)来传递给下一个间件或控制器 return $next($request); } } ``` 4. 注册间件。在Swoole服务器启动文件(通常是`swoole.php`或者类似的文件),你需要在创建Swoole Server时指定间件。这通常在`onRequest`事件完成,例如: ```php $app->swoole->on('request', function ($request, $response) use ($app) { // 创建请求和响应对象 $psrRequest = \think\swoole\Psr7\SwooleRequest::load($request); $psrResponse = \think\swoole\Psr7\SwooleResponse::load($response); // 创建间件调度器 $middleware = new \think\swoole\middleware调度器($app); // 应用间件 $middleware->add(YourMiddleware::class)->start($psrRequest, $psrResponse); }); ``` 在这个例子,`YourMiddleware`是你创建的间件类。 5. 最后,确保在Swoole启动配置正确地应用了间件,这样每当有新的请求到来时,Swoole服务器都会按照配置执行间件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值