美文网首页
swoole协程中使用数据库连接池库open-smf/conne

swoole协程中使用数据库连接池库open-smf/conne

作者: 骑蚂蚁上高速_jun | 来源:发表于2020-02-01 14:00 被阅读0次

1.通过composer 安装库

$ composer require "open-smf/connection-pool:~1.0"
# 该包库只能在swoole 的协程环境下使。

2.引用connection-pool并配置ConnectionPool.php步骤:

use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\CoroutineMySQLConnector;
use Smf\ConnectionPool\Connectors\PhpRedisConnector;
use Swoole\Coroutine\MySQL;

/**
 * 该连接池类 只能在 swoole的协程中使用
 */
trait ConnectionPool
{
    use ConnectionPoolTrait;

    /**
     * 启动mysql连接池对象
     */
    public function startMysqlPool(int $minActive=10,int $maxActive=30){
        $pool = new ConnectionPool([
                'minActive'         => $minActive,
                'maxActive'         => $maxActive,
                'maxWaitTime'       => 5,
                'maxIdleTime'       => 20,
                'idleCheckInterval' => 10,
            ],
            new CoroutineMySQLConnector,[
                'host'        => '127.0.0.1',
                'port'        => '3306',
                'user'        => 'root',
                'password'    => 'xy123456',
                'database'    => 'mysql',
                'timeout'     => 10,
                'charset'     => 'utf8mb4',
                'strict_type' => true,
                'fetch_mode'  => true,
            ]
        );
        $pool->init();
        $this->addConnectionPool("mysql", $pool);
    }

    /**
     * 启动redis连接池
     */
    public function startRedisPool(int $minActive=5,int $maxActive=20){
        $pool = new ConnectionPool(
            [
                'minActive' => $minActive,
                'maxActive' => $maxActive,
            ],
            new PhpRedisConnector,
            [
                'host'     => '127.0.0.1',
                'port'     => '6379',
                'database' => 0,
                'password' => null,
            ]);
        $pool->init();
        $this->addConnectionPool("redis",$pool);
    }

       /**
     * 获取mysql 连接实例
     */
    public function getMysqlPool():ConnectionPool{
        return $this->getConnectionPool("mysql");
    }

    /**
     * 获取redis 连接实例
     */
    public function getMysqlPool():ConnectionPool{
        return $this->getConnectionPool("redis");
    }

    /**
     * 关闭连接池
     */
    public function closePool(){
        $this->closeConnectionPools();
    }
}

2.在swoole中的使用思路讲解

use Swoole\Websocket\Server;
use \ConnectionPool;
use Swoole\Coroutine\MySQL;
use Swoole\Http\Request;
use Swoole\Http\Response;

class WsServer
{
  1 . 设置Server开启协程
  2.  在workerStart 事件中,启动连接池
  $this->startMysqlPool();
  $this-> startRedisPool();
 3. workerStop 和 workerError 事件中 关闭连接池
  $this->closePool();
4 . 在其他业务事件中
$pool = $this-> getMysqlPool(); // 获取mysql连接池操作对象
$mysql = $pool->borrow();// 获取mysql操作对象
$status = $mysql->query("sql语句");
$pool->return($mysql); // 操作完成后归还mysql连接

$pool = $this->getRedisPool(); // 获取redis连接对象
$redis = $pool->borrow(); // 得到redis操作对象
$redis->set("name","test");
$pool->return($redis); // 操作完成后归还redis连接
}

相关文章

网友评论

      本文标题:swoole协程中使用数据库连接池库open-smf/conne

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