Swoole½ø½×£ºÔõÑùʹÓÃгÌʵÏÖ¸ßЧ²¢·¢Êý¾Ý²Ù×÷
Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬ÍøÂçÇëÇó±äµÃÔ½À´Ô½ÆµÈÔºÍÖØ´ó£¬ÔõÑùʵÏÖ¸ßЧµÄ²¢·¢Êý¾Ý²Ù×÷ÒѾ³ÉΪЧÀÍÆ÷¿ª·¢ÕßÃæÁÙµÄÒ»¸öÖ÷ÒªÎÊÌâ¡£ÔڹŰåµÄphp¿ª·¢ÖУ¬ÎªÁËʵÏÖ²¢·¢²Ù×÷£¬ÍùÍùÐèÒª½ÓÄɶàÀú³Ì»ò¶àÏ̵߳ķ½·¨£¬µ«ÕâÖÖ·½·¨±£´æ×ÅÏÔÖøµÄÐÔÄÜÆ¿¾±ºÍ×ÊÔ´ÆÌÕÅ¡£È»¶ø£¬ÔÚʹÓÃswooleгÌÖ®ºó£¬¿ª·¢Õß¿ÉÒÔÇáËɵØʵÏÖ¸ßЧ²¢·¢Êý¾Ý²Ù×÷£¬±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃswooleгÌʵÏÖ¸ßЧ²¢·¢Êý¾Ý²Ù×÷¡£
Ò»¡¢Ê²Ã´ÊÇSwooleгÌ
SwooleÊÇ»ùÓÚPHPÓïÑÔµÄÍøÂçͨѶ¿ò¼Ü£¬ËüÄÚÖÃÁËгÌÖ§³Ö£¬¿ÉÒÔ×ÊÖú¿ª·¢ÕßÇáËɵØʵÏÖ¸ßЧµÄÒì²½±à³ÌºÍ²¢·¢²Ù×÷¡£Ð³ÌÊÇÒ»ÖÖÇáÁ¿¼¶µÄỊ̈߳¬¿ÉÒÔʵÏÖÔÚÒ»¸öÏß³ÌÖÐÔÚ²î±ðµÄ´úÂë¿éÖ®¼ä¾ÙÐÐÇл»£¬Òò´Ë¿ÉÒÔÔÚµ¥Ïß³ÌÏÂÃæʵÏÖ²¢·¢²Ù×÷¡£SwooleµÄгÌÄ£¿éÖ÷ÒªÓÐÒÔϼ¸¸ö×é¼þ£º
1¡¢Ð³Ìµ÷ÀíÆ÷£ºÓÃÓÚг̿ØÖƺÍÇл»¡£
2¡¢Ð³Ì¿Í»§¶Ë£º¿ÉÒÔʵÏÖг̷½·¨ÏµÄÍøÂçͨѶ¡£
3¡¢Ð³ÌÐźż°×¼Ê±Æ÷£º¿ÉÒÔʵÏÖг̷½·¨ÏµÄ׼ʱÆ÷ºÍÐźŴ¦Öóͷ£¡£
4¡¢Ð³ÌSocket¼°HTTPЧÀÍÆ÷£º¿ÉÒÔʵÏÖг̷½·¨ÏµÄSocketͨѶºÍHTTPЧÀÍÆ÷¡£
¶þ¡¢Ê¹ÓÃSwooleгÌʵÏÖ¸ßЧ²¢·¢Êý¾Ý²Ù×÷
ÔÚSwooleгÌÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃCoun()º¯ÊýÀ´½¨ÉèÒ»¸öг̣¬ÀýÈ磺
Coun(function(){ // do something in coroutine });
µÇ¼ºó¸´ÖÆ
ÔÚгÌÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃSwooleÌṩµÄгÌMySQL¿Í»§¶ËºÍгÌRedis¿Í»§¶ËÀ´ÊµÏÖ¸ßЧ²¢·¢Êý¾Ý²Ù×÷¡£ÏÂÃæÎÒÃǽ«ÏêϸÏÈÈÝʹÓÃÕâÁ½ÖÖ¿Í»§¶ËµÄÒªÁì¡£
1¡¢Ê¹ÓÃгÌMySQL¿Í»§¶Ë
ÔÚʹÓÃгÌMySQL¿Í»§¶Ë֮ǰ£¬ÎÒÃÇÐèÒªÏÈ×°ÖÃswooleÀ©Õ¹ÖеÄmysqlÄ£¿é£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî×°Öãº
pecl install swoole_mysql
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³ÉÖ®ºó£¬ÎÒÃǾͿÉÒÔÔÚгÌÖÐʹÓÃгÌMySQL¿Í»§¶ËÁË¡£ÏÂÃæÊÇÒ»¸öʹÓÃгÌMySQL¿Í»§¶ËÅÌÎÊÊý¾ÝµÄÀý×Ó£º
Coun(function(){ $db = new SwooleCoroutineMySQL(); $db->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => '123456', 'database' => 'test', ]); $res = $db->query('select * from user'); var_dump($res); });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÊ×ÏȽ¨ÉèÁËÒ»¸öгÌMySQL¿Í»§¶Ë£¬È»ºóʹÓÃconnect()ÒªÁìÅþÁ¬µ½MySQLЧÀÍÆ÷¡£½Ó×Å£¬ÎÒÃÇʹÓÃquery()ÒªÁìÀ´Ö´ÐÐÒ»ÌõÅÌÎÊÓï¾ä£¬²¢½«Ð§¹û·ÅÈë$res±äÁ¿ÖС£ÕâÑù£¬ÎÒÃǾͿÉÒÔÔÚгÌÖÐʵÏÖ¸ßЧµÄÊý¾ÝÅÌÎÊÁË¡£
2¡¢Ê¹ÓÃгÌRedis¿Í»§¶Ë
ÔÚʹÓÃгÌRedis¿Í»§¶Ë֮ǰ£¬ÎÒÃÇͬÑùÐèÒªÏÈ×°ÖÃswooleÀ©Õ¹ÖеÄredisÄ£¿é£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî×°Öãº
pecl install swoole_redis
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³ÉÖ®ºó£¬ÎÒÃǾͿÉÒÔÔÚгÌÖÐʹÓÃгÌRedis¿Í»§¶ËÁË¡£ÏÂÃæÊÇÒ»¸öʹÓÃгÌRedis¿Í»§¶Ë´æÈ¡Êý¾ÝµÄÀý×Ó£º
Coun(function(){ $redis = new SwooleCoroutineRedis(); $redis->connect('127.0.0.1', 6379); $redis->set('name', 'swoole'); $name = $redis->get('name'); var_dump($name); });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÊ×ÏȽ¨ÉèÁËÒ»¸öгÌRedis¿Í»§¶Ë£¬È»ºóʹÓÃconnect()ÒªÁìÅþÁ¬µ½RedisЧÀÍÆ÷¡£½Ó×Å£¬ÎÒÃÇʹÓÃset()ÒªÁ콫һ¸ö¼üÖµ¶Ô´æÈëRedisÖУ¬ÔÙʹÓÃget()ÒªÁìÀ´»ñÈ¡¼üÖµ¶ÔµÄÖµ¡£ÕâÑù£¬ÎÒÃǾͿÉÒÔÔÚгÌÖÐʵÏÖ¸ßЧµÄ´æÈ¡Êý¾ÝÁË¡£
Èý¡¢×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃSwooleгÌÀ´ÊµÏÖ¸ßЧ²¢·¢Êý¾Ý²Ù×÷¡£Í¨¹ýʹÓÃгÌMySQL¿Í»§¶ËºÍгÌRedis¿Í»§¶Ë£¬ÎÒÃÇ¿ÉÒÔÔÚÒ»¸öÏß³ÌÖÐʵÏÖ¸ßЧµÄÊý¾ÝÅÌÎʺͲÙ×÷¡£Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬Ê¹ÓÃSwooleгÌÀ´ÊµÏÖ¸ßЧ²¢·¢Êý¾Ý²Ù×÷ÒѾ³ÉΪЧÀÍÆ÷¿ª·¢ÕßµÄÒ»¸öÖ÷ÒªÊÖÒÕ¡£ÎÒÃÇÏ£Íû±¾ÎÄÄܹ»¶Ô¸÷ÈËÓÐËù×ÊÖú£¬×ÊÖú¸÷È˸üºÃµØÓ¦¶Ô²¢·¢Êý¾Ý²Ù×÷µÄÎÊÌâ¡£
ÒÔÉϾÍÊÇSwoole½ø½×£ºÔõÑùʹÓÃгÌʵÏÖ¸ßЧ²¢·¢Êý¾Ý²Ù×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡