Swooleʵ¼ù£º¹¹½¨¸ßÐÔÄܵÄ΢ЧÀÍ¿ò¼Ü
Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬Î¢Ð§Àͼܹ¹ÒѾ³ÉΪÁËÄ¿½ñµÄÇ÷ÊÆ¡£Î¢Ð§ÀÍÊÇÒ»ÖÖÉè¼ÆÀíÄͨ¹ý½«Ò»¸ö´óÐ͵ļòµ¥Ó¦ÓóÌÐò¾ÙÐвð·Ö³É¶à¸öСÐÍÓ¦ÓóÌÐòÀ´ÊµÏÖÄ£¿é»¯¿ª·¢¡£ÕâÖÖģʽʹµÃÍŶӿÉÒÔ¸ü¿ìµØ¹¹½¨ºÍ°²ÅÅÖØ´óµÄϵͳ£¬Í¬Ê±Ò²¾ß±¸¸üºÃµÄ¿ÉÀ©Õ¹ÐԺͿÉά»¤ÐÔ¡£
ÔÚÕâÀÎÒÃǽ«ÏÈÈÝÒ»ÖÖʹÓÃSwooleʵÏÖ¸ßÐÔÄÜ΢ЧÀͼܹ¹µÄÒªÁì¡£SwooleÊÇÒ»ÖÖ¿ªÔ´µÄ¸ßÐÔÄÜÍøÂç¿ò¼Ü£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖÒì²½IO¡¢Ð³ÌºÍTCP/UDPÍøÂç±à³ÌµÈ¹¦Ð§¡£Ëü¾ßÓÐÓÅÒìµÄÐÔÄܺÍÎȹÌÐÔ£¬Ê¹µÃËü³ÉΪ¹¹½¨¸ßÐÔÄÜ΢ЧÀͼܹ¹µÄÀíÏëÑ¡Ôñ¡£
ΪÁËÑÝʾSwooleÔÚ¹¹½¨Î¢Ð§ÀÍÖеÄÓ¦Óã¬ÎÒÃǽ«½¨ÉèÒ»¸ö¼òÆÓµÄ΢ЧÀÍÓ¦ÓóÌÐò£¬°üÀ¨APIÍø¹ØºÍ¶à¸öЧÀͽڵ㡣ËùÓÐЧÀͽڵ㽫ʹÓÃSwooleʵÏÖÒ첽ͨѶºÍ¸ßÐÔÄÜ´¦Àí¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÉè¼ÆÒ»¸öAPIÍø¹Ø£¬½«¿Í»§¶ËÇëÇóת·¢µ½²î±ðµÄЧÀͽڵ㡣»ùÓÚSwooleµÄHTTPЧÀÍÆ÷ÌṩÁËÒ»ÖÖ¼òÆӵķ½·¨À´ÊµÏÖÕâ¸ö¹¦Ð§¡£ÎÒÃÇ¿ÉÒÔ±àдһ¸ö¼òÆÓµÄHTTPЧÀÍÆ÷£¬´¦Àí¿Í»§¶ËÇëÇó²¢½«ËüÃÇ·Óɵ½²î±ðµÄЧÀÍ¡£
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«Ê¹ÓÃÒÔÏ·ÓÉ£º
/user/:id – »ñÈ¡Ò»¸öÌض¨Óû§µÄÏêϸÐÅÏ¢
/product/:id – »ñÈ¡Ò»¸öÌض¨ÉÌÆ·µÄÏêϸÐÅÏ¢
ÔÚ·ÓÉÖУ¬:idÊÇÒ»¸ö²ÎÊý£¬ÓÃÓÚ¶¨Î»Ìض¨µÄÓû§»òÉÌÆ·¡£ÎÒÃǽ«ÔÚÎÒÃǵÄЧÀͽڵãÖÐʵÏÖÕâЩAPI¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨Éè¶à¸öЧÀͽڵ㣬ËüÃǽ«´¦ÀíAPIÇëÇó²¢ÏìÓ¦¿Í»§¶Ë¡£ÓÉÓÚSwooleµÄÒì²½IOºÍгÌÖ§³Ö£¬ÎÒÃÇ¿ÉÒÔʹÓÃËüÀ´ÊµÏÖ¿ìËÙÏìÓ¦µÄЧÀͶˡ£
ÎÒÃǽ«½¨ÉèÁ½¸öЧÀͽڵ㣺Óû§Ð§ÀͺÍÉÌƷЧÀÍ¡£ÎªÁ˼ò»¯Õâ¸öÀý×Ó£¬ÎÒÃǽ«Ê¹ÓÃSQLite×÷ΪÎÒÃǵÄÊý¾Ý¿â¡£
Óû§Ð§Àͽ«ÌṩÒÔÏÂAPI£º
/user/:id – »ñÈ¡Ò»¸öÌض¨Óû§µÄÏêϸÐÅÏ¢
/user/:id/orders – »ñÈ¡Ò»¸öÌض¨Óû§µÄËùÓж©µ¥
ÉÌƷЧÀͽ«ÌṩÒÔÏÂAPI£º
/product/:id – »ñÈ¡Ò»¸öÌض¨ÉÌÆ·µÄÏêϸÐÅÏ¢
ÎÒÃǽ«ÑÝʾÔõÑùʹÓÃSwoole½¨ÉèÒ»¸ö»ù±¾µÄЧÀͽڵ㡣Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö»ù´¡µÄSwooleЧÀÍÆ÷£¬²¢¼àÌýÖ¸¶¨µÄ¶Ë¿Ú¡£
$server = new SwooleServer('0.0.0.0', 8001, SWOOLE_PROCESS, SWOOLE_SOCK_TCP); $server->on('Receive', function (SwooleServer $server, $fd, $reactor_id, $data) { }); $server->start();
µÇ¼ºó¸´ÖÆ
µ±ÓÐÇëÇóµÖ´ïʱ£¬ÎÒÃǽ«ÔÚon(‘receive’)»Øµ÷Öд¦ÀíËü¡£ÎÒÃǽ«ÆÊÎöHTTPÇëÇó²¢ÌáÈ¡ÓÐÓõÄÐÅÏ¢£¬ÈçURLºÍHTTPÒªÁì¡£
$server->on('Receive', function (SwooleServer $server, $fd, $reactor_id, $data) { // ÆÊÎöHTTPÇëÇó $header_end_pos = strpos($data, " "); $header_data = substr($data, 0, $header_end_pos); $body_data = substr($data, $header_end_pos + 4); $http_parser = new SwooleHttpParser(); $request = $http_parser->execute($header_data, $body_data); // ÌáÈ¡URLºÍHTTPÒªÁì $path_info = isset($request['server']['path_info']) ? $request['server']['path_info'] : '/'; $http_method = isset($request['server']['request_method']) ? $request['server']['request_method'] : 'GET'; });
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃǽ«´¦ÀíÕâЩÇëÇó²¢·µ»ØÏìÓ¦¡£ÎÒÃÇÐèҪʹÓÃSwooleµÄгÌÌØÕ÷ÔÚͳһ¸öÇëÇó´¦ÀíÖÜÆÚÄÚʵÏÖÒì²½IO¡£ÔÚÎÒÃǵÄÀý×ÓÖУ¬ÎÒÃǽ«ÅÌÎÊSQLiteÊý¾Ý¿âÀ´»ñÈ¡Óû§ºÍÉÌÆ·ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔʹÓÃг̿ͻ§¶ËÀ©Õ¹À´Ö´ÐÐÒì²½ÅÌÎʺÍдÈë¡£×îºó£¬ÎÒÃǽ«·µ»ØÒ»¸ö´øÓÐ׼ȷHTTP״̬ÂëºÍÏìÓ¦ÄÚÈݵÄHTTPÏìÓ¦¡£
$server->on('Receive', function (SwooleServer $server, $fd, $reactor_id, $data) { // ÆÊÎöHTTPÇëÇó ... // ´¦ÀíÇëÇó $response = ['status' => 404, 'content' => 'Not Found']; if (preg_match('/^/user/(d+)/', $path_info, $matches)) { // ÅÌÎÊÓû§ÐÅÏ¢ $user_id = $matches[1]; $db = new SwooleCoroutineMySQL(); $db->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => 'password', 'database' => 'test' ]); $result = $db->query("SELECT * FROM users WHERE id = '{$user_id}'"); // ÌìÉúÏìÓ¦ if ($result) { $response = ['status' => 200, 'content' => json_encode($result->fetch(), JSON_UNESCAPED_UNICODE)]; } else { $response = ['status' => 404, 'content' => 'Not Found']; } $db->close(); } else if (preg_match('/^/user/(d+)/orders/', $path_info, $matches)) { // ÅÌÎÊÓû§¶©µ¥ ... // ÌìÉúÏìÓ¦ ... } else if (preg_match('/^/product/(d+)/', $path_info, $matches)) { // ÅÌÎÊÉÌÆ·ÐÅÏ¢ ... // ÌìÉúÏìÓ¦ ... } // ·¢ËÍÏìÓ¦ $http_response = new SwooleHttpResponse(); $http_response->status($response['status']); $http_response->header('Content-Type', 'application/json'); $http_response->end($response['content']); });
µÇ¼ºó¸´ÖÆ
ÕâÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ËµÃ÷ÎúÔõÑùʹÓÃSwooleÀ´¹¹½¨¸ßÐÔÄܵÄ΢ЧÀͼܹ¹¡£SwooleÌṩÁËÐí¶à¹¦Ð§ºÍ¹¤¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖ¿ìËÙÏìÓ¦¡¢¸ßÐÔÄܺͿÉÀ©Õ¹µÄ΢ЧÀÍÓ¦ÓóÌÐò¡£
ÔÚʹÓÃSwooleʱ£¬ÎÒÃÇÐèҪעÖØÒÔϼ¸µã£º
ʼÖÕ¼á³Ö´úÂ뾫Á·ºÍÒ×ÓÚÃ÷È·¡£
³ä·ÖʹÓÃSwooleµÄÒì²½IOºÍгÌÌØÕ÷£¬ÒÔʵÏÖ¸ßÐÔÄÜ´¦ÀíºÍÏìÓ¦¡£
½«¶à¸öЧÀͽڵãÍŽáÆðÀ´£¬Ê¹ÓÃAPIÍø¹ØͳһÖÎÀíºÍ·ÓÉÇëÇó¡£
ͨ¹ýÕâÖÖ·½·¨£¬ÎÒÃÇ¿ÉÒÔÇáËɵع¹½¨¸ßÐÔÄܵÄ΢ЧÀͼܹ¹£¬ÎªÎÒÃǵÄÓ¦ÓóÌÐòÌṩ¸üºÃµÄ¿ÉÀ©Õ¹ÐÔ¡¢¿Éά»¤ÐԺͿɿ¿ÐÔ¡£
ÒÔÉϾÍÊÇSwooleʵ¼ù£º¹¹½¨¸ßÐÔÄܵÄ΢ЧÀÍ¿ò¼ÜµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡