Swoole½ø½×£ºÔõÑùʹÓÃг̾ÙÐи߲¢·¢Êý¾Ý´¦Àí
ÔÚÏÖ´ú»¥ÁªÍøÓ¦ÓÃÖУ¬¸ß²¢·¢ÒѾ³ÉΪÁËÒ»¸ö²»¿É×èÖ¹µÄÎÊÌâ¡£¶øÕë¶Ô¸ß²¢·¢µÄ½â¾ö¼Æ»®ÖУ¬Ð³ÌÊÖÒÕÊÇÒ»¸ö±¸ÊÜÍÆÐíµÄÆ«Ïò¡£ÔÚphpÁìÓòÄÚ£¬swoole¾ÍÊÇÒ»¿îг̿ò¼Ü£¬¿ÉÒÔÓÃÀ´ÊµÏָ߲¢·¢µÄÊý¾Ý´¦Àí¡£
±¾ÎĽ«ÏÈÏÈÈÝSwooleµÄгÌÌØÕ÷£¬È»ºóÕë¶ÔÊý¾Ý´¦ÀíµÄ³¡¾°£¬ÏêϸÐðÊöSwooleÖÐг̵ÄʹÓÃÒªÁì¼°×¢Öص㡣
Ò»¡¢SwooleгÌ
SwooleÊÇÒ»¿î»ùÓÚPHPÓïÑÔµÄг̿ò¼Ü¡£SwooleµÄг̿ÉÒÔÔÚͳһÏß³ÌÄÚʵÏÖ²¢·¢Ö´ÐУ¬×èÖ¹Á˽øÈëÄÚºË̬ºÍÓû§Ì¬µÄƵÈÔÇл»£¬ÌáÉýÁË´¦ÀíЧÂÊ¡£Í¬Ê±£¬ÔÚгÌÄÚ²¿£¬IO²Ù×÷²»ÔÙ¼ûµ¼ÖÂÏ̵߳ÄÛÕ±Õ£¬Ê¹µÃÓ¦ÓÃÄܹ»Ôڸ߲¢·¢Ï¼á³Ö½Ï¸ßµÄÍÌÍÂÁ¿¡£
гÌÊÇÒ»ÖÖ½ÏÁ¿ÌØÊâµÄº¯Êý¡£ÔÚгÌÄÚ£¬¿ÉÒÔÖ´ÐÐÒ»¶Î´úÂ룬Ȼºó½«Ö´ÐÐȨ½»»Ø¸øŲÓ÷½£¬ÆÚ´ýÏÂÒ»´ÎŲÓÃʱÔÙ¼ÌÐøÖ´ÐС£Ò²¾ÍÊÇ˵£¬Ð³ÌÄÚ²¿¿ÉÒÔʵÏÖÔÝÍ£ºÍ»Ö¸´£¬ÕâʹµÃг̿ÉÒÔÓÃÀ´ÊµÏÖÒì²½±à³Ì¡£
¶þ¡¢ÔõÑùÔÚSwooleÖÐʹÓÃг̾ÙÐи߲¢·¢Êý¾Ý´¦Àí
Êý¾Ý¿â²Ù×÷
ÔڹŰåµÄPHPÓ¦ÓÃÖУ¬¾ÙÐÐÊý¾Ý¿â²Ù×÷ʱ£¬ÎÒÃǾ³£Ê¹ÓõÄÊÇͬ²½µÄ·½·¨£¬¼´·¢ËÍÒ»¸öÇëÇóºóÐèÒªÆÚ´ýÏìÓ¦²Å»ª¾ÙÐÐÏÂÒ»ÐдúÂëµÄÖ´ÐС£¶øÔÚSwooleÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃгÌÖеÄÒì²½·½·¨£¬À´ÊµÏָ߲¢·¢µÄÊý¾Ý´¦Àí¡£
ÒÔÏÂÊÇSwooleÖÐʹÓÃг̾ÙÐÐÊý¾Ý¿â²Ù×÷µÄʾÀý´úÂ룺
// ½¨ÉèMySQLг̿ͻ§¶Ë $db = new SwooleCoroutineMySQL(); // ÅþÁ¬MySQLЧÀÍÆ÷ $db->connect([ 'host' => '127.0.0.1', 'user' => 'root', 'password' => 'root', 'database' => 'test', ]); // ÔÚгÌÖÐÖ´ÐÐÅÌÎʲÙ×÷ SwooleCoroutineun(function() use($db){ $result = $db->query('select * from users'); // ´¦ÀíÅÌÎÊЧ¹û });
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈͨ¹ý SwooleCoroutineMySQL() ½¨ÉèÁËÒ»¸öMySQLг̿ͻ§¶Ë£¬²¢Ê¹Óà connect() ÒªÁìÅþÁ¬µ½MySQLЧÀÍÆ÷¡£È»ºó£¬ÎÒÃÇÔÚ SwooleCoroutineun() гÌÖУ¬Ê¹Óà $db->query() Ö´ÐÐÁËÒ»ÌõÅÌÎÊÓï¾ä£¬²¢Í¨¹ý $result »ñÈ¡ÁËÅÌÎÊЧ¹û¡£
HTTPÇëÇó
¹ØÓÚHTTPÇëÇóµÄ´¦Àí£¬SwooleÖÐÌṩÁËÒ»¸ö SwooleCoroutineHttpClient() ×é¼þ¡£Í¬ÑùµØ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÕâ¸ö×é¼þÀ´ÊµÏÖÒì²½´¦Àí£¬ÒԴ˵ִï¸ß²¢·¢µÄÄ¿µÄ¡£
ÒÔÏÂÊÇʹÓà SwooleCoroutineHttpClient() ¾ÙÐÐHTTPÇëÇóµÄʾÀý´úÂ룺
// ½¨ÉèHTTP¿Í»§¶Ë $client = new SwooleCoroutineHttpClient('www.m.linesum.net', 80); // ÔÚгÌÖÐÖ´ÐÐÇëÇó²Ù×÷ SwooleCoroutineun(function() use($client){ $client->get('/'); // ´¦ÀíÏìÓ¦ });
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öHTTP¿Í»§¶Ë£¬È»ºóÔÚ SwooleCoroutineun() гÌÖУ¬Í¨¹ý $client ÌᳫÁËÒ»´ÎGETÇëÇ󣬲¢Í¨¹ýÏìӦЧ¹û¾ÙÐкóÐø´¦Àí¡£
Redis²Ù×÷
×îºó£¬ÎÒÃÇÀ´ÏÈÈÝÒ»ÏÂʹÓÃг̾ÙÐÐRedis²Ù×÷µÄÒªÁì¡£SwooleÖÐÌṩÁËÒ»¸ö SwooleCoroutineRedis() ×é¼þ£¬¿ÉÓÃÓڸ߲¢·¢µÄRedis²Ù×÷¡£
ÒÔÏÂÊÇSwooleʹÓÃг̾ÙÐÐRedis²Ù×÷µÄʾÀý´úÂ룺
// ½¨ÉèRedisг̿ͻ§¶Ë $redis = new SwooleCoroutineRedis(); // ÅþÁ¬RedisЧÀÍÆ÷ $redis->connect('127.0.0.1', 6379); // ÔÚгÌÖÐÖ´ÐвÙ×÷ SwooleCoroutineun(function() use($redis){ $redis->set('key', 'value'); $result = $redis->get('key'); // ´¦ÀíÅÌÎÊЧ¹û });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀý´úÂëÖУ¬ÎÒÃÇÊ×ÏÈͨ¹ý SwooleCoroutineRedis() ½¨ÉèÁËÒ»¸öRedisг̿ͻ§¶Ë£¬²¢Ê¹Óà connect() ÒªÁìÅþÁ¬µ½RedisЧÀÍÆ÷¡£È»ºó£¬ÎÒÃÇÔÚ SwooleCoroutineun() гÌÖУ¬Ê¹Óà $redis->set() ÒªÁìÉèÖÃÁËÒ»¸ö¼üÖµ¶Ô£¬²¢Í¨¹ý $redis->get() »ñÈ¡Á˸üüÖµ¶Ô£¬×îºóͨ¹ý $result ±äÁ¿»ñÈ¡ÁËÅÌÎÊЧ¹û¡£
Èý¡¢×¢ÖØÊÂÏî
Ö»¹ÜʹÓÃÁËгÌÊÖÒÕ£¬SwooleÖоÙÐи߲¢·¢Êý¾Ý´¦ÀíÈÔÈ»ÐèҪעÖØһЩÊÂÏî¡£
Ê×ÏÈ£¬ÓÉÓÚSwooleÖÐг̹¦Ð§ÊÇÓÉг̵÷ÀíÆ÷À´µ÷ÀíµÄ£¬ÒÔÊÇÎÒÃÇÔÚʹÓÃгÌʱÐèÒª×ñÕÕһЩԼÊø¡£ÀýÈ磬гÌÖв»¿É¾ÙÐÐÛÕ±ÕIO²Ù×÷£¬²»È»»áµ¼ÖÂÏ̵߳ÄÛÕ±Õ£¬½ø¶øÓ°ÏìÕû¸öÓ¦ÓõÄÐÔÄÜ¡£±ðµÄ£¬¹ØÓÚ´ó×ÚCPU÷缯Ð͵IJÙ×÷£¬Ò²ÐèÒªÉóÉ÷ʹÓÃг̣¬ÒÔÃâÕ¼Óùý¶àµÄ×ÊÔ´¡£
Æä´Î£¬¹ØÓÚÊý¾Ý¿â¡¢RedisµÈ³¤ÅþÁ¬Ð§À͵IJÙ×÷£¬ÔÚ¾ÙÐÐÅþÁ¬³ØÖÎÀíʱ£¬ÐèÒªÉèÖÃÒ»¶¨µÄ³¬Ê±Ê±¼ä£¬×èÖ¹ÅþÁ¬¹ý¶à»ò¹ýÉÙ£¬Ó°ÏìÓ¦ÓõÄÎȹÌÐÔºÍÐÔÄÜ¡£
×îºó£¬Swooleг̵ÄʹÓÃÐèҪרÐÄÉóÉ÷¡£ËäȻг̿ÉÒÔÌá¸ßÓ¦ÓõÄÐÔÄÜ£¬µ«ÈôÊÇʹÓò»µ±£¬Ò²»áµ¼Ö±àдµÄ´úÂëÄÑÒÔÃ÷È·ºÍµ÷ÊÔ¡£
ËÄ¡¢×ܽá
±¾ÎÄÏêϸÏÈÈÝÁËÔÚSwoole¿ò¼ÜÖÐÔõÑùʹÓÃгÌÊÖÒÕ¾ÙÐи߲¢·¢Êý¾Ý´¦Àí¡£ÎÒÃÇͨ¹ýʵÀýչʾÁËÔõÑùʹÓÃSwooleÖеÄMySQLг̿ͻ§¶Ë¡¢HTTP¿Í»§¶Ë¡¢Redisг̿ͻ§¶ËµÈ×é¼þ¡£Í¬Ê±£¬Ò²ÏÈÈÝÁËÔõÑùÔÚгÌʹÓÃʱÐèҪעÖصÄÎÊÌâ¡£
ÔÚSwooleµÄÌìÏÂÀгÌÒѾ³ÉΪÁËʵÏָ߲¢·¢Êý¾Ý´¦ÀíµÄÖ÷ÒªÊֶΡ£ÎÒÃÇÐÅÍУ¬Í¨¹ýÕÆÎÕSwooleг̵ÄʹÓÃÒªÁìºÍ×¢ÖØÊÂÏÄú¿ÉÒÔ¸üºÃµØÓ¦¶ÔÓªÒµÖеĸ߲¢·¢ÎÊÌâ¡£
ÒÔÉϾÍÊÇSwoole½ø½×£ºÔõÑùʹÓÃг̾ÙÐи߲¢·¢Êý¾Ý´¦ÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡