Swoole³£¼ûÎÊÌâ½â¾öÓë×î¼Ñʵ¼ùÂÄÀú·ÖÏí
Ëæ×Å»¥ÁªÍøʱ´úµÄÉú³¤£¬webÓ¦ÓõÄÐÔÄܺÍÎȹÌÐÔÔ½À´Ô½±»ÖØÊÓ¡£swoole×÷ΪphpÓïÑԵĸßÐÔÄÜÍøÂçͨѶ¿â£¬ËüµÄ·ºÆð½â¾öÁËphpÓïÑÔÔڸ߲¢·¢³¡¾°ÏÂÐÔÄܺÍÎȹÌÐÔµÄÆ¿¾±¡£¿ÉÊÇ£¬ÔÚswooleµÄ¿ª·¢ºÍʹÓÃÖУ¬Ò²»áÓöµ½Ò»Ð©³£¼ûµÄÎÊÌâ¡£±¾ÎĽ«·ÖÏíswooleµÄ³£¼ûÎÊÌâ½â¾öÓë×î¼Ñʵ¼ùÂÄÀú£¬×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍʹÓÃswoole¡£
Ò»¡¢SwooleµÄ°²ÅÅÎÊÌâ
1.1 Swoole×÷ΪPHPµÄÀ©Õ¹°ü¿ª·¢Ê±£¬ÔõÑù×°Öã¿
½«ÍâµØPHPºÍSwooleµÄÀ©Õ¹°üÏÂÔØÏÂÀ´²¢Í¨¹ýPHPµÄ¹Ù·½ÏÂÁîÐй¤¾ß¾ÙÐбàÒëºÍ×°Öü´¿É¡£ÏêϸÀú³ÌºÍÒªÁì¿ÉÒԲο¼Swoole¹Ù·½Îĵµ¡£
1.2 ÔõÑù½«SwooleÔÚLaravel¿ò¼ÜÖÐʹÓã¿
Laravel¿ò¼Ü¼¯³ÉSwooleÊÇͨ¹ý×°ÖÃlaravel-swooleÀ©Õ¹ÊµÏֵģ¬¸ÃÀ©Õ¹¿ÉÒÔÖ±½Óͨ¹ýComposer¾ÙÐÐ×°Öá£
1.3 ÔõÑùÔÚdockerÖа²ÅÅSwoole£¿
¿ÉÒÔʹÓÃDocker ComposeÒ»¼ü°²ÅÅSwoole£¬ÐèÒª±àдÏìÓ¦µÄDockerfileºÍdocker-compose.ymlÎļþ£¬Ïêϸ²Ù×÷¿ÉÒԲο¼Swoole¹Ù·½Îĵµ¡£
¶þ¡¢SwooleµÄ´úÂ뿪·¢ÎÊÌâ
2.1 ÔõÑù׼ȷʹÓÃSwooleÖеÄг̣¿
ʹÓÃSwooleÖеÄгÌʱ£¬ÐèÒª×èÖ¹ÔÚгÌÖÐʹÓÃsleep()µÈÛÕ±Õ´úÂ룬²»È»½«»áÔì³ÉÕû¸öÀú³ÌµÄ¿¨ËÀ¡£¿ÉÒÔʹÓÃSwooleÌṩµÄÒì²½I/O²Ù×÷¡¢Ð³Ì׼ʱÆ÷µÈ¹¦Ð§À´ÊµÏÖ·ÇÛÕ±ÕŲÓá£
2.2 ÈçÄÇÀïÖÃSwooleÖеÄÒì³££¿
ÔÚʹÓÃSwooleʱ£¬Ò»Ð©Òì³£ÇéÐοÉÄܻᵼÖÂÀú³ÌÖÕÖ¹¡£Ê¹ÓÃtry-catchÓï¾ä¿ÉÒÔ²¶»ñµ½ÕâЩÒì³££¬×èÖ¹Àú³ÌÒì³£Í˳ö¡£½¨Ò齫ËùÓпÉÄܱ¬·¢Òì³£µÄ´úÂë¿é¶¼°ü¹üÔÚtry-catchÓï¾äÖС£
Èý¡¢SwooleµÄÐÔÄÜÓÅ»¯ÎÊÌâ
3.1 ÔõÑù׼ȷÉèÖÃSwooleµÄWorkerÀú³ÌÊý£¿
SwooleµÄWorkerÀú³ÌÊýÐèҪƾ֤ЧÀÍÆ÷µÄÓ²¼þÉèÖúÍÏÖʵӪҵ¸ºÔØÇéÐÎÀ´¾ÙÐе÷½â¡£Ò»Ñùƽ³£ÇéÐÎÏ£¬ÍƼö½«WorkerÀú³ÌÊýÉèÖÃΪCPU½¹µãÊýµÄ1-2±¶¡£
3.2 ÈçÄÇÀïÖÃSwooleµÄÄÚ´æÎÊÌ⣿
SwooleÔËÐÐÀú³ÌÖУ¬ÐèÒª·ÖÅÉÐí¶àµÄÄÚ´æ¿Õ¼ä£¬ÈôÊÇÄÚ´æδÄÜʵʱÊÍ·Å£¬»áÔì³ÉÄÚ´æÒç³öµÈÎÊÌâ¡£½¨ÒéÔÚ´úÂëÖÐÏÔʽµØÊÍ·ÅÄڴ棬ʹÓÃunset()º¯ÊýÀ´ÊÖ¶¯Ïú»Ù±äÁ¿¡£
3.3 ÔõÑù·¿ªSwooleµÄTCP_NODELAYÑ¡Ï
ʹÓÃTCPÅþÁ¬Ê±£¬SwooleĬÈÏ¿ªÆôÁËNagleËã·¨£¬µ¼ÖÂÊý¾Ý·¢Ë͵ÄЧÂʽϵ͡£¿ÉÒÔͨ¹ýset()ÒªÁìÉèÖÃTCP_NODELAYÑ¡ÏîΪtrue£¬¹Ø±ÕNagleËã·¨£¬Ìá¸ßÊý¾Ý·¢ËÍЧÂÊ¡£
ËÄ¡¢SwooleµÄ×î¼Ñʵ¼ùÂÄÀú
4.1 ½¨ÒéʹÓÃSwooleÌṩµÄÒì²½MySQLÅþÁ¬¹¦Ð§
SwooleÌṩµÄÒì²½MySQLÅþÁ¬¹¦Ð§¿ÉÒÔ×èÖ¹´ó×ÚµÄMySQLÅþÁ¬Ôì³ÉµÄÐÔÄÜÎÊÌ⣬ͬʱҲÄܹ»Ìá¸ßÅÌÎÊЧÂÊ¡£½¨ÒéÔÚ¿ª·¢ÖÐʹÓÃÒì²½MySQLÅþÁ¬£¬¿ÉÒÔÓÐÓÃÌáÉý³ÌÐòµÄÐÔÄÜ¡£
4.2 ½¨ÒéʹÓÃSwooleÌṩµÄTask¹¦Ð§
SwooleÌṩµÄTask¹¦Ð§¿ÉÒÔʵÏÖÒ첽ʹÃü´¦Àí£¬½«Ò»Ð©ÐèÒª³¤Ê±¼äÔËÐеÄʹÃü½»¸øTaskÀú³ÌÀ´´¦Àí£¬¿ÉÒÔ×èÖ¹ÛÕ±ÕWorkerÀú³Ì¡£ÌØÊâÊÇÔÚ´¦Àí´ó×ÚI/O²Ù×÷µÄ³¡¾°ÖУ¬Ê¹ÓÃTask¿ÉÒÔ´ó´óÌá¸ß³ÌÐòµÄÐÔÄÜ¡£
4.3 ½¨ÒéʹÓÃSwoole×Ô´øµÄHTTPЧÀÍÆ÷
Swoole×Ô´øµÄHTTPЧÀÍÆ÷ÐÔÄܽϸߣ¬¿ÉÒÔÌ滻ͨÀýµÄPHP-FPM·½·¨À´´¦ÀíHTTPÇëÇó¡£ÔÚʹÓÃSwoole×Ô´øµÄHTTPЧÀÍÆ÷ʱ£¬ÐèÒªÎÞаÉèÖÃÏà¹ØÑ¡Ïî²ÎÊý£¬Äܹ»¸üºÃµØ˳ӦÏÖʵӪҵ³¡¾°¡£
×ܽá
SwooleÊÇPHPÓïÑԵĸßÐÔÄÜÍøÂçͨѶ¿â£¬ÔÚ´ó¹æÄ£¸ß²¢·¢µÄÓªÒµ³¡¾°Ï¾ßÓо«²ÊµÄÌåÏÖ¡£È»¶ø£¬ÔÚSwooleµÄ¿ª·¢ºÍʹÓÃÖУ¬Ò²»áÓöµ½ÖÖÖÖÎÊÌâ¡£±¾ÎľÍSwooleµÄ°²ÅÅ¡¢´úÂ뿪·¢¡¢ÐÔÄÜÓÅ»¯ÒÔ¼°×î¼Ñʵ¼ùµÈ·½Ãæ¾ÙÐÐÁË×ܽáºÍ·ÖÏí£¬Ï£Íû¶Ô¶ÁÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇSwoole³£¼ûÎÊÌâ½â¾öÓë×î¼Ñʵ¼ùÂÄÀú·ÖÏíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡