Make a message notification function based on swoole and redis
Use swoole to start the resident process, you need a few according to your own situation To determine, the number of swoole processes should be equal to the number of server cpu cores (Recommended learning: swoole video tutorial)
Use the resident process started by swoole to continuously detect redis The values in the queue can be weighted according to the key value, such as fast, medium and slow. If urgent processing is required, more processes can be used for large amounts of data. Generally, different numbers of processes can be allocated for execution.
The code below:
swoole startup code
function run() { try { $swoole = new \swoole_server(127.0.0.1, 9999); $swoole->set([ 'daemonize' => 1, //是否开启守护进程 'worker_num' => 8, //实际需要去设定 'log_file' => __APP_LOGS_PATH__ . '/swoole.log' ]); $swoole->on('WorkerStart', 'onWorkerStart'); $swoole->on('Receive', 'onReceive'); $swoole->start(); } catch (\Exception $e) { logs(['err_code' => $e->getCode(), 'err_msg' => $e->getMessage()], 'error'); } }
swoole monitors the data in the redis queue in real time, Ranking by weight based on key values
Code
function onWorkerStart(swoole_server $swoole, $worker_id) { $chQuick = [0, 1, 2, 3]; $chNormal = [4, 5]; $chSlow = [6]; for ($i = 1; $i <= 3000; $i++) { $redis = connectRedis();//断线重连redis $queueData = $keys = []; if (in_array($worker_id, $chQuick)) { if ($redis->llen(QUEUE_QUICK)) $keys[] = QUEUE_QUICK; if ($keys) $queueData = $redis->brpop(QUEUE_QUICK, 5); } elseif (in_array($worker_id, $chNormal)) { if ($redis->llen(QUEUE_NORMAL)) $keys[] = QUEUE_NORMAL; if ($redis->llen(QUEUE_QUICK)) $keys[] = QUEUE_QUICK; if ($keys) $queueData = $redis->brpop(QUEUE_NORMAL, QUEUE_QUICK, 5); } elseif (in_array($worker_id, $chSlow)) { if ($redis->llen(QUEUE_SLOW)) $keys[] = QUEUE_SLOW; if ($redis->llen(QUEUE_NORMAL)) $keys[] = QUEUE_NORMAL; if ($redis->llen(QUEUE_QUICK)) $keys[] = QUEUE_QUICK; if ($keys) $queueData = $redis->brpop(QUEUE_SLOW, QUEUE_QUICK, QUEUE_NORMAL, 5); } else { if ($redis->llen(QUEUE_FAIL)) $keys[] = QUEUE_FAIL; if ($redis->llen(QUEUE_SLOW)) $keys[] = QUEUE_SLOW; if ($redis->llen(QUEUE_NORMAL)) $keys[] = QUEUE_NORMAL; if ($redis->llen(QUEUE_QUICK)) $keys[] = QUEUE_QUICK; if ($keys) $queueData = $redis->brpop(QUEUE_FAIL, QUEUE_QUICK, QUEUE_NORMAL, QUEUE_SLOW, 5); } logs('test'.$keys.'%%'.$queueData); if ($queueData) { $queueName = $queueData[0]; $message = $queueData[1]; if ($worker_id == QUEUE_FAIL_WORKER_ID && $queueName == QUEUE_FAIL) { call_user_func_array('retryPostMessage', [&$message, &$redis]); } else { call_user_func_array('postMessage', [&$message, &$redis]); } } else { sleep(5); } } sleep(10); $redis->close(); unset($redis); method_exists($swoole, 'stop') ? $swoole->stop() : @exit; }
The for loop inside is to be used with the sleep function. Three failures can be recorded If it fails, you can handle it manually. The above is a simple message notification push function based on swoole and redis queue. The specific configuration can be written in the configuration or stored in the database.
The convenience is that you can perform modular separation according to blocks, according to your own needs, business modules, and use different key values and different operations to process your own messages, which can be processed very efficiently in redis. The data
The only shortcoming is that the memory occupied by the process is relatively high
The above is the detailed content of How to do message notification in swoole. For more information, please follow other related articles on the PHP Chinese website!