美文网首页
Swoole官方包 swoole/library 实现 服务器数

Swoole官方包 swoole/library 实现 服务器数

作者: 骑蚂蚁上高速_jun | 来源:发表于2020-03-07 21:21 被阅读0次

    1 . 两种安装方式

    1. swoole 4.4.10+版本内置了 library连接池 (推荐使用内置)
    $ php --ri swoole  查看 swoole.enable_library => On 是否开启即可
    2. 低于swoole4.4 使用composer 下载
    $ composer require swoole/library  
    

    2 . 使用案例

    use Swoole\Database\PDOConfig;
    use Swoole\Http\Server;
    use Swoole\Http\Request;
    use Swoole\Http\Response;
    use Swoole\Database\PDOPool;
    define("SWOOLE_USE_SHORTNAME",true);
    require "./vendor/autoload.php";
    
    class swoole
    {
        private $pdoPool;
    
        private $daemon;
        private $logPath="/data/wwwroot/php-cli/swoole.log";
    
        public function __construct()
        {
            $getopt = getopt("d:");
            $this->daemon = $getopt["d"] == "true" ? true : false;
        }
    
        public function main(){
    
            $http = new Server("0.0.0.0",20001);
    
            $http->set([
                "enable_coroutine"=>true,
                "daemonize"=>$this->daemon,
                "worker_num"=>4,
            ]);
            $http->on("workerStart",[$this,"workerStart"]);
            $http->on("workerError",[$this,"workerError"]);
            $http->on("request",[$this,"request"]);
            $http->start();
        }
    
        public function workerStart(Server $server,int $workerId){
            try{
                // 在所有的 worker & task 进程中启用mysql 的Pdo连接池
                $this->pdoPool = new PDOPool((new PDOConfig)
                    ->withHost('127.0.0.1')
                    ->withPort(3306)
                    // ->withUnixSocket('/tmp/mysql.sock')
                    ->withDbName('test')
                    ->withCharset('utf8mb4')
                    ->withUsername('root')
                    ->withPassword('123456')
                    ->withOptions([
                        PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
                    ])
                );
                // 检测数据库是否连接成功
                $pdo = $this->pdoPool->get();
                echo "启用100个数据库连接 \n";
                //$this->pdoPool->put($pdo);
            }catch(PDOException $e){
                $log= "数据库连接异常 {$e->getMessage()}\n";
                echo $log;
                file_put_contents($this->logPath,$log,FILE_APPEND);
            }
        }
    
        public function request(Request $request , Response $response){
            // 屏蔽 Chrome 浏览器的 favicon 访问
            if($request->server['request_uri'] == "/favicon.ico" || "/favicon.ico"==$request->server['path_info']){
                $response->end();
                return true;
            }
            $time = date("Y-m-d H:i:s",time());
            try{
                // 根据路由 解析用户访问的 controller & action
                list($controller,$action)=explode("/",trim($request->server['path_info'],"/"));
                // 得到用户访问的控制器名和方法名
                var_dump($controller,$action);
                $pdo = $this->pdoPool->get(); // 从连接池获取一个链接
    
                // 处理用户请求的业务逻辑
                (new $controller($pdo,$request,$response))->$action();
    
                $this->pdoPool->put($pdo); // 归还链接
            }catch(Exception $e){
                $this->pdoPool->put($pdo); // 归还链接
                $log= "{$time} 产生严重错误 {$e->getMessage()} \n";
                file_put_contents($this->logPath,$log,FILE_APPEND);
            }
    
            $response->header("Server","Wj");
            $response->end("Hello world!!");
            return true;
        }
    
        public function workerError(Server $server){
            $time = date("Y-m-d H:i:s",time());
            file_put_contents($this->logPath,"{$time}发生严重错误...\n",FILE_APPEND);
        }
    
    
    }
    
    
    (new swoole())->main();
    

    相关文章

      网友评论

          本文标题:Swoole官方包 swoole/library 实现 服务器数

          本文链接:https://www.haomeiwen.com/subject/smckdhtx.html