Swooleʵ¼ù£º¹¹½¨¸ßÐÔÄܵÄÐÐÁÐϵͳ
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬ÖÖÖָ߲¢·¢³¡¾°Ò²Ô½À´Ô½Æձ顣ÔÚÕâЩ³¡¾°Ï£¬¹Å°åµÄÐÐÁÐϵͳÍùÍù»á·ºÆðÐÔÄÜÆ¿¾±£¬ÎÞ·¨Öª×ãʵʱÐÔµÄÐèÇó¡£ÎªÏàʶ¾öÕâ¸öÎÊÌ⣬swoole×÷ΪһÖÖÊÂÎñÇý¶¯µÄ¸ßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬³ÉΪÁËÒ»¸öºÜÊǺõÄÑ¡Ôñ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùʹÓÃswoole¹¹½¨Ò»¸ö¸ßÐÔÄܵÄÐÐÁÐϵͳ£¬ÒÔÓ¦¶Ô¸ß²¢·¢ÓªÒµ³¡¾°ÖеÄÌôÕ½¡£
Ò»¡¢Ê²Ã´ÊÇÐÐÁÐϵͳ
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÏàʶʲôÊÇÐÐÁÐϵͳ¡£ÐÐÁÐϵͳÊÇÒ»ÖÖÊý¾Ý½á¹¹£¬ÓÃÓÚ´æ´¢ÐèÒª±»´¦Öóͷ£µÄʹÃü»òÐÂÎÅ£¬²¢Æ¾Ö¤Ò»¶¨Ë³Ðò¾ÙÐд¦Öóͷ£¡£Í¨³£ÇéÐÎÏ£¬ÐÐÁÐϵͳʹÓÃFIFO£¨ÏȽøÏȳö£©µÄ·½·¨¾ÙÐÐʹÃü»òÐÂÎŵĴ¦Öóͷ£¡£µ±Ò»¸öʹÃü»òÐÂÎű»·ÅÈëÐÐÁÐʱ£¬Ëü¾Í³ÉΪÁËÐÐÁеÄβ²¿£¬µ±ÐèÒª´¦Öóͷ£Ê¹Ãü»òÐÂÎÅʱ£¬´ÓÐÐÁÐÍ·²¿×îÏÈ´¦Öóͷ£¡£ÐÐÁÐϵͳͨ³£±»ÓÃÓÚ´¦Öóͷ£¸ß¸ºÔØ¡¢¸ß²¢·¢¡¢¸ß¿ÉÓõÄÓªÒµ³¡¾°£¬ºÃ±ÈµçÉÌƽ̨¡¢É罻ƽ̨¡¢ÓÎϷƽ̨µÈ¡£
¶þ¡¢SwooleÏÈÈÝ
SwooleÊÇÒ»ÖÖ»ùÓÚPHPµÄÊÂÎñÇý¶¯µÄ¸ßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬¾ßÓÐг̡¢Òì²½IO¡¢¶àÀú³Ì¡¢¶àÏ̵߳ÈÌØÕ÷¡£Ëü¿ÉÒÔ×ÊÖúPHPÓ¦ÓóÌÐòÔڸ߲¢·¢µÄÓªÒµ³¡¾°ÖлñµÃ¸üºÃµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ¡£SwooleÒѳÉΪPHPÓïÑÔÖÐ×îÊܽӴýµÄ¸ßÐÔÄÜÍøÂçͨѶ¿ò¼Ü¡£SwooleÄÚÖÃÁËÒì²½TCP/UDPÍøÂç±à³Ì¡¢Òì²½Îļþϵͳ¡¢Ð³ÌÍøÂçЧÀÍÆ÷¡¢Ò첽ʹÃü¡¢ÂþÑÜʽ°²ÅÅ¡¢Òì²½SQLiteµÈÖÖÖÖ¹¦Ð§¡£Óë¹Å°åµÄPHPÓ¦ÓóÌÐòÏà±È£¬Ê¹ÓÃSwoole¿ª·¢µÄÓ¦ÓóÌÐò¿ÉÒÔ»ñµÃ¸ü¿ìµÄÏìÓ¦ËÙÂÊ¡¢¸üÉÙµÄ×ÊÔ´Õ¼Óᢸü¸ßµÄ²¢·¢ÄÜÁ¦µÈÓŵ㡣
Èý¡¢Ê¹ÓÃSwoole¹¹½¨ÐÐÁÐϵͳ
»ùÓÚÒÔÉÏÏÈÈÝ£¬ÎÒÃÇ¿ÉÒÔʹÓÃSwooleÀ´¹¹½¨Ò»¸ö¸ßÐÔÄܵÄÐÐÁÐϵͳ¡£Ïêϸ°ì·¨ÈçÏ£º
1.Éè¼ÆÐÐÁнṹ
ÓÉÓÚÐÐÁÐϵͳÖ÷ҪʹÓÃFIFOµÄ·½·¨¾ÙÐÐʹÃü»òÐÂÎŵĴ¦Öóͷ££¬Òò´ËÎÒÃÇÐèÒªÉè¼ÆÒ»¸öÇкÏFIFO¹æÔòµÄÐÐÁнṹ¡£ÐÐÁнṹ¿ÉÒÔʹÓÃÊý×é¡¢Á´±íµÈÊý¾Ý½á¹¹¾ÙÐÐʵÏÖ¡£
2.»ùÓÚSwooleʵÏÖÒ첽ʹÃüÐÐÁÐ
ÔÚʹÓÃSwoole¹¹½¨ÐÐÁÐϵͳµÄÀú³ÌÖУ¬ÎÒÃÇÐèҪʵÏÖÒ»¸öÒ첽ʹÃüÐÐÁС£Ò첽ʹÃüÐÐÁÐÓëͨË×µÄʹÃüÐÐÁвî±ð£¬ÔÚʹÓÃÒ첽ʹÃüÐÐö¾ÙÐÐʹÃü´¦Öóͷ£Ê±£¬ÏµÍ³²»»áÛÕ±ÕÆÚ´ýʹÃüµÄÍê³É¡£ÕâÖÖ·½·¨¿ÉÒÔÌá¸ßϵͳµÄÍÌÍÂÁ¿ºÍЧÂÊ¡£
3.ʹÓÃSwooleʵÏÖÐÐÁеÄÏûºÄÕߺÍÉú²úÕß
ÔÚÐÐÁÐϵͳÖУ¬ÐèÒªÓÐÏûºÄÕߺÍÉú²úÕß¡£Éú²úÕßÌØÊâÈÏÕ潫ʹÃüѹÈëÐÐÁУ¬ÏûºÄÕßÈÏÕæ´ÓÐÐÁÐÖÐÈ¡³öʹÃü²¢Ö´ÐС£ÔÚʹÓÃSwoole¹¹½¨ÐÐÁÐϵͳʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃгÌÀ´ÊµÏÖÏûºÄÕߺÍÉú²úÕß¡£
4.ʹÓÃSwooleʵÏÖÂþÑÜʽÐÐÁÐ
¹ØÓڸ߲¢·¢µÄÓªÒµÐèÇó£¬ÎÒÃÇ¿ÉÄÜÐèÒª¹¹½¨Ò»¸öÂþÑÜʽÐÐÁÐϵͳ¡£ÕâÖÖÐÐÁÐϵͳ¿ÉÒÔ°ÑÐÐÁÐÖеÄʹÃü·ÖÅɵ½¶à¸öЧÀÍÆ÷ÉϾÙÐд¦Öóͷ££¬ÒÔ¼ÓËÙʹÃüµÄ´¦Öóͷ£ËÙÂÊ¡£ÔÚʹÓÃSwoole¹¹½¨ÂþÑÜʽÐÐÁÐϵͳʱ£¬¿ÉÒÔʹÓÃSwooleÌṩµÄÂþÑÜʽ°²ÅŹ¦Ð§À´ÊµÏÖ¡£
ÒÔÉϾÍÊÇʹÓÃSwoole¹¹½¨¸ßÐÔÄÜÐÐÁÐϵͳµÄ»ù±¾°ì·¨¡£½ÓÏÂÀ´ÎÒÃǽ«ÒÔÒ»¸öµçÉÌÍøվΪÀý£¬Ïêϸ½â˵ÔõÑùʹÓÃSwoole¹¹½¨Ò»¸ö¸ßÐÔÄÜÐÐÁÐϵͳ¡£
ËÄ¡¢Ê¹ÓÃSwoole¹¹½¨µçÉÌÍøÕ¾µÄ¶©µ¥´¦Öóͷ£ÐÐÁÐ
ÔÚµçÉÌÍøÕ¾ÖУ¬¶©µ¥´¦Öóͷ£ÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄÓªÒµ¡£ÎªÁËÓ¦¶Ô¸ß²¢·¢¡¢¸ß¸ºÔصÄÓªÒµ³¡¾°£¬ÎÒÃÇ¿ÉÒÔʹÓÃSwoole¹¹½¨Ò»¸ö¸ßÐÔÄܵĶ©µ¥´¦Öóͷ£ÐÐÁС£ÏÂÃæÊÇÏêϸ°ì·¨£º
1.Éè¼Æ¶©µ¥´¦Öóͷ£ÐÐÁнṹ
ÎÒÃÇ¿ÉÒÔʹÓÃÊý×éÀ´ÊµÏÖ¶©µ¥´¦Öóͷ£ÐÐÁУ¬²¢Ê¹ÓÃFIFOµÄÔÔò¾ÙÐÐʹÃü´¦Öóͷ£¡£
// ¶©µ¥´¦Öóͷ£ÐÐÁнṹ $orderQueue = array();
µÇ¼ºó¸´ÖÆ
2.»ùÓÚSwooleʵÏÖÒ첽ʹÃüÐÐÁÐ
ʹÓÃSwooleÌṩµÄTask Worker¹¦Ð§£¬¿ÉÒÔʵÏÖÒ첽ʹÃüÐÐÁС£
// SwooleÒ첽ʹÃüÐÐÁÐ $serv = new SwooleServer("127.0.0.1", 9501); $serv->set(array( 'task_worker_num' => 4, )); $serv->on('receive', function($serv, $fd, $from_id, $data) { $task_id = $serv->task($data); echo "Dispath AsyncTask: id=$task_id "; }); $serv->on('task', function ($serv, $task_id, $from_id, $data) use ($orderQueue) { $orderQueue[] = $data; echo "New Task: id=$task_id, data=$data "; }); $serv->on('finish', function ($serv, $task_id, $data) { echo "Task Finished: id=$task_id, data=$data "; }); $serv->start();
µÇ¼ºó¸´ÖÆ
3.ʹÓÃSwooleʵÏÖÐÐÁеÄÏûºÄÕߺÍÉú²úÕß
ÔÚÏûºÄÕ߶ˣ¬ÎÒÃÇ¿ÉÒÔʹÓÃг̾ÙÐÐʹÃüµÄ´¦Öóͷ£¡£ÔÚÉú²úÕ߶ˣ¬ÎÒÃÇÖ»Ð轫ʹÃüѹÈëÐÐÁм´¿É¡£
// ÏûºÄÕß Coun(function () use ($orderQueue) { while (true) { if (!empty($orderQueue)) { $order = array_shift($orderQueue); // ´¦Öóͷ£¶©µ¥ echo "Processing Order: $order "; } Co::sleep(0.1); } }); // Éú²úÕß for ($i = 1; $i <= 10000; $i++) { $data = "Order $i"; $client = new SwooleClient(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", function($cli) use ($data){ $cli->send($data . PHP_EOL); }); $client->connect('127.0.0.1', 9501, 0.5); }
µÇ¼ºó¸´ÖÆ
4.ʹÓÃSwooleʵÏÖÂþÑÜʽÐÐÁÐ
ΪÁËÓ¦¶Ô¸ü¸ßµÄ²¢·¢Á¿£¬ÎÒÃÇ¿ÉÒÔʹÓÃSwooleÌṩµÄÂþÑÜʽ¹¦Ð§£¬ÔÚ¶à¸öЧÀÍÆ÷ÉϾÙÐÐʹÃüµÄ´¦Öóͷ£¡£
// Éú²úÕß¶Ë for ($i = 1; $i <= 10000; $i++) { $data = "Order $i"; $server_list = array( array('host'=>'192.168.0.100', 'port'=>9501), array('host'=>'192.168.0.101', 'port'=>9501), array('host'=>'192.168.0.102', 'port'=>9501), array('host'=>'192.168.0.103', 'port'=>9501), ); $client = new SwooleClient(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", function($cli) use ($data) { $cli->send($data . PHP_EOL); }); $client->connect($server_list[array_rand($server_list)]['host'], $server_list[array_rand($server_list)]['port'], 0.5); } // ЧÀÍ¶Ë $serv = new SwooleServer("127.0.0.1", 9501); $serv->set(array( 'task_worker_num' => 4, 'worker_num' => 4, 'task_ipc_mode' => 3, 'message_queue_key' => 0x70001001, )); $serv->on('receive', function($serv, $fd, $from_id, $data) { $task_id = $serv->task($data); echo "Dispath AsyncTask: id=$task_id "; }); $serv->on('task', function ($serv, $task_id, $from_id, $data) use ($orderQueue) { $orderQueue[] = $data; echo "New Task: id=$task_id, data=$data "; $serv->finish($data); }); $serv->on('finish', function ($serv, $task_id, $data) { echo "Task Finished: id=$task_id, data=$data "; }); $serv->start();
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏ´úÂ룬ÎÒÃǾͿÉÒÔÀֳɵØʹÓÃSwoole¹¹½¨Ò»¸ö¸ßÐÔÄܵĶ©µ¥´¦Öóͷ£ÐÐÁС£Õâ¸öÐÐÁÐϵͳ¼È¿ÉÒÔÓ¦¶Ô¸ß²¢·¢¡¢¸ß¸ºÔصij¡¾°£¬Ò²Ö§³ÖÂþÑÜʽ°²ÅÅ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÓÅ»¯Õâ¸ö»ù´¡ÐÐÁÐϵͳÀ´¹¹½¨Ô½·¢Öش󡢸ßЧµÄÓªÒµ³¡¾°¡£
Îå¡¢×ܽá
±¾ÎÄÖ÷ÒªÌÖÂÛÁËÔõÑùʹÓÃSwoole¹¹½¨¸ßÐÔÄܵÄÐÐÁÐϵͳ£¬ÒÔÓ¦¶Ô¸ß²¢·¢¡¢¸ß¸ºÔصÄÓªÒµ³¡¾°¡£ÎÒÃÇͨ¹ýÉÏÃæµÄʾÀý£¬ÏêϸÏÈÈÝÁË»ùÓÚSwooleµÄÒ첽ʹÃüÐÐÁС¢ÏûºÄÕߺÍÉú²úÕߣ¬ÒÔ¼°ÂþÑÜʽÐÐÁеĹ¹½¨ÒªÁ졣ϣÍû±¾ÎĶԶÁÕßÃ÷È·ºÍʹÓÃSwoole´òÔì¸ßÐÔÄÜÐÐÁÐϵͳÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇSwooleʵ¼ù£º¹¹½¨¸ßÐÔÄܵÄÐÐÁÐϵͳµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡