г̱à³ÌÓëSwooleÊÃ÷ÈÕ½£ºÊµÏָ߲¢·¢½Ó¿ÚÉè¼Æ
Ëæ×Å»¥ÁªÍøÓ¦ÓõÄÆÕ¼°£¬Ô½À´Ô½¶àµÄÓ¦ÓÃÐèÒªÃæÁٸ߲¢·¢µÄÌôÕ½¡£¹Å°åµÄÏ̳߳ػòÀú³Ì³Ø·½·¨ÒѾ²»¿ÉÖª×ãÕâÖÖÇéÐÎϵÄÐèÇó¡£Ð³Ì±à³ÌÊÖÒÕ³ÉΪÁËÒ»ÖÖ½â¾ö¸ß²¢·¢ÎÊÌâµÄÓÐÓ÷½·¨£¬¶øswooleÔòÊÇÏÖÔÚÓ¦ÓÃ×îÆÕ±éµÄг̿ò¼ÜÖ®Ò»¡£
±¾ÎĽ«ÏÈÈÝг̱à³ÌµÄ»ù±¾¿´·¨ºÍÔÀí£¬ÒÔ¼°ÔõÑùʹÓÃSwoole¿ò¼Ü¾ÙÐи߲¢·¢½Ó¿ÚÉè¼Æ¡£ÎÒÃǽ«ÒÔÒ»¸ö¼òÆÓµÄWebЧÀÍΪÀý£¬·Ö°ì·¨ÏÈÈÝÔõÑùʹÓÃг̺ÍSwooleʵÏָ߲¢·¢µÄ½Ó¿ÚÉè¼Æ¡£
Ò»¡¢Ð³Ì±à³Ì¼ò½é
гÌÊÇÖ¸Ò»ÖÖ»ùÓÚÓû§Ì¬µÄÇáÁ¿¼¶Ị̈߳¬ÔÚÀú³Ì»òÏß³ÌÖÐʵÏÖµÄÒ»ÖÖÐ×÷ʽ¶àʹÃü´¦Àí·½·¨¡£ÓëÏß³ÌÏà±È£¬Ð³ÌµÄ×ÊÔ´ÏûºÄ¸üÉÙ£¬Çл»ÉÏÏÂÎĵļÛÇ®¸üС¡£Í¨¹ýʹÓÃг̣¬¿ÉÒÔ¸üºÃµØʹÓÃ×ÊÔ´£¬Ìá¸ß³ÌÐòµÄÔËÐÐЧÂÊ¡£
г̱à³ÌµÄ»ùÀ´Ô´ÀíÊÇ£¬ÔËÐÐÔÚͳһ¸öÏß³ÌÄڵĶà¸öгÌÖ®¼ä²¢·¢Ö´ÐУ¬Í¨¹ýг̵ĹÒÆðºÍ»Ö¸´»úÖÆʵÏÖ´úÂëµÄÁ÷³Ì¿ØÖÆ¡£Ð³ÌÖ®¼äµÄÇл»²»ÐèÒª½øÈëÄÚºË̬£¬¶øÊÇÔÚÓû§Ì¬Íê³É£¬Òò´ËÇл»·Ç³£¿ìËÙ£¬Äܹ»Öª×ã¸ß²¢·¢µÄÐèÇó¡£
¶þ¡¢Swoole¼ò½é
SwooleÊÇÒ»¿î»ùÓÚг̵ÄÍøÂçͨѶ¿ò¼Ü£¬ËüÌṩÁ˶ÔTCP/UDP/WebSocketµÈÐÒéµÄÖ§³Ö£¬²¢ÌṩÁ˶àÖÖÒì²½±à³ÌÄ£×Ó£¬Èçг̡¢Òì²½IOµÈ£¬Äܹ»Öª×ãÖÖÖָ߲¢·¢³¡¾°µÄÐèÇó¡£
SwooleµÄÖ÷ÒªÌصã°üÀ¨ÒÔϼ¸µã£º
»ùÓÚг̵ÄÍøÂçͨѶģ×Ó£¬ÎÞÐ轨Éè´ó×ÚµÄÏ̺߳ÍÀú³Ì£¬Äܹ»¸üºÃµØʹÓÃ×ÊÔ´¡£
ÌṩÁË»ùÓÚÒì²½±à³ÌÄ£×ӵĶàÖÖAPI£¬ÈçÒì²½MySQL¡¢RedisµÈ¡£
Ö§³Ö¶àÀú³Ìģʽ£¬Äܹ»³ä·ÖʹÓöàºËCPUµÄÓÅÊÆ¡£
ÌṩÁ˶àÖָ߲¢·¢½â¾ö¼Æ»®£¬ÈçTCP³¤ÅþÁ¬¡¢ÅþÁ¬³ØµÈ¡£
ÄÚÖÃÁËHTTPЧÀÍÆ÷£¬¿ÉÖ±½ÓÓÃÓÚWeb¿ª·¢¡£
Èý¡¢½Ó¿ÚÉè¼ÆÓëʵÏÖ
¼ÙÉèÎÒÃÇÓÐÒ»¸öÐèÒª´¦Àí´ó×ÚHTTPÇëÇóµÄ½Ó¿Ú£¬ÎÒÃÇÏ£ÍûÄܹ»ÔÚ´¦ÀíÇëÇóʱʵÏָ߲¢·¢ºÍÐÔÄܵÄÌáÉý¡£½ÓÏÂÀ´£¬ÎÒÃÇÒÔ´ËΪÀý£¬·Ö°ì·¨ÏÈÈÝÔõÑùʹÓÃг̺ÍSwooleʵÏָ߲¢·¢µÄ½Ó¿ÚÉè¼Æ¡£
½¨ÉèHTTPЧÀÍÆ÷
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öHTTPЧÀÍÆ÷£¬ÒÔÎüÊÕ¿Í»§¶ËµÄHTTPÇëÇó¡£Ê¹ÓÃSwoole¿ò¼Ü¿ÉÒÔºÜÊÇÀû±ãµØʵÏÖÈçÏ´úÂ룺
$http = new swoole_http_server("0.0.0.0", 9501); $http->on('request', function ($request, $response) { $response->header("Content-Type", "text/plain"); $response->end("Hello World "); }); $http->start();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öHTTPЧÀÍÆ÷£¬²¢¼àÌý9501¶Ë¿Ú¡£µ±ÎüÊÕµ½¿Í»§¶ËÇëÇóʱ£¬»áÖ´ÐÐonRequest»Øµ÷º¯Êý£¬²¢·¢ËÍÒ»¸ö”Hello World”µÄÏìÓ¦ÐÂÎÅ¡£
Ìí¼ÓгÌÖ§³Ö
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª¸øHTTPЧÀÍÆ÷Ìí¼ÓгÌÖ§³Ö¡£ÔÚSwooleÖУ¬¿ÉÒÔʹÓÃг̿ͻ§¶ËÀ´Ìæ»»¹Å°åµÄͬ²½¿Í»§¶Ë£¬´Ó¶øʵÏÖг̵ÄÒì²½±à³Ì¡£
$http = new swoole_http_server("0.0.0.0", 9501); $http->on('request', function ($request, $response) { $redis = new SwooleCoroutineRedis(); $mysql = new SwooleCoroutineMySQL(); $redis->connect('127.0.0.1', 6379); $mysql->connect([ 'host' => '127.0.0.1', 'user' => 'root', 'password' => '', 'database' => 'test', ]); $redis->set('key', 'value'); $mysql->query("SELECT * FROM `table` WHERE `id` = 1"); $response->header("Content-Type", "text/plain"); $response->end("Hello World "); }); $http->set([ 'enable_coroutine' => true, ]); $http->start();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÌí¼ÓÁËRedisºÍMySQLµÄг̿ͻ§¶Ë£¬²¢ÔÚÇëÇó´¦ÀíÖÐʹÓÃÕâЩг̿ͻ§¶Ë¡£ÔÚÆô¶¯HTTPЧÀÍÆ÷µÄʱ¼ä£¬ÎÒÃÇÐèÒªÉèÖÃenable_coroutineÑ¡ÏîΪtrueÀ´¿ªÆôгÌÖ§³Ö¡£
Ìí¼ÓÅþÁ¬³ØÖ§³Ö
ΪÁ˸üºÃµØÖÎÀíÅþÁ¬£¬ÎÒÃÇ¿ÉÒÔʹÓÃÅþÁ¬³ØÊÖÒÕÀ´Ìá¸ß×ÊÔ´µÄʹÓÃÂʺÍÐÔÄÜÌåÏÖ¡£SwooleÄÚÖÃÁ˶àÖÖÅþÁ¬³ØµÄÖ§³Ö£¬ÈçMySQLºÍRedisÅþÁ¬³Ø¡£ÏÂÃæÊÇÒ»¸öʹÓÃSwooleÄÚÖõÄMySQLÅþÁ¬³ØµÄʾÀý´úÂ룺
$http = new swoole_http_server("0.0.0.0", 9501); $http->on('request', function ($request, $response) { $pool = SwooleDatabasePDOPool::class; $options = [ 'dsn' => 'mysql:host=127.0.0.1;dbname=test', 'username' => 'root', 'password' => '', 'charset' => 'utf8mb4', ]; /** @var SwooleDatabasePDOProxy $db */ $db = SwooleDatabase::getInstance($pool)->getConnection(); $db->query("SELECT * FROM `table` WHERE `id` = 1"); $db->close(); $response->header("Content-Type", "text/plain"); $response->end("Hello World "); }); $http->start();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËSwooleÄÚÖõÄMySQLÅþÁ¬³Ø£¬²¢Í¨¹ýgetInstanceÒªÁì»ñÈ¡µ½Ò»¸öÅþÁ¬¡£ÔÚʹÓÃÍê±Ïºó£¬ÎÒÃÇÐèÒªÊÖ¶¯¹Ø±Õ¸ÃÅþÁ¬¡£ÅþÁ¬³ØµÄʹÓÿÉÒÔÓÐÓõØïÔÌÅþÁ¬½¨ÉèºÍÏú»ÙµÄ¿ªÏú£¬´Ó¶øÌá¸ßÓ¦ÓÃÐÔÄÜ¡£
ËÄ¡¢×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËг̱à³ÌºÍSwoole¿ò¼Ü£¬²¢Í¨¹ýÒ»¸ö¼òÆÓµÄWebЧÀ͵ÄʾÀý£¬ÐðÊöÁËÔõÑùʹÓÃг̱à³ÌºÍSwoole¿ò¼ÜʵÏָ߲¢·¢½Ó¿ÚÉè¼Æ¡£
г̱à³ÌÊÇÒ»ÖÖ¸ßЧµÄ±à³Ì·½·¨£¬Äܹ»ÓÐÓõØÌá¸ßÓ¦ÓõÄÐÔÄܺÍÍÌÍÂÁ¿¡£SwooleÔòÊÇÏÖÔÚ½ÏÁ¿Ê¢ÐеÄг̿ò¼Ü£¬ÌṩÁ˶àÖÖÒì²½±à³ÌÄ£×Ӻ͸߲¢·¢½â¾ö¼Æ»®£¬Äܹ»Öª×ãÖÖÖָ߲¢·¢³¡¾°µÄÐèÇó¡£
¹ØÓÚÐèÒª´¦Àí´ó×ÚÇëÇóµÄÓ¦Óã¬Ê¹ÓÃг̱à³ÌºÍSwoole¿ò¼Ü¿ÉÒÔ×ÊÖúÎÒÃǸüºÃµØ½â¾ö¸ß²¢·¢ÎÊÌ⣬Ìá¸ßÓ¦ÓõÄÐÔÄܺÍÎȹÌÐÔ¡£
ÒÔÉϾÍÊÇг̱à³ÌÓëSwooleÊÃ÷ÈÕ½£ºÊµÏָ߲¢·¢½Ó¿ÚÉè¼ÆµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡