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ʵÏÖÒ첽ЧÀͼܹ¹Éè¼ÆÓëÓ¦ÓÃʵ¼ùµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡