swoole原生mysql进程池_详解基于swoole的mysql连接池实现

本文介绍了如何利用swoole框架构建一个MySQL连接池,通过减少频繁的连接和断开操作来提高高并发应用的性能。文章详细讲解了连接池的工作原理和优势,并提供了实现连接池和数据库操作类的PHP代码示例。
摘要由CSDN通过智能技术生成

swoole框架栏目介绍swoole实现mysql连接池的方法

dc46dfb7719d011471d49fd9ee997872.png

前言

传统的nginx+FPM模式的PHP程序而言,每次请求FPM的worker都会连接一次mysql,然后请求结束便会断开连接。对于并发小的应用来说这不会有什么问题,但是对于高并发的应用来说,频繁建立连接Connect和销毁连接Close,数据库便会成为瓶颈,相信不少人也遇到过to many connection的mysql报错吧。

连接池的优势

连接池采用的是长连接模式,会一直保持与MySQL的连接,用完后会重新放回连接池,从而节省了建立连接和断开连接的消耗,大大降低了系统IO的消耗,一定程度上提高了程序的并发性能。如果连接池空闲,就从连接池分配一个连接,否则,请求将被加入到等待队列中。

实现

我们采用swoole实现mysql连接池

连接池类<?php

require_once "MysqlDB.php";class MysqlPool{

private static $instance;

private $pool;

private $config;

private $pool_get_timeout;

/**

* 获取mysql进程池单例

* @param null $config

* @return MysqlPool

*/

public static function getInstance($config = null)

{

if (empty(self::$instance)) {

if (empty($config)) {

throw new RuntimeException("mysql config is empty");

}

self::$instance = new static($config);

}

return self::$instance;

}

public function __construct($config)

{

if (empty($this->pool)) {

$this->config = $config;

$this->pool = new \Swoole\Coroutine\Channel($config['pool_size']);

for ($i = 0; $i < $config['pool_size']; $i++) {

\go(function() use ($config) {

$mysql = new MysqlDB();

$res = $mysql->connect($config['mysql']);

if ($res === false) {

throw new RuntimeException("Failed to connect mysql server");

} else {

$this->pool->push($mysql);

}

});

}

}

}

public function get()

{

if ($this->pool->length() > 0) {

$mysql = $this->pool->pop($this->config['pool_get_timeout']);

if (false === $mysql) {

throw new RuntimeException("Pop mysql timeout");

}

return $mysql;

} else {

throw new RuntimeException("Pool length <= 0");

}

}

public function recycle(MysqlDB $mysql){

$this->pool->push($mysql);

}

/**

* 获取连接池长度

* @return mixed

*/

public function getPoolSize(){

return $this->pool->length();

}}

数据库DB类<?phpclass MysqlDB{

private $connection;

public function connect($config)

{

$connection = new \Swoole\Coroutine\MySQL();

$res = $connection->connect($config);

if ($res === false) {

throw new RuntimeException($connection->connect_error, $connection->errno);

} else {

$this->connection = $connection;

}

return $res;

}

public function query($sql){

$result = $this->connection->query($sql);

return $result;

}}

在HTTP协程服务器中创建连接池<?php

require_once "MysqlPool.php";\Co\run(function () {

$server = new \Co\Http\Server("0.0.0.0", 9501, false);

$pool = MysqlPool::getInstance([

'pool_size'=>5,

'pool_get_timeout'=>1,

'timeout'=>1,

'charset'=>'utf8',

'strict_type'=>false,

'fetch_mode'=>true,

'mysql'=>[

'host'=>'127.0.0.1',

'port'=>'3306',

'user'=>'homestead',

'password'=>'secret',

'database'=>'blog',

]

]);

$server->handle('/', function ($request, $response) use ($pool){

$mysql = $pool->get();

$res = $mysql->query("select id,phone,username from user limit 1");

var_dump($res);

$pool->recycle($mysql);

$response->end("

Test

");

});

$server->handle('/test', function ($request, $response) {

$response->end("

Test

");

});

$server->handle('/stop', function ($request, $response) use ($server) {

$response->end("

Stop

");

$server->shutdown();

});

$server->start();});

源码地址 swoole-mysql-pool相关免费学习推荐:mysql视频教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值