thinkphp5+swoole使用异步mysql和http请求

作者:谢高升 发布:2018-11-09 浏览:5733次

根据thinkphp官网的自定义指令来完成异步的mysql和http的请求;

日常开发中一些大量的操作日志,可以通过异步扔进数据库,不用去等待

thinkphp5.1+swoolw4.1

自定义指令的文档https://www.kancloud.cn/manual/thinkphp5_1/354146

1,在application/console/创建一个Http.php

<?php
/**
 * Created by PhpStorm.
 * User: xiaoxie
 * Date: 2018/11/9
 * Time: 17:05
 **/
namespace app\console;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Http extends Command
{
    protected $server;
    protected $dbConfig = [
            'host' => '127.0.0.1',
            'port' => '3306',
            'user' => 'root',
            'password' => 'root',
            'database' => 'study',
            'charset' => 'utf8',
        ];
    // 命令行配置函数
    protected function configure()
    {
        // setName 设置命令行名称 && setDescription 设置命令行描述
        $this->setName('http:server')->setDescription('Start http Server!');
    }
    // 设置命令返回信息
    protected function execute(Input $input, Output $output)
    {
        $this->server = new \swoole_http_server("0.0.0.0", 9501);
        $this->server->on('WorkerStart', [$this, 'onWorkerStart']);
        $this->server->on('Request', [$this, 'onRequest']);
        $this->server->on('Task', [$this, 'onTask']);
        $this->server->on('Finish', [$this, 'onFinish']);
        $this->server->start();
    }
    // Worker/Task进程启动时回调函数
    public function onWorkerStart(\swoole_server $serv, $worker_id)
    {
        echo "start success\n";

    }
    // 请求处理 处理各种业务逻辑写redis,,,,
    public function onRequest(\swoole_http_request $request, \swoole_http_response $response)
    {
        try {
            $data = $request->get;
            if (!$data) {
                return '缺少参数';
            }
            $db = new \swoole_mysql();
            $db->connect($this->dbConfig, function ($db, $r) {
                if ($r === false) {
                    var_dump($db->connect_errno, $db->connect_error);
                    return;
                }
                $sql = 'show tables';
                $db->query($sql, function(\swoole_mysql $db, $r) {
                    if ($r === false)
                    {
                        var_dump($db->error, $db->errno);
                    }
                    elseif ($r === true )
                    {
                        var_dump($db->affected_rows, $db->insert_id);
                    }
                    var_dump($r);
                    $db->close();
                });
            });
            var_dump($data);
            //也可以投递任务
            //$server->task();
        } catch (\Exception $exception) {
            $response->end($exception->getMessage());
        }
        $response->end('success');
    }
    // 异步任务处理函数
    public function onTask(\swoole_server $serv, $task_id, $src_worker_id, $data)
    {
        return 'task success';
    }
    // 异步任务完成通知
    public function onFinish(\swoole_server $server, $task_id, $data)
    {
    }

}

第二步,配置application/command.php文件

return [
    'app\console\Http',
];

image.png

浏览器打开127.0.0.1:9501?param=111;终端中就会出现上面的输出;