基于swoole扩展实现真正的PHP数据库连接池
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP的数据库连接池<span style="color: black;">始终</span><span style="color: black;">败兴</span>都是一个<span style="color: black;">困难</span>,<span style="color: black;">非常多</span>从PHP语言转向Java的项目,大<span style="color: black;">都数</span><span style="color: black;">原由</span>都是<span style="color: black;">由于</span>Java有更好的连接池实现。PHP的MySQL扩展<span style="color: black;">供给</span>了长连接的API,但在PHP<span style="color: black;">设备</span>数量较多,规模<span style="color: black;">很强</span>的<span style="color: black;">状况</span>下,mysql_pconnect非但<span style="color: black;">不可</span>节约MySQL资源,反而会加剧数据库的负荷。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假设有100台PHP的应用服务器,<span style="color: black;">每一个</span><span style="color: black;">设备</span><span style="color: black;">必须</span><span style="color: black;">起步</span>100个apache或fpm工作进程,那<span style="color: black;">每一个</span>进程都会产生一个长连接到MySQL。这一共会产生1万个My</p>SQL连接。<span style="color: black;">大众</span>都<span style="color: black;">晓得</span>MySQL是<span style="color: black;">每一个</span>连接会占用1个线程。那MYSQL就<span style="color: black;">必须</span>创建1万个线程,<span style="color: black;">这般</span><span style="color: black;">海量</span>的系统资源被浪费在线程间上下文切换上。而你的业务代码中并不是所有<span style="color: black;">地区</span>都在做数据库操作,<span style="color: black;">因此</span>这个<span style="color: black;">便是</span>浪费的。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">连接池就<span style="color: black;">区别</span>了,100个worker进程,公用10个数据库连接<span style="color: black;">就可</span>,当操作完数据库后,立即释放资源给其他worker进程。<span style="color: black;">这般</span>就算有100台PHP的服务器,那<span style="color: black;">亦</span>只会创建1000个MySQL的连接,完全<span style="color: black;">能够</span>接受的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以前确实<span style="color: black;">无</span>好的办法来<span style="color: black;">处理</span>此问题的,<span style="color: black;">此刻</span>有了swoole扩展,利用swoole<span style="color: black;">供给</span>的task功能<span style="color: black;">能够</span>很方便做出一个连接池来。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$serv = new swoole_server("127.0.0.1", 9508);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$serv->set(array(</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> worker_num => 100,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">task_worker_num => 10, //MySQL连接的数量 ));</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">function my_onReceive($serv, $fd, $from_id, $data)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> //taskwait<span style="color: black;">便是</span>投递一条任务,<span style="color: black;">这儿</span>直接传递SQL语句了</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//<span style="color: black;">而后</span>阻塞等待SQL完成 $result = $serv->taskwait("show tables");</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> if ($result !== false) {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> list($status, $db_res) = explode(:, $result, 2);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> if ($status == OK) {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//数据库操作成功了,执行业务<span style="color: black;">规律</span>代码,<span style="color: black;">这儿</span>就自动释放掉MySQL连接的占用 $serv->send($fd, var_export(unserialize($db_res), true) . "\n");</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> } else {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $serv->send($fd, $db_res);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> return;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> } else {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $serv->send($fd, "Error. Task timeout\n");</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">function my_onTask($serv, $task_id, $from_id, $sql)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> static $link = null;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> if ($link == null) {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $link = mysqli_connect("127.0.0.1", "root", "root", "test");</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if (!$link) {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $link = null;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $serv->finish("ER:" . mysqli_error($link));</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> return;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $result = $link->query($sql);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> if (!$result) {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$serv->finish("ER:" . mysqli_error($link));</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> return;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $data = $result->fetch_all(MYSQLI_ASSOC);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $serv->finish("OK:" . serialize($data));</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">function my_onFinish($serv, $data)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">echo "AsyncTask Finish:Connect.PID=" . posix_getpid() . PHP_EOL;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$serv->on(Receive, my_onReceive);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$serv->on(Task, my_onTask);</p>$serv->on(Finish,
页:
[1]