×ðÁú¿­Ê±¹ÙÍøµÇ¼

SwooleʵÏÖÒ첽ЧÀͼܹ¹Éè¼ÆÓëÓ¦ÓÃʵ¼ù

Ëæ×Å»¥ÁªÍøʱ´úµÄÉú³¤£¬¹ØÓÚÓ¦ÓõÄÐÔÄܺͿÉÀ©Õ¹ÐÔÒªÇóÔ½À´Ô½¸ß£¬¹Å°åµÄͬ²½²¢·¢Ä£×ÓÃæÁÙ×ÅÆ¿¾±¡£¶øÒì²½±à³ÌÍ·ÄÔÊÇÌá¸ßÐÔÄܺͿÉÀ©Õ¹ÐÔµÄÓÐÓÃÊÖ¶ÎÖ®Ò»£¬ÆäÖÐswooleÊÇÒ»¿îÇ¿Ê¢µÄÒì²½ÍøÂçͨѶ¿ò¼Ü¡£±¾ÎĽ«ÏÈÈÝswooleµÄ»ù±¾¿´·¨ºÍÓ¦ÓÃʵ¼ù£¬Ì½ÌÖÔõÑùʹÓÃswooleʵÏÖÒ첽ЧÀͼܹ¹¡£

Ò»¡¢Swoole»ù±¾¿´·¨

SwooleÊÇÒ»¿î»ùÓÚC++ʵÏÖµÄPHPÍøÂçͨѶ¿â£¬Äܹ»ÊµÏÖPHPÀú³ÌºÍÔ­ÉúTCP¡¢UDP¡¢Unix SocketµÈЭÒéÖ®¼äµÄ¸ßЧͨѶ¡£SwooleµÄ»ù±¾¿´·¨ÈçÏ£º

Э³Ì£ºÐ­³ÌÊÇÒ»ÖÖÇáÁ¿¼¶Ị̈߳¬Ïà±ÈÓڹŰåÏ̶߳øÑÔ£¬Ð­³ÌÇл»¿ªÏúС¡¢²¢·¢Á¿´ó£¬¹ØÓڸ߲¢·¢Ó¦ÓÃÓÐ×ÅÖ÷ÒªÒâÒå¡£

Òì²½I/O£ºÒì²½I/OÊÇSwooleʵÏÖÒì²½±à³ÌµÄÖ÷ÒªÊֶΣ¬²î±ðÓڹŰåI/OÄ£×ÓµÄÛձպͷÇÛÕ±Õ£¬Òì²½I/O×èÖ¹ÁËÏß³ÌÆÚ´ý£¬³ä·ÖʹÓÃCPUºÍIOµÄ²¢ÐÐÐÔÄÜ¡£

ÊÂÎñÇý¶¯£ºSwoole»ùÓÚÊÂÎñÇý¶¯µÄIOʵÏÖ£¬Í¨¹ý½«ÅþÁ¬ÊÂÎñ¡¢¶ÁдÊÂÎñµÈ¹ØÁªµ½²î±ðµÄº¯ÊýÉÏ£¬ÊµÏÖ²î±ðÊÂÎñµÄ´¦Àí£¬Ìá¸ßÁËÓ¦ÓóÌÐòµÄ¿ÉÀ©Õ¹ÐÔºÍÐÔÄÜ¡£

¶þ¡¢SwooleÓ¦ÓÃʵ¼ù

Òì²½HTTPЧÀÍÆ÷

SwooleÌṩÁËswoole_http_serverÀ࣬¿ÉÒÔÖ±½ÓʵÏÖÒì²½HTTPЧÀÍÆ÷¡£ÔÚÒ첽ЧÀÍÆ÷ÉÏ£¬Ã¿¸ö¿Í»§¶ËÅþÁ¬¶¼»á¶ÔÓ¦Ò»¸öЭ³Ì£¬¿ÉÒÔ³ä·ÖʹÓÃCPU×ÊÔ´£¬Ìá¸ß²¢·¢´¦ÀíÄÜÁ¦¡£ÒÔÏÂÊÇSwooleʵÏÖHTTPЧÀÍÆ÷µÄʾÀý´úÂ룺

// ½¨ÉèHTTPЧÀÍÆ÷
$http = new swoole_http_server("0.0.0.0", 9503);
// ´¦ÀíÇëÇó
$http->on('request', function ($request, $response) {
    var_dump($request);
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World
");
});

// Æô¶¯HTTPЧÀÍÆ÷
$http->start();

µÇ¼ºó¸´ÖÆ

Òì²½MySQL¿Í»§¶Ë

SwooleÌṩÁËÒì²½MySQL¿Í»§¶ËÀàswoole_mysql£¬Ö§³ÖMySQLÅÌÎÊ¡¢²åÈëµÈ²Ù×÷¡£Í¨¹ýЭ³ÌÊÖÒÕ£¬ÊµÏÖ¶à¸ö¿Í»§¶ËÇëÇóµÄ²¢·¢´¦Àí¡£

ÒÔÏÂÊÇSwooleʵÏÖÒì²½MySQLÅÌÎʵÄʾÀý´úÂ룺

// ½¨ÉèMySQLÅþÁ¬
$mysql = new SwooleCoroutineMySQL();
$mysql->connect([
    'host' => '127.0.0.1',
    'port' => 3306,
    'user' => 'root',
    'password' => 'password',
    'database' => 'test',
]);

// Ö´ÐÐMySQLÅÌÎÊ
$result = $mysql->query('SELECT * FROM `user` WHERE `id` = 1');

// Êä³öÅÌÎÊЧ¹û
var_dump($result->fetch());

µÇ¼ºó¸´ÖÆ

WebSocketЧÀÍÆ÷

SwooleÌṩÁËÒì²½WebSocketЧÀÍÆ÷Ààswoole_websocket_server£¬¿ÉÒÔ¿ìËÙʵÏÖWebSocketÓ¦ÓóÌÐò¡£ÓëHTTPЧÀÍÆ÷ÀàËÆ£¬WebSocketЧÀÍÆ÷Ò²»áΪÿ¸ö¿Í»§¶ËÅþÁ¬½¨ÉèÒ»¸öЭ³Ì£¬ÊµÏָ߲¢·¢´¦Àí¡£

ÒÔÏÂÊÇSwooleʵÏÖÒì²½WebSocketЧÀÍÆ÷µÄʾÀý´úÂ룺

// ½¨ÉèWebSocketЧÀÍÆ÷
$ws = new swoole_websocket_server("0.0.0.0", 9502);

// ¼àÌýWebSocketÅþÁ¬ÊÂÎñ
$ws->on('open', function ($ws, $request) {
    var_dump($request->fd, $request->get, $request->server);
    $ws->push($request->fd, "Hello, welcome
");
});

// ¼àÌýWebSocketÐÂÎÅÊÂÎñ
$ws->on('message', function ($ws, $frame) {
    echo "Message: {$frame->data}
";
    $ws->push($frame->fd, "server: {$frame->data}");
});

// ¼àÌýWebSocket¹Ø±ÕÊÂÎñ
$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed
";
});

// Æô¶¯WebSocketЧÀÍÆ÷
$ws->start();

µÇ¼ºó¸´ÖÆ

Èý¡¢SwooleʵÏÖÒ첽ЧÀͼܹ¹

ÔÚÒ첽ЧÀͼܹ¹Éè¼ÆÖУ¬Ã¿ÌõÇëÇó²»»áÛÕ±ÕÏ̻߳òÀú³Ì£¬¶øÊÇͨ¹ýÒ첽Э³ÌµÄ·½·¨´¦ÀíÇëÇó£¬ÐγÉÒ»ÌõÒì²½µÄ´¦ÀíÁ÷³Ì¡£Swoole×÷ΪÒì²½ÍøÂç¿â£¬ºÜÊÇÊÊÓÃÓÚ¹¹½¨¸ßÐÔÄÜ¡¢¸ß²¢·¢µÄÒ첽ЧÀͼܹ¹¡£

Ò첽ЧÀͼܹ¹µÄÉè¼ÆÔ­Ôòͨ³£ÓÐÒÔϼ¸µã£º

ÍøÂçÒì²½£ºÍøÂçI/OʹÓÃÒ첽Э³Ì·½·¨£¬±ÜÃâÍøÂçÆÚ´ýÛÕ±ÕÏ̻߳òÀú³Ì¡£

CPUÒì²½£ºCPU÷缯ÐͲÙ×÷ʹÓÃЭ³Ì·½·¨¡£

Êý¾ÝÒì²½£ºÊý¾ÝÒì²½¿ÉÒÔͨ¹ýʹÓÃÐÂÎÅÐÐÁС¢»º´æµÈÊÖÒÕʵÏÖ¡£

À©ÈÝÒì²½£ºÓ¦ÓóÌÐòµÄÀ©ÈÝͨ¹ýÂþÑÜʽ¡¢¼¯ÈºµÈ·½·¨ÊµÏÖ¡£

ʹÓÃSwooleʵÏÖÒ첽ЧÀͼܹ¹£¬¿ÉÒÔ³ä·ÖʹÓÃCPU¡¢ÍøÂçI/OµÈ×ÊÔ´£¬Ìá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺÍЧÂÊ¡£ÒÔÏÂÊÇSwooleʵÏÖÒ첽ЧÀͼܹ¹µÄʾÀý´úÂ룺

// Òì²½MySQLÅÌÎÊ
$result = $mysql->query('SELECT * FROM `user` WHERE `id` = 1');
var_dump($result->fetch());

// Òì²½HTTPÇëÇó
$client = new SwooleCoroutineHttpClient('127.0.0.1', 80);
$client->post('/path', ['data' => 'test']);
var_dump($client->statusCode, $client->body);

// Òì²½RedisÅÌÎÊ
$redis = new SwooleCoroutineRedis();
$redis->connect('127.0.0.1', 6379);
$redis->set('key', 'value');
var_dump($redis->get('key'));

µÇ¼ºó¸´ÖÆ

ËÄ¡¢×ܽá

Swoole×÷Ϊһ¿îÇ¿Ê¢µÄÒì²½ÍøÂçͨѶ¿ò¼Ü£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖ¸ßÐÔÄÜ¡¢¸ß²¢·¢µÄÓ¦ÓóÌÐò¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇ¿ÉÒÔ½èÖúSwooleʵÏÖÒì²½HTTPЧÀÍÆ÷¡¢MySQL¿Í»§¶Ë¡¢WebSocketЧÀÍÆ÷µÈÓ¦Óã¬Ò²¿ÉÒÔʹÓÃSwooleʵÏÖÒ첽ЧÀͼܹ¹£¬ÒÔÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺÍЧÂÊ¡£Î´À´£¬ÎÒÃÇÐÅÍÐSwoole»áÔ½À´Ô½³ÉÊ죬Ҳ»á¸øPHPÓ¦ÓóÌÐòµÄÐÔÄÜ´øÀ´¸ü´óµÄÌáÉý¡£

ÒÔÉϾÍÊÇSwooleʵÏÖÒ첽ЧÀͼܹ¹Éè¼ÆÓëÓ¦ÓÃʵ¼ùµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ