企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# workerman - 使用Mysql ``` <pre class="calibre14">``` $db <span class="token">=</span> <span class="token5">new</span> <span class="token1"><span class="token2">\</span>Workerman<span class="token2">\</span>MySQL<span class="token2">\</span>Connection</span><span class="token2">(</span><span class="token4">'host'</span><span class="token2">,</span> <span class="token4">'port'</span><span class="token2">,</span> <span class="token4">'user'</span><span class="token2">,</span> <span class="token4">'password'</span><span class="token2">,</span> <span class="token4">'db_name'</span><span class="token2">)</span><span class="token2">;</span> $db<span class="token">-</span><span class="token">></span><span class="token1">query</span><span class="token2">(</span><span class="token4">"SELECT ID,Sex FROM `Persons` WHERE sex='M' AND ID = 1"</span><span class="token2">)</span><span class="token2">;</span> $db<span class="token">-</span><span class="token">></span><span class="token1">row</span><span class="token2">(</span><span class="token4">"SELECT ID,Sex FROM `Persons` WHERE sex='M'"</span><span class="token2">)</span><span class="token2">;</span> 一行数据 $db<span class="token">-</span><span class="token">></span><span class="token1">column</span><span class="token2">(</span><span class="token4">"SELECT `ID` FROM `Persons` WHERE sex='M'"</span><span class="token2">)</span><span class="token2">;</span> 一列数据 $db<span class="token">-</span><span class="token">></span><span class="token1">single</span><span class="token2">(</span><span class="token4">"SELECT ID FROM `Persons` WHERE sex='M'"</span><span class="token2">)</span><span class="token2">;</span> 单个值 $db<span class="token">-</span><span class="token">></span><span class="token1">beginTrans</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>事务开始 $db<span class="token">-</span><span class="token">></span><span class="token1">commitTrans</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// or $db->rollBackTrans(); 回滚或提交</span> 其余操作用使用 query即可 use \Workerman\Worker<span class="token2">;</span> <span class="token6">//use \Workerman\Lib\Timer;</span> require_once __DIR__ <span class="token2">.</span> <span class="token4">'/Workerman/Autoloader.php'</span><span class="token2">;</span> require_once __DIR__ <span class="token2">.</span> <span class="token4">'/Workerman/Mysql.php'</span><span class="token2">;</span> $worker <span class="token">=</span> <span class="token5">new</span> <span class="token1">Worker</span><span class="token2">(</span><span class="token4">"http://0.0.0.0:2345"</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 开启多少个进程运行定时任务,注意业务是否在多进程有并发问题</span> $worker<span class="token">-</span><span class="token">></span>count <span class="token">=</span> <span class="token3">1</span><span class="token2">;</span> $worker<span class="token">-</span><span class="token">></span>onWorkerStart <span class="token">=</span> <span class="token5">function</span><span class="token2">(</span>$worker<span class="token2">)</span> <span class="token2">{</span> global $db<span class="token2">;</span> $db <span class="token">=</span> <span class="token5">new</span> <span class="token1"><span class="token2">\</span>Workerman<span class="token2">\</span>MySQL<span class="token2">\</span>Connection</span><span class="token2">(</span><span class="token4">'127.0.0.1'</span><span class="token2">,</span> <span class="token4">'3306'</span><span class="token2">,</span> <span class="token4">'root'</span><span class="token2">,</span> <span class="token4">'mysql'</span><span class="token2">,</span> <span class="token4">'laravel'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">;</span> $worker<span class="token">-</span><span class="token">></span>onMessage <span class="token">=</span> <span class="token5">function</span><span class="token2">(</span>$connection<span class="token2">,</span> $data<span class="token2">)</span> <span class="token2">{</span> <span class="token6">// 通过全局变量获得db实例</span> global $db<span class="token2">;</span> <span class="token6">// 执行SQL</span> $all_tables <span class="token">=</span> $db<span class="token">-</span><span class="token">></span><span class="token1">query</span><span class="token2">(</span><span class="token4">'select * from users'</span><span class="token2">)</span><span class="token2">;</span> $connection<span class="token">-</span><span class="token">></span><span class="token1">send</span><span class="token2">(</span><span class="token1">dump</span><span class="token2">(</span>$all_tables<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">;</span> <span class="token6">// 运行worker</span> Worker<span class="token2">:</span><span class="token2">:</span><span class="token1">runAll</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - 定时器加心跳 ``` <pre class="calibre14">``` require_once __DIR__ <span class="token2">.</span> <span class="token4">'/Workerman/Autoloader.php'</span><span class="token2">;</span> use Workerman\Worker<span class="token2">;</span> use Workerman\Lib\Timer<span class="token2">;</span> <span class="token6">// 心跳间隔55秒</span> <span class="token1">define</span><span class="token2">(</span><span class="token4">'HEARTBEAT_TIME'</span><span class="token2">,</span> <span class="token3">55</span><span class="token2">)</span><span class="token2">;</span> $worker <span class="token">=</span> <span class="token5">new</span> <span class="token1">Worker</span><span class="token2">(</span><span class="token4">'text://0.0.0.0:1234'</span><span class="token2">)</span><span class="token2">;</span> $worker<span class="token">-</span><span class="token">></span>onMessage <span class="token">=</span> <span class="token5">function</span><span class="token2">(</span>$connection<span class="token2">,</span> $msg<span class="token2">)</span> <span class="token2">{</span> <span class="token6">// 给connection临时设置一个lastMessageTime属性,用来记录上次收到消息的时间</span> $connection<span class="token">-</span><span class="token">></span>lastMessageTime <span class="token">=</span> <span class="token1">time</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 其它业务逻辑...</span> <span class="token2">}</span><span class="token2">;</span> <span class="token6">// 进程启动后设置一个每秒运行一次的定时器</span> $worker<span class="token">-</span><span class="token">></span>onWorkerStart <span class="token">=</span> <span class="token5">function</span><span class="token2">(</span>$worker<span class="token2">)</span> <span class="token2">{</span> Timer<span class="token2">:</span><span class="token2">:</span><span class="token1">add</span><span class="token2">(</span><span class="token3">1</span><span class="token2">,</span> <span class="token5">function</span><span class="token2">(</span><span class="token2">)</span><span class="token1">use</span><span class="token2">(</span>$worker<span class="token2">)</span><span class="token2">{</span> $time_now <span class="token">=</span> <span class="token1">time</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$worker<span class="token">-</span><span class="token">></span>connections as $connection<span class="token2">)</span> <span class="token2">{</span> <span class="token6">// 有可能该connection还没收到过消息,则lastMessageTime设置为当前时间</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$connection<span class="token">-</span><span class="token">></span>lastMessageTime<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> $connection<span class="token">-</span><span class="token">></span>lastMessageTime <span class="token">=</span> $time_now<span class="token2">;</span> <span class="token5">continue</span><span class="token2">;</span> <span class="token2">}</span> <span class="token6">// 上次通讯时间间隔大于心跳间隔,则认为客户端已经下线,关闭连接</span> <span class="token5">if</span> <span class="token2">(</span>$time_now <span class="token">-</span> $connection<span class="token">-</span><span class="token">></span>lastMessageTime <span class="token">></span> HEARTBEAT_TIME<span class="token2">)</span> <span class="token2">{</span> $connection<span class="token">-</span><span class="token">></span><span class="token1">close</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">;</span> Worker<span class="token2">:</span><span class="token2">:</span><span class="token1">runAll</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - 定时任务处理队列 ``` <pre class="calibre16">``` use \Workerman\Worker<span class="token2">;</span> use \Workerman\Lib\Timer<span class="token2">;</span> require_once __DIR__ <span class="token2">.</span> <span class="token4">'/Workerman/Autoloader.php'</span><span class="token2">;</span> require_once __DIR__ <span class="token2">.</span> <span class="token4">'/Workerman/Mysql.php'</span><span class="token2">;</span> $worker <span class="token">=</span> <span class="token5">new</span> <span class="token1">Worker</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 开启多少个进程运行定时任务,注意业务是否在多进程有并发问题</span> $worker<span class="token">-</span><span class="token">></span>count <span class="token">=</span> <span class="token3">4</span><span class="token2">;</span> $worker<span class="token">-</span><span class="token">></span>onWorkerStart <span class="token">=</span> <span class="token5">function</span><span class="token2">(</span>$worker<span class="token2">)</span> <span class="token2">{</span> global $db<span class="token2">,</span>$redis<span class="token2">;</span> $db <span class="token">=</span> <span class="token5">new</span> <span class="token1"><span class="token2">\</span>Workerman<span class="token2">\</span>MySQL<span class="token2">\</span>Connection</span><span class="token2">(</span><span class="token4">'127.0.0.1'</span><span class="token2">,</span> <span class="token4">'3306'</span><span class="token2">,</span> <span class="token4">'root'</span><span class="token2">,</span> <span class="token4">'mysql'</span><span class="token2">,</span> <span class="token4">'laravel'</span><span class="token2">)</span><span class="token2">;</span> $redis <span class="token">=</span> <span class="token5">new</span> <span class="token1">Redis</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">connect</span><span class="token2">(</span><span class="token4">'127.0.0.1'</span><span class="token2">,</span> <span class="token3">6379</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$worker<span class="token">-</span><span class="token">></span>id <span class="token">==</span> <span class="token3">0</span><span class="token2">)</span><span class="token6">//只在第一个上边使用定时</span> <span class="token2">{</span> Timer<span class="token2">:</span><span class="token2">:</span><span class="token1">add</span><span class="token2">(</span><span class="token3">5</span><span class="token2">,</span> <span class="token5">function</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> global $redis<span class="token2">;</span> <span class="token5">while</span><span class="token2">(</span><span class="token3">true</span><span class="token2">)</span> <span class="token2">{</span> $res <span class="token">=</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">rpop</span><span class="token2">(</span><span class="token4">"sendsms"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>$res<span class="token2">)</span> <span class="token2">{</span> $arr <span class="token">=</span> <span class="token1">unserialize</span><span class="token2">(</span>$res<span class="token2">)</span><span class="token2">;</span><span class="token6">//取出队列</span> echo <span class="token4">'取出成功'</span><span class="token2">.</span><span class="token1">date</span><span class="token2">(</span><span class="token4">"Y-m-d H:i:s"</span><span class="token2">)</span><span class="token2">.</span><span class="token4">"\n"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> <span class="token5">return</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">;</span> <span class="token6">// 运行worker</span> Worker<span class="token2">:</span><span class="token2">:</span><span class="token1">runAll</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> ``` ```