{"id":1090,"date":"2025-01-19T14:07:02","date_gmt":"2025-01-19T06:07:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1090"},"modified":"2025-01-19T14:07:02","modified_gmt":"2025-01-19T06:07:02","slug":"%e6%80%8e%e6%a0%b7%e5%b0%86-swoole-%e5%8d%8f%e7%a8%8b%e9%9b%86%e6%88%90%e5%88%b0-laravel-%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/19\/%e6%80%8e%e6%a0%b7%e5%b0%86-swoole-%e5%8d%8f%e7%a8%8b%e9%9b%86%e6%88%90%e5%88%b0-laravel-%e4%b8%ad\/","title":{"rendered":"\u600e\u6837\u5c06 Swoole \u534f\u7a0b\u96c6\u6210\u5230 Laravel \u4e2d"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Laravel \u4e2d\u4f7f\u7528 Swoole \u534f\u7a0b\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347 Laravel \u5e94\u7528\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u9ad8\u5e76\u53d1\u548c\u9ad8\u8d1f\u8f7d\u7684\u60c5\u51b5\u4e0b\u3002\u901a\u8fc7\u96c6\u6210 Swoole\uff0c\u4f60\u53ef\u4ee5\u5229\u7528\u5176\u534f\u7a0b\u673a\u5236\u6765\u4f18\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3001I\/O \u8bf7\u6c42\u548c\u4efb\u52a1\u8c03\u5ea6\u7b49\u65b9\u9762\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u5c06 Swoole \u534f\u7a0b\u96c6\u6210\u5230 Laravel \u9879\u76ee\u4e2d\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5 Swoole \u6269\u5c55<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684 PHP \u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86 Swoole \u6269\u5c55\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5 Swoole\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pecl install swoole<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u9700\u8981\u5728 <code>php.ini<\/code> \u6587\u4ef6\u4e2d\u542f\u7528 Swoole \u6269\u5c55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extension=swoole.so<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5b89\u88c5 Swoole Laravel \u6269\u5c55<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u66f4\u65b9\u4fbf\u5730\u5c06 Swoole \u4e0e Laravel \u96c6\u6210\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Swoole \u63d0\u4f9b\u7684 Laravel \u6269\u5c55\u5305\uff0c\u4f8b\u5982 <code>swooletw\/laravel-swoole<\/code>\u3002\u901a\u8fc7\u8be5\u6269\u5c55\u5305\uff0cLaravel \u5e94\u7528\u53ef\u4ee5\u8f7b\u677e\u5229\u7528 Swoole \u7684\u7279\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 Composer \u5b89\u88c5\u8be5\u5305\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require swooletw\/laravel-swoole<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u914d\u7f6e Laravel \u9879\u76ee<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9700\u8981\u5c06 Swoole \u914d\u7f6e\u6587\u4ef6\u53d1\u5e03\u5230 Laravel \u914d\u7f6e\u76ee\u5f55\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan vendor:publish --provider=\"SwooleTW\\LaravelSwoole\\LaravelSwooleServiceProvider\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u4f1a\u628a\u914d\u7f6e\u6587\u4ef6\u53d1\u5e03\u5230 <code>config\/swoole.php<\/code>\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u6539\u3002\u914d\u7f6e\u6587\u4ef6\u4e2d\u5305\u542b\u4e86 Swoole \u7684\u591a\u79cd\u8bbe\u7f6e\uff0c\u4f8b\u5982\u5de5\u4f5c\u8fdb\u7a0b\u6570\u3001\u7aef\u53e3\u8bbe\u7f6e\u3001\u65e5\u5fd7\u914d\u7f6e\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u914d\u7f6e <code>.env<\/code> \u6587\u4ef6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>.env<\/code> \u6587\u4ef6\u4e2d\u914d\u7f6e Swoole \u7684\u76f8\u5173\u53c2\u6570\uff0c\u5c24\u5176\u662f\u670d\u52a1\u5668\u7aef\u53e3\u7b49\u4fe1\u606f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SWOOLE_HTTP_HOST=127.0.0.1\nSWOOLE_HTTP_PORT=9501<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u542f\u52a8 Swoole \u670d\u52a1\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Laravel \u4e2d\u542f\u7528 Swoole HTTP \u670d\u52a1\u5668\uff0c\u901a\u8fc7 <code>php artisan swoole:http start<\/code> \u547d\u4ee4\u542f\u52a8 Swoole \u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan swoole:http start<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u65f6\uff0cSwoole \u5c06\u4f1a\u542f\u52a8\u5e76\u76d1\u542c\u914d\u7f6e\u7684\u7aef\u53e3\uff08\u5982 <code>9501<\/code>\uff09\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee <code>http:\/\/127.0.0.1:9501<\/code> \u6765\u67e5\u770b\u4f60\u7684 Laravel \u5e94\u7528\u662f\u5426\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u4f7f\u7528\u534f\u7a0b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u534f\u7a0b\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u5b83\u80fd\u591f\u8ba9\u4f60\u4ee5\u5f02\u6b65\u3001\u975e\u963b\u585e\u7684\u65b9\u5f0f\u5904\u7406\u8bf7\u6c42\u3002\u5728 Laravel \u4e2d\u4f7f\u7528 Swoole \u534f\u7a0b\u65f6\uff0c\u5f88\u591a\u5e38\u89c4\u7684\u540c\u6b65\u64cd\u4f5c\uff08\u5982\u6570\u636e\u5e93\u67e5\u8be2\u3001HTTP \u8bf7\u6c42\u3001\u6587\u4ef6\u64cd\u4f5c\u7b49\uff09\u90fd\u53ef\u4ee5\u901a\u8fc7\u534f\u7a0b\u6765\u5b9e\u73b0\u5e76\u53d1\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u534f\u7a0b\u5316\u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Laravel \u4e2d\u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Swoole\\Coroutine<\/code> \u7c7b\u3002\u6bd4\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u534f\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Swoole\\Coroutine;\n\nclass MyController extends Controller\n{\n    public function index()\n    {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\n        Coroutine::create(function () {\n            \/\/ \u6267\u884c\u6570\u636e\u5e93\u67e5\u8be2\n            $users = DB::table('users')-&gt;get();\n\n            \/\/ \u8f93\u51fa\u67e5\u8be2\u7ed3\u679c\n            dd($users);\n        });\n\n        return response('Request is being processed');\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u534f\u7a0b\u5316 HTTP \u8bf7\u6c42<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 Swoole \u534f\u7a0b\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5f02\u6b65\u53d1\u8d77 HTTP \u8bf7\u6c42\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Swoole\\Coroutine\\Http\\Client;\n\nclass MyController extends Controller\n{\n    public function fetchData()\n    {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\n        Coroutine::create(function () {\n            \/\/ \u53d1\u8d77 HTTP \u8bf7\u6c42\n            $client = new Client('www.example.com', 80);\n            $client-&gt;get('\/');\n\n            \/\/ \u83b7\u53d6\u54cd\u5e94\u5e76\u8f93\u51fa\n            echo $client-&gt;body;\n        });\n\n        return response('Request is being processed');\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u534f\u7a0b\u5316 HTTP \u8bf7\u6c42\u548c\u6570\u636e\u5e93\u64cd\u4f5c\uff0cLaravel \u5e94\u7528\u80fd\u591f\u5728\u5904\u7406\u5927\u91cf\u8bf7\u6c42\u65f6\uff0c\u907f\u514d\u963b\u585e\uff0c\u63d0\u5347\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u914d\u7f6e\u4efb\u52a1\u8c03\u5ea6\uff08\u53ef\u9009\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Laravel \u4e2d\u7684\u4efb\u52a1\u8c03\u5ea6\u7cfb\u7edf\u4e5f\u53ef\u4ee5\u4e0e Swoole \u534f\u7a0b\u7ed3\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u8ba1\u5212\u4efb\u52a1\u901a\u8fc7 Swoole \u7684\u5f02\u6b65\u6a21\u578b\u6765\u6267\u884c\uff0c\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4efb\u52a1\u6267\u884c\u6548\u7387\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>SwooleTW\\LaravelSwoole\\Task<\/code> \u7c7b\u6765\u521b\u5efa\u548c\u8c03\u5ea6\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. \u4f7f\u7528 Swoole Worker \u8fdb\u7a0b\u6c60\uff08\u53ef\u9009\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u8fd8\u63d0\u4f9b\u4e86\u8fdb\u7a0b\u6c60\u7ba1\u7406\u80fd\u529b\uff0c\u5728 Laravel \u4e2d\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6765\u7ba1\u7406 worker \u8fdb\u7a0b\u6570\uff0c\u8fbe\u5230\u8d1f\u8f7d\u5747\u8861\u7684\u6548\u679c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u6765\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u8bf7\u6c42\uff0c\u51cf\u8f7b\u5355\u4e2a\u8fdb\u7a0b\u7684\u538b\u529b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. \u6d4b\u8bd5\u4e0e\u76d1\u63a7<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u8fd0\u884c Swoole \u534f\u7a0b\u65f6\uff0c\u5efa\u8bae\u5f00\u542f Swoole \u7684\u65e5\u5fd7\u548c\u76d1\u63a7\u529f\u80fd\uff0c\u786e\u4fdd\u5e94\u7528\u6027\u80fd\u7684\u53ef\u89c6\u5316\u76d1\u63a7\u548c\u95ee\u9898\u6392\u67e5\u3002\u53ef\u4ee5\u5728 <code>config\/swoole.php<\/code> \u4e2d\u914d\u7f6e\u65e5\u5fd7\u8def\u5f84\u548c\u5f00\u542f\u76d1\u63a7\u529f\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5c06 Swoole \u534f\u7a0b\u96c6\u6210\u5230 Laravel \u4e2d\uff0c\u4f60\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7684\u5e76\u53d1\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002Swoole \u534f\u7a0b\u4f7f\u5f97 Laravel \u652f\u6301\u5f02\u6b65 I\/O \u64cd\u4f5c\uff0c\u53ef\u4ee5\u5728\u65e0\u9700\u963b\u585e\u8bf7\u6c42\u7684\u60c5\u51b5\u4e0b\u9ad8\u6548\u5904\u7406\u5927\u91cf\u5e76\u53d1\u4efb\u52a1\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u9ad8\u541e\u5410\u91cf\u548c\u4f4e\u5ef6\u8fdf\u7684\u5b9e\u65f6\u5e94\u7528\u3001WebSocket \u670d\u52a1\u3001API \u7f51\u5173\u7b49\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Laravel \u4e2d\u4f7f\u7528 Swoole \u534f\u7a0b\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347 Laravel \u5e94\u7528\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u9ad8\u5e76\u53d1\u548c\u9ad8\u8d1f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1090","post","type-post","status-publish","format-standard","hentry","category-swoole"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1090"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1090\/revisions"}],"predecessor-version":[{"id":1091,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1090\/revisions\/1091"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}