×ðÁú¿­Ê±

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½ø½×£º¶¯Ì¬À©ÈÝÓë¸ß¿ÉÓÃÐÔÉè¼ÆµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

13452372176

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿