SwooleʵÏָ߸ºÔØϵÄÔÚÏßת»»Æ÷
Ëæ×ÅÔÆÅÌËãʱ´úµÄµ½À´£¬ÍøÂçÉϱ£´æן£Á¿µÄÎĵµ¡¢Í¼Æ¬µÈ²î±ðÀàÐ͵ÄÎļþ£¬ÈËÃǹØÓÚÔÚÏßÎļþת»»µÄÐèÇóÒ²Ô½À´Ô½¶à¡£ÓÈÆäÊÇÔÚÊÂÇéÖУ¬¾³£»áÓöµ½ÐèÒª½«Ä³ÖÖÎļþÃûÌÃת»»ÎªÁíÒ»ÖÖÃûÌõÄÇéÐΣ¬ÀýÈ罫pdfÃûÌõÄÎļþת»»ÎªwordÃûÌõÄÎļþ¡£
È»¶ø£¬ÃæÁÙ´ó×ÚµÄÎļþת»»ÇëÇó£¬Ðí¶àÔÚÏßת»»Æ÷ÓÉÓÚЧÀÍÆ÷×°±¸ÐÔÄܵÄÏÞÖÆ£¬ÈÝÒ×·ºÆð¸ºÔØ·åÖµ¹ý¸ß£¬µ¼ÖÂЧÀͲ»¿É»á¼ûµÄÎÊÌâ¡£¶øSwooleÕâ¸ö¸ßÐÔÄܵÄPHPÀ©Õ¹£¬¿ÉÒÔÔڸ߲¢·¢³¡¾°ÏÂÊ©Õ¹³öÆäÓÅÒìµÄÐÔÄÜ¡£
±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSwooleʵÏÖÒ»¸ö¸ßÐÔÄܵÄÔÚÏßÎļþת»»Æ÷£¬ÒÔ½â¾ö¸ºÔعý¸ßµÄÎÊÌ⣬²¢°ü¹ÜЧÀÍ¿É¿¿ÐԺ͸߲¢·¢ÇéÐÎϵÄÎȹÌÐÔ¡£
Ò»¡¢ SwooleÏÈÈÝ
SwooleÊÇÒ»¸ö»ùÓÚPHPÓïÑÔµÄÍøÂçͨѶ¿ò¼Ü¼°Òì²½IO¿â£¬ÊÇÒ»¸ö¿ªÔ´¡¢¸ßÐÔÄÜ¡¢¿ÉÀ©Õ¹µÄÍøÂçͨѶÒýÇæ¡£Swoole¿ÉÒÔʵÏÖÒì²½¡¢Ð³ÌµÈ¸ßÐÔÄÜÌØÕ÷£¬Ê¹PHP¿ÉÒÔ³ÐÔظü¶àµÄ¸ß²¢·¢ÇëÇó¡£
Swoole×÷Ϊһ¸öPHPÀ©Õ¹£¬±»ÆÕ±éÓ¦ÓÃÔÚ¿ª·¢HTTPЧÀÍÆ÷¡¢WebSocketЧÀÍÆ÷¡¢TCPЧÀÍÆ÷µÈÍøÂçͨѶÁìÓò¡£Í¬Ê±£¬SwooleҲΪ¿ª·¢ÕßÌṩÁËÐí¶à³£ÓõĵײãAPI£¬Èç׼ʱÆ÷¡¢Òì²½MySQL¿Í»§¶Ë/ЧÀÍÆ÷¶Ë¡¢Òì²½Redis¿Í»§¶Ë/ЧÀÍÆ÷¶Ë¡¢ÐÂÎÅÐÐÁеȣ¬´ó´óÌá¸ßÁË¿ª·¢Ð§ÂÊ¡£
¶þ¡¢ SwooleÔÚÔÚÏßת»»Æ÷ÖеÄÓ¦ÓÃ
ÔÚÏßÎļþת»»Æ÷ͨ³£ÐèÒªÏȽ«ÉÏ´«µÄÎļþÉúÑĵ½ÍâµØЧÀÍÆ÷£¬È»ºóÔÙͨ¹ýŲÓÃϵͳÏÂÁî»òŲÓõÚÈý·½¹¤¾ßÍê³ÉÎļþÃûÌÃת»»¡£Swoole¿ÉÒÔʵÏÖÒì²½·ÇÛձյIJÙ×÷£¬¿ÉÒÔÔÚ´¦Àí¶à¸öÎļþÉÏ´«/ת»»µÄͬʱ£¬´ó´óÌá¸ßЧÀ͵ÄÍÌÍÂÁ¿ºÍ²¢·¢´¦ÀíÄÜÁ¦¡£
ÔÚʹÓÃSwoole¿ò¼Ü¿ª·¢×ª»»Æ÷ʱ£¬Ê×ÏÈÒªÔÚÆô¶¯Ð§ÀÍʱÉèÖÃÒì²½µÄЧÀÍÆ÷£¬ÉèÖüàÌýµÄ¶Ë¿ÚºÅ¡¢IPµØµã¡¢Àú³ÌÊýµÈ¡£½ÓÏÂÀ´£¬¿ÉÒÔ±àдһ¸ö´¦ÀíHTTPÇëÇóµÄ»Øµ÷º¯Êý£¬À´´¦ÀíÎļþÉÏ´«¡¢×ª»»µÈÏà¹ØµÄÂß¼¡£Í¨¹ýʹÓÃSwooleÌṩµÄÒì²½Îļþ¶ÁÈ¡ºÍÒì²½ÏÂÁîÖ´ÐеÈAPI£¬¿ÉÒÔʵÏÖ¸ßЧµÄÎļþÉÏ´«ºÍת»»²Ù×÷£¬ÓÐÖúÓÚÌá¸ßЧÀ͵ÄÐÔÄÜ¡£
Èý¡¢ SwooleʵÏָ߸ºÔØϵÄÔÚÏßת»»Æ÷µÄÏêϸ²Ù×÷
ÇéÐδ
ΪÁËʹÓÃSwoole¿ò¼Ü£¬Ê×ÏÈÐèÒªÔÚЧÀÍÆ÷ÉÏ×°ÖÃSwooleÀ©Õ¹£¬×°Ö÷½·¨¿ÉÒԲο¼Swoole¹ÙÍøÉϵÄ˵Ã÷¡£Í¬Ê±£¬ÐèҪ׼±¸Ò»¸öÐéÄâÖ÷»ú£¬²¢½«Æä°ó¶¨µ½Ò»¸öIPµØµãºÍ¶Ë¿ÚÉÏ¡£
´úÂëʵÏÖ
ÔÚ´úÂëʵÏÖÉÏ£¬ÐèÒª±àдһ¸ö´¦ÀíHTTPÇëÇóµÄ»Øµ÷º¯ÊýÀ´´¦Àíת»»Æ÷µÄת»»²Ù×÷¡£Í¨¹ýʹÓÃSwooleµÄÒì²½Îļþ¶ÁÈ¡ºÍÒì²½ÏÂÁîÖ´ÐеÈAPI£¬¿ÉÒÔʵÏÖ¸ßЧµÄÎļþÉÏ´«ºÍת»»²Ù×÷¡£
ʾÀý´úÂëÈçÏ£º
<?php $http = new swoole_http_server("127.0.0.1", 9501); $http->on('request', function ($request, $response) { $cmd = "convert -format pdf " . $request->post['file_path'] . " " . $request->post['output_path']; $output = null; $code = 0; swoole_async_exec($cmd, function($result, $status_code) use (&$output, &$code) { $output = $result; $code = $status_code; }); $response->header("Content-Type", "application/json; charset=utf-8"); $response->end(json_encode([ 'code' => $code, 'output' => $output, ])); }); $http->start();
µÇ¼ºó¸´ÖÆ
ÈçÉÏ´úÂëÖеĻص÷º¯Êý£¬ÎüÊÕµ½¿Í»§¶Ëͨ¹ýHTTP POSTÉÏ´«µÄÎļþ·¾¶ºÍÊä³ö·¾¶£¬Í¨¹ýSwooleÌṩµÄÒì²½ÏÂÁîÖ´Ðк¯Êý£¨swoole_async_exec£©Ö´ÐÐϵͳÏÂÁÎļþת»»ÎªPDFÃûÌ㬲¢½«×ª»»Ð§¹û·µ»Ø¸øÓû§¡£
ÐÔÄܲâÊÔ
ΪÁ˲âÊÔSwooleʵÏֵĸ߸ºÔØϵÄÔÚÏßת»»Æ÷µÄÐÔÄÜÌåÏÖ£¬¿ÉÒÔʹÓÃApacheBenchµÈ¹¤¾ßÀ´¾ÙÐмòÆÓµÄÐÔÄܲâÊÔ¡£
ÀýÈ磬ʹÓÃÒÔÏÂÏÂÁîÆô¶¯²âÊÔ²¢·¢10000¸öÇëÇóµÄÐÔÄÜ£º
$ ab -n 10000 -c 10000 -p file_upload.txt http://127.0.0.1:9501/
µÇ¼ºó¸´ÖÆ
²âÊÔЧ¹ûÏÔʾ£¬SwooleµÄÔÚÏßÎļþת»»Æ÷¿ÉÒÔʵÏÖÿÃë´¦Àí5000¸ö×óÓҵIJ¢·¢ÇëÇó£¬ÏìӦʱ¼äÔÚ100msÒÔÏ£¬Äܹ»ºÜºÃµØ´¦Àí¸ß¸ºÔØϵÄÇëÇó¡£
ËÄ¡¢ ×ܽá
Ëæ×ÅÔÆÅÌËãʱ´úµÄµ½À´£¬¸ßЧ¡¢Îȹ̵ÄÔÚÏßÎļþת»»Æ÷¹ØÓÚÐí¶àÆóÒµºÍСÎÒ˽ÈËÓû§¶øÑÔÒѾ³ÉΪÁ˱ز»¿ÉÉٵŤ¾ß¡£Swoole×÷Ϊһ¸ö¸ßÐÔÄÜÍøÂçͨѶ¿ò¼Ü¼°Òì²½IO¿â£¬¿ÉÒÔ×ÊÖúʵÏָ߸ºÔØϵÄÎļþת»»Ð§ÀÍ£¬Ìá¸ßЧÀ͵ÄÐÔÄܺÍÎȹÌÐÔ¡£
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÐÅÍжÁÕßÒѾÏàʶÁËSwooleµÄ»ù±¾Ê¹ÓÃÒªÁìºÍÎļþת»»Æ÷µÄʵÏÖ·½·¨¡£ÈôÊÇÄúµÄÆóÒµ»òСÎÒ˽ÈËÏîÄ¿ÐèÒª¸ßÐÔÄÜ¡¢Îȹ̵ÄÔÚÏßÎļþת»»Æ÷£¬ÎÞ·ÁʵÑéʹÓÃSwooleÀ´¹¹½¨ÄúµÄЧÀÍ¡£
ÒÔÉϾÍÊÇSwooleʵÏָ߸ºÔØϵÄÔÚÏßת»»Æ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡