Swoole½ø½×£º¶¯Ì¬À©ÈÝÓë¸ß¿ÉÓÃÐÔÉè¼Æ
Ëæ×Å»¥ÁªÍøµÄÒ»Ö±Éú³¤ºÍÓ¦Óó¡¾°µÄÒ»Ö±À©´ó£¬¼òµ¥Ó¦ÓóÌÐò¶Ôϵͳ×ÊÔ´µÄÐèÇóÒ²Ô½À´Ô½¸ß£¬ÆäÖУ¬¸ß²¢·¢´¦Öóͷ£ÊÇ»¥ÁªÍøÓ¦ÓÃÖеÄÒ»ºÆ½Ùµã¡£swoole×÷ΪphpµÄ¸ßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬ÒѾ³ÉΪÁËphpÁìÓòÖеÄÉñÆ÷£¬ËüÄܹ»×ÊÖúÎÒÃÇÇáËɹ¹½¨¸ßÐÔÄܵÄÍøÂçÓ¦ÓóÌÐò¡£¿ÉÊÇ£¬¹ØÓÚÒ»¸ö´ó¹æÄ£µÄ»¥ÁªÍøÓ¦Ó㬴¿´âµÄ¸ßÐÔÄÜ»¹Ô¶Ô¶²»·ó£¬ÎÒÃÇ»¹ÐèҪ˼Á¿ÆäËûÒòËØ£¬ºÃ±È¶¯Ì¬À©Èݺ͸߿ÉÓÃÐÔµÈÎÊÌâ¡£
±¾ÎĽ«´ÓSwooleÓ¦ÓóÌÐòµÄ¶¯Ì¬À©Èݺ͸߿ÉÓÃÐÔÁ½¸ö·½Ãæ¾ÙÐÐÏÈÈÝ£¬×ÊÖú¶ÁÕß¹¹½¨Ò»¸öÇ¿Ê¢¶øÎȹ̵ÄSwooleÓ¦ÓóÌÐò¡£
¶¯Ì¬À©ÈÝ
ÔÚ»¥ÁªÍøÓ¦ÓÃÖУ¬ÎÒÃÇͨ³£ÐèÒªÓ¦¶Ôº£Á¿Óû§»á¼ûµÄÇéÐΣ¬¶ø»úеµÄÐÔÄܺÍ×ÊÔ´ÊÇÓÐÏ޵ģ¬ÒÔÊǾ³£ÐèÒª¶¯Ì¬À©ÈÝÀ´Öª×ãÓ¦ÓõÄÐèÇó¡£Swoole×÷Ϊһ¿î¸ßÐÔÄܵÄÍøÂçͨѶ¿ò¼Ü£¬×Ô¼º¾Í¾ß±¸Ò»¶¨µÄ³ÐÔØÄÜÁ¦£¬¿ÉÊǹØÓÚ´óÐ͵Ļ¥ÁªÍøÓ¦ÓöøÑÔ£¬Ö±½ÓÒÀÀµ¼òµ¥µÄSwooleÀú³ÌÀ´´¦Öóͷ£ËùÓÐÇëÇó£¬ÏÔÈ»ÊDz»·óµÄ£¬Òò´ËÎÒÃÇÐèҪͨ¹ý¶¯Ì¬À©ÈÝÀ´ÊµÏÖÓ¦ÓõĺáÏòÀ©Õ¹¡£ÏÂÃæÊǼ¸ÖÖ³£¼ûµÄ¶¯Ì¬À©Èݼƻ®¡£
¼Æ»®Ò»£ºÀú³ÌÄ£×Ó
SwooleµÄÀú³ÌÄ£×Ó¿ÉÒÔÇáËÉʵÏÖ¶¯Ì¬À©ÈÝ£¬Ö»ÐèÒªÆô¶¯¶à¸öSwooleÀú³Ì£¬Ã¿¸öÀú³Ì¶¼¼àÌýͳһ¸ö¶Ë¿Ú£¬¾Í¿ÉÒÔʵÏÖ¸ºÔØƽºâ£¬´Ó¶øÖª×ã´ó×ÚÓû§µÄ»á¼ûÐèÇó¡£ÔÚSwooleÖУ¬¿ÉÒÔͨ¹ý½¨Éè¶à¸öWorkerÀú³ÌÀ´Íê³É¶¯Ì¬À©ÈÝ£¬ÀýÈçÏÂÃæÕâ¶Î´úÂ룺
<?php $workers = []; $workerNum = 4; // WorkerÀú³ÌÊý for ($i = 0; $i < $workerNum; $i++) { $process = new SwooleProcess(function (SwooleProcess $process) { // ¿ªÆôSwooleЧÀÍÆ÷ $server = new SwooleHttpServer('0.0.0.0', 9501); $server->on('request', function ($request, $response) { $response->header('Content-Type', 'text/plain'); $response->end("Hello World from Swoole "); }); $server->start(); }); $pid = $process->start(); $workers[$pid] = $process; } // ÆÚ´ýËùÓÐWorkerÀú³ÌÖ´ÐÐÍê±Ï foreach ($workers as $process) { $process->wait(); }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬Æô¶¯ÁË4¸öWorkerÀú³Ì£¬Ã¿¸öÀú³Ì¼àÌýͳһ¸ö¶Ë¿Ú£¬ÓÃÓÚ´¦Öóͷ£À´×Ô¿Í»§¶ËµÄÇëÇó¡£ÔÚÎüÊÕµ½ÇëÇóºó£¬Ã¿¸öWorkerÀú³Ì¶¼Äܹ»×ÔÁ¦´¦Öóͷ££¬ÊµÏÖÁ˸ºÔØƽºâºÍ¶¯Ì¬À©ÈÝ¡£
ÐèҪעÖصÄÊÇ£¬ËäȻͨ¹ýÀú³ÌÄ£×Ó¿ÉÒÔʵÏÖ¶¯Ì¬À©ÈÝ£¬¿ÉÊÇÔÚÏÖʵӦÓÃÖУ¬»¹ÐèҪעÖØÀú³ÌÖ®¼äµÄͨѶ¡¢Êý¾Ýͬ²½µÈÎÊÌ⣬²»È»¿ÉÄÜ»á´øÀ´ÐµÄÎÊÌâ¡£
¼Æ»®¶þ£ºÐ³ÌÄ£×Ó
³ýÁËÀú³ÌÄ£×ÓÍ⣬Swoole»¹Ö§³ÖгÌÄ£×Ó£¬Í¨¹ýг̵ÄÓÅÊÆ£¬¿ÉÒÔÇáËÉʵÏÖ¶¯Ì¬À©ÈÝ¡£ÔÚSwooleÖУ¬½¨Éè¶à¸öг̿ÉÒÔʵÏÖ¶à¸öʹÃüͬʱִÐУ¬´Ó¶øÌá¸ßÁËϵͳµÄ²¢·¢ÐÔÄÜ¡£ÀýÈçÏÂÃæÕâ¶Î´úÂ룺
<?php $coroutines = []; $coroutinesNum = 10; // гÌÊý for ($i = 0; $i < $coroutinesNum; $i++) { $coroutines[$i] = go(function () { $server = new SwooleHttpServer('0.0.0.0', 9501); $server->on('request', function ($request, $response) { $response->header('Content-Type', 'text/plain'); $response->end("Hello World from Swoole "); }); $server->start(); }); } SwooleCoroutineWaitGroup::wait($coroutines); // ÆÚ´ýËùÓÐгÌÖ´ÐÐÍê³É
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬Í¨¹ý½¨Éè¶à¸öгÌÀ´ÊµÏÖ¶¯Ì¬À©ÈÝ£¬´Ó¶øÖ§³ÖÁ˸ü¶àµÄ²¢·¢ÇëÇó£¬Ìá¸ßÁËϵͳµÄÐÔÄÜ¡£
¼Æ»®Èý£ºÒ첽ģ×Ó
ͨ¹ýÒ첽ģ×ÓÒ²¿ÉÒÔʵÏÖ¶¯Ì¬À©ÈÝ¡£ÔÚSwooleÖУ¬Ò첽ģ×ӵij£¼ûʵÏÖ·½·¨ÊÇͨ¹ý½¨Éè¶à¸öÒ첽ʹÃüÀ´´¦Öóͷ£ÇëÇ󣬺ñÈʹÓÃSwooleµÄÒì²½HTTP¿Í»§¶Ë¿âswoole_http_client¡£ÀýÈçÏÂÃæÕâ¶Î´úÂ룺
<?php $httpClients = []; $httpClientNum = 10; // Òì²½HTTP¿Í»§¶ËÊý for ($i = 0; $i < $httpClientNum; $i++) { $httpClients[$i] = new SwooleHttpClient('www.example.com', 80); } foreach ($httpClients as $httpClient) { $httpClient->get('/', function ($httpClient) { echo $httpClient->body; $httpClient->close(); }); }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬½¨ÉèÁË10¸öÒì²½HTTP¿Í»§¶ËʵÀý£¬Ã¿¸ö¿Í»§¶Ë²¢·¢Ö´ÐÐÇëÇó£¬ÊµÏÖÁ˶¯Ì¬À©ÈÝ¡£ÐèҪעÖصÄÊÇ£¬Ò첽ģ×ÓÐèÒª¶Ôÿ¸öʹÃü¾ÙÐйýʧ´¦Öóͷ££¬²»È»¿ÉÄܻᵼÖÂÕû¸öÓ¦ÓóÌÐòÍ߽⡣
¸ß¿ÉÓÃÐÔÉè¼Æ
³ýÁ˶¯Ì¬À©ÈÝÍ⣬¸ß¿ÉÓÃÐÔÉè¼ÆÒ²ÊÇÒ»¸ö±ØÐè˼Á¿µÄÒòËØ¡£¹ØÓÚÒ»¸ö´óÐ͵Ļ¥ÁªÍøÓ¦ÓöøÑÔ£¬¼òµ¥µÄSwooleÀú³Ì²¢²»¿É¿¿£¬¿ÉÄÜ»áÓöµ½Ðí¶àÎÊÌ⣬ºÃ±ÈÀú³Ì¹Òµô¡¢ÍøÂç¹ÊÕÏ¡¢´ÅÅ̹ÊÕϵȣ¬ÕâЩÎÊÌⶼ¿ÉÄܵ¼ÖÂÓ¦ÓóÌÐòÎÞ·¨Õý³£ÔËÐС£Òò´Ë£¬ÐèÒª¶ÔSwooleÓ¦ÓóÌÐò¾ÙÐи߿ÉÓÃÐÔÉè¼Æ£¬È·±£Ó¦ÓóÌÐòÔÚ·ºÆðÎÊÌâʱÈÔÈ»Äܹ»Õý³£ÊÂÇé¡£
¼Æ»®Ò»£º¶àÀú³ÌÄ£×Ó
¶àÀú³ÌÄ£×ÓÊÇÒ»ÖÖ³£¼ûµÄ¸ß¿ÉÓÃÐÔÉè¼Æ¼Æ»®£¬Í¨¹ý½«SwooleÓ¦ÓóÌÐò²ð·Ö³É¶à¸öÀú³Ì£¬Ã¿¸öÀú³Ì¶¼ÔËÐÐͳһ·Ý´úÂ룬¿ÉÊÇÏ໥×ÔÁ¦¡£ÔÚij¸öÀú³Ì¹Òµôʱ£¬ÆäËûÀú³Ì¿ÉÒÔ½ÓÌæÆäÊÂÇ飬´Ó¶øʵÏÖÓ¦ÓóÌÐòµÄ¸ß¿ÉÓÃÐÔ¡£ÀýÈçÏÂÃæÕâ¶Î´úÂ룺
<?php // Æô¶¯¶à¸öWorkerÀú³Ì $workers = []; $workerNum = 4; // WorkerÀú³ÌÊý for ($i = 0; $i < $workerNum; $i++) { $process = new SwooleProcess(function (SwooleProcess $process) { // ¿ªÆôSwooleЧÀÍÆ÷ $server = new SwooleHttpServer('0.0.0.0', 9501); $server->on('request', function ($request, $response) { $response->header('Content-Type', 'text/plain'); $response->end("Hello World from Swoole "); }); $server->start(); }); $pid = $process->start(); $workers[$pid] = $process; } // ¼ì²âWorkerÀú³Ì SwooleTimer::tick(1000, function () use (&$workers) { foreach ($workers as $pid => $process) { if (!$process->isRunning()) { $process->start(); $workers[$pid] = $process; } } });
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬Æô¶¯ÁË4¸öWorkerÀú³Ì£¬Ã¿¸öÀú³Ì¼àÌýͳһ¸ö¶Ë¿Ú£¬ÓÃÓÚ´¦Öóͷ£À´×Ô¿Í»§¶ËµÄÇëÇó¡£ÎªÁËÈ·±£¶àÀú³ÌÄ£×ӵĸ߿ÉÓÃÐÔ£¬Ê¹ÓÃSwooleTimer׼ʱÆ÷¼ì²âÀú³ÌÊÇ·ñ´æ»î£¬ÈôÊÇij¸öÀú³Ì¹Òµô£¬¾ÍÆô¶¯Ò»¸öеÄÀú³ÌÈ¡´úËü¡£
ÐèҪעÖصÄÊÇ£¬¶àÀú³ÌÄ£×ÓÐèÒª¶ÔÀú³ÌÖ®¼äµÄͨѶ¡¢Êý¾Ýͬ²½µÈÎÊÌâ¾ÙÐд¦Öóͷ££¬²»È»¿ÉÄÜ»áÓöµ½ÐµÄÎÊÌâ¡£
¼Æ»®¶þ£º¸ºÔØƽºâ»úÖÆ
¸ºÔØƽºâ»úÖÆÒ²ÊÇÒ»ÖÖ³£¼ûµÄ¸ß¿ÉÓÃÐÔÉè¼Æ¼Æ»®£¬¿ÉÒÔͨ¹ý¸ºÔØƽºâ»úÖÆ£¬½«ÇëÇó·ÖÅɵ½²î±ðµÄÀú³Ì»òЧÀÍÆ÷ÉÏ´¦Öóͷ££¬´Ó¶øÌá¸ßÓ¦ÓóÌÐòµÄ¿ÉÓÃÐÔ¡£SwooleÌṩÁ˶àÖÖ¸ºÔØƽºâ»úÖÆ£¬°üÀ¨ÂÖѯ¡¢IP¹þÏ£¡¢È¨Öص÷Àí¡¢×îÉÙÅþÁ¬µÈ·½·¨£¬¿ÉÒÔƾ֤ӦÓÃÐèÇóÑ¡ÔñÊʵ±µÄ¸ºÔØƽºâ»úÖÆ¡£ÀýÈçÏÂÃæÕâ¶Î´úÂ룺
<?php $server = new SwooleHttpServer('0.0.0.0', 9501); // ÉèÖøºÔØƽºâ»úÖÆ $server->set([ 'worker_num' => 4, 'dispatch_mode' => 3, ]); $server->on('request', function ($request, $response) { $response->header('Content-Type', 'text/plain'); $response->end("Hello World from Swoole "); }); $server->start();
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬Í¨¹ýÉèÖÃdispatch_modeΪ3£¬Ê¹ÓÃIP¹þÏ£µÄ·½·¨¾ÙÐиºÔØƽºâ£¬½«ÇëÇó·ÖÅɵ½²î±ðµÄWorkerÀú³ÌÖд¦Öóͷ££¬´Ó¶øÌá¸ßÁËÓ¦ÓóÌÐòµÄ¿ÉÓÃÐÔ¡£
¼Æ»®Èý£º¼à¿Ø±¨¾¯»úÖÆ
³ýÁ˶àÀú³ÌÄ£×Ӻ͸ºÔØƽºâ»úÖÆÖ®Í⣬¼à¿Ø±¨¾¯»úÖÆÒ²ÊÇÒ»ÖÖÖ÷ÒªµÄ¸ß¿ÉÓÃÐÔÉè¼Æ¼Æ»®¡£Í¨¹ý¼à¿ØSwooleÓ¦ÓóÌÐòµÄÔËÐÐ״̬£¬¿ÉÒÔʵʱ·¢Ã÷ÎÊÌ⣬²¢Í¨¹ý±¨¾¯»úÖƾÙÐÐÌáÐÑ£¬´Ó¶ø×èÖ¹ÎÊÌâÀ©´ó»¯Ó°ÏìϵͳµÄÎȹÌÐÔ¡£³£¼ûµÄ¼à¿ØÖ¸±ê°üÀ¨CPUÕ¼ÓÃÂÊ¡¢ÄÚ´æʹÓÃÇéÐΡ¢ÍøÂçÁ÷Á¿¡¢ÇëÇóÏìӦʱ¼äµÈ£¬¿ÉÒÔͨ¹ýSwoole×Ô´øµÄ¼à¿ØÄ£¿éswoole_server_statusÀ´»ñÈ¡¡£
<?php $server = new SwooleHttpServer('0.0.0.0', 9501); $server->on('request', function ($request, $response) { $response->header('Content-Type', 'text/plain'); $response->end("Hello World from Swoole "); }); // Ìí¼Ó״̬¼à¿Ø $server->on('ManagerStart', function () use ($server) { SwooleTimer::tick(1000, function () use ($server) { echo $server->stats() . PHP_EOL; }); }); $server->start();
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬Æô¶¯SwooleЧÀÍÆ÷²¢Ìí¼Ó״̬¼à¿Ø£¬×¼Ê±Êä³öÄ¿½ñЧÀÍÆ÷µÄ״̬ÐÅÏ¢£¬°üÀ¨ÅþÁ¬Êý¡¢ÇëÇó´ÎÊý¡¢¸÷Àú³ÌµÄCPUºÍÄÚ´æµÈÇéÐΡ£
½áÓï
±¾ÎÄÏÈÈÝÁËSwooleÓ¦ÓóÌÐòµÄ¶¯Ì¬À©Èݺ͸߿ÉÓÃÐÔÉè¼Æ¼Æ»®£¬Õâ¹ØÓÚ¹¹½¨Ò»¸öÎȹ̺͸ßÐÔÄܵÄSwooleÓ¦ÓóÌÐòºÜÊÇÖ÷Òª¡£Ï£Íûͨ¹ý±¾ÎĵÄÏÈÈÝ£¬Äܹ»×ÊÖú¶ÁÕßÉîÈëÏàʶSwoole½ø½×¼¼ÇÉ£¬¹¹½¨Ô½·¢¾«²ÊµÄ»¥ÁªÍøÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇSwoole½ø½×£º¶¯Ì¬À©ÈÝÓë¸ß¿ÉÓÃÐÔÉè¼ÆµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡