{"id":24665,"date":"2024-11-21T13:31:12","date_gmt":"2024-11-21T05:31:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24665\/"},"modified":"2024-11-21T13:31:12","modified_gmt":"2024-11-21T05:31:12","slug":"tp6-think-swoole-rpc%e6%9c%8d%e5%8a%a1%e7%9a%84%e9%ab%98%e5%8f%af%e6%89%a9%e5%b1%95%e6%80%a7%e4%b8%8e%e5%88%86%e5%b8%83%e5%bc%8f%e9%83%a8%e7%bd%b2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24665\/","title":{"rendered":"TP6 Think-Swoole RPC\u670d\u52a1\u7684\u9ad8\u53ef\u6269\u5c55\u6027\u4e0e\u5206\u5e03\u5f0f\u90e8\u7f72"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169708007224708.jpg\" class=\"aligncenter\" title=\"TP6 Think-Swoole RPC\u670d\u52a1\u7684\u9ad8\u53ef\u6269\u5c55\u6027\u4e0e\u5206\u5e03\u5f0f\u90e8\u7f72\u63d2\u56fe\" alt=\"TP6 Think-Swoole RPC\u670d\u52a1\u7684\u9ad8\u53ef\u6269\u5c55\u6027\u4e0e\u5206\u5e03\u5f0f\u90e8\u7f72\u63d2\u56fe\" \/><\/p>\n<p>TP6\uff08ThinkPHP 6\uff09\u662f\u4e00\u6b3e\u57fa\u4e8ePHP\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u5177\u6709\u9ad8\u53ef\u6269\u5c55\u6027\u4e0e\u5206\u5e03\u5f0f\u90e8\u7f72\u7684\u7279\u70b9\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528TP6\u914d\u5408Swoole\u6269\u5c55\uff0c\u642d\u5efa\u4e00\u4e2a\u5177\u5907\u9ad8\u53ef\u6269\u5c55\u6027\u7684RPC\u670d\u52a1\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5TP6\u548cSwoole\u6269\u5c55\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>composer require topthink\/think\npecl install swoole<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728TP6\u7684\u914d\u7f6e\u6587\u4ef6config\/app.php\u4e2d\u542f\u7528Swoole\u6269\u5c55\uff1a<\/p>\n<pre>'providers'       =&amp;gt; [\n    \/\/ ...\n    thinkswooleServiceProvider::class,\n],<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aRPC\u670d\u52a1\u7c7b\uff0c\u7528\u4e8e\u5904\u7406\u8fdc\u7a0b\u8c03\u7528\u3002\u5728app\/swoole\/rpc\u76ee\u5f55\u4e0b\u521b\u5efaService.php\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace appswoolepc;\n\nclass Service\n{\n    public function hello($name)\n    {\n        return 'Hello, ' . $name;\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6211\u4eec\u8fd8\u9700\u8981\u521b\u5efa\u4e00\u4e2aRPC\u670d\u52a1\u542f\u52a8\u811a\u672c\u3002\u5728app\/swoole\u76ee\u5f55\u4e0b\u521b\u5efarpc.php\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php use thinkswooleRPC;\nuse appswoolepcService;\n\nrequire __DIR__ . '\/..\/vendor\/autoload.php';\n\n$rpc = new RPC();\n\n$rpc-&gt;service('Appswoole\rpcService') \/\/ \u6307\u5b9aRPC\u670d\u52a1\u7c7b\n    -&amp;gt;host('0.0.0.0') \/\/ \u76d1\u542cIP\u5730\u5740\n    -&amp;gt;port(9527) \/\/ \u76d1\u542c\u7aef\u53e3\u53f7\n    -&amp;gt;run();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86RPC\u670d\u52a1\u7684\u642d\u5efa\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528TP6\u63d0\u4f9b\u7684\u63a7\u5236\u53f0\u547d\u4ee4\u6765\u542f\u52a8RPC\u670d\u52a1\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php think swoole:rpc start<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u65f6\uff0cRPC\u670d\u52a1\u5df2\u7ecf\u6210\u529f\u542f\u52a8\uff0c\u5e76\u76d1\u542c\u57289527\u7aef\u53e3\u4e0a\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Swoole\u7684RpcClient\u6765\u8fdb\u884c\u8fdc\u7a0b\u8c03\u7528\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5topthink\/think-rpc-client\uff1a<\/p>\n<pre>composer require topthink\/think-rpc-client<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u8c03\u7528RPC\u670d\u52a1\uff1a<\/p>\n<pre>&lt;?php use thinkswoolepcClient;\n\nrequire __DIR__ . '\/vendor\/autoload.php';\n\n$client = new Client();\n\n$res = $client-&gt;hello('John'); \/\/ \u8c03\u7528RPC\u670d\u52a1\u7684hello\u65b9\u6cd5\n\nvar_dump($res);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5148\u521b\u5efa\u4e86\u4e00\u4e2aClient\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528\u5176hello\u65b9\u6cd5\u6765\u8c03\u7528RPC\u670d\u52a1\u4e2d\u7684hello\u65b9\u6cd5\u3002\u7ed3\u679c\u4f1a\u88ab\u8f93\u51fa\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u642d\u5efa\u4e86\u4e00\u4e2a\u57fa\u4e8eTP6\u548cSwoole\u7684RPC\u670d\u52a1\uff0c\u5e76\u8fdb\u884c\u4e86\u8fdc\u7a0b\u8c03\u7528\u3002\u901a\u8fc7\u4f7f\u7528TP6\u7684\u9ad8\u53ef\u6269\u5c55\u6027\u548cSwoole\u7684\u534f\u7a0b\u80fd\u529b\uff0c\u6211\u4eec\u80fd\u591f\u8f7b\u677e\u642d\u5efa\u51fa\u9ad8\u6027\u80fd\u4e14\u5177\u5907\u9ad8\u53ef\u6269\u5c55\u6027\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8eSwoole\u6269\u5c55\u7684\u7279\u6027\uff0c\u4ee5\u4e0a\u4ee3\u7801\u5fc5\u987b\u5728Swoole\u7684Server\u73af\u5883\u4e0b\u6267\u884c\uff0c\u800c\u4e0d\u80fd\u5728\u4f20\u7edf\u7684PHP-FPM\u73af\u5883\u4e2d\u6267\u884c\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5bf9\u60f3\u8981\u5b9e\u73b0\u9ad8\u53ef\u6269\u5c55\u6027\u4e0e\u5206\u5e03\u5f0f\u90e8\u7f72\u7684\u5f00\u53d1\u8005\u4eec\u63d0\u4f9b\u4e00\u4e9b\u5e2e\u52a9\u3002\u76f8\u4fe1\u901a\u8fc7\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u4f60\u4eec\u80fd\u591f\u66f4\u597d\u5730\u8fd0\u7528TP6\u548cSwoole\u6765\u6784\u5efa\u51fa\u66f4\u5f3a\u5927\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fTP6 Think-Swoole RPC\u670d\u52a1\u7684\u9ad8\u53ef\u6269\u5c55\u6027\u4e0e\u5206\u5e03\u5f0f\u90e8\u7f72\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TP6\uff08ThinkPHP 6\uff09\u662f\u4e00\u6b3e\u57fa\u4e8ePHP\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u5177\u6709\u9ad8\u53ef\u6269\u5c55\u6027\u4e0e\u5206\u5e03\u5f0f\u90e8\u7f72\u7684\u7279\u70b9\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528TP6\u914d\u5408Swoole\u6269\u5c55\uff0c\u642d\u5efa\u4e00\u4e2a\u5177\u5907\u9ad8\u53ef\u6269\u5c55\u6027\u7684RPC\u670d\u52a1\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5TP6\u548cSwoole\u6269\u5c55\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a composer require topthink\/think pecl install swoole \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728TP6\u7684\u914d\u7f6e\u6587\u4ef6config\/app.php\u4e2d\u542f\u7528Swoole\u6269\u5c55\uff1a &#8216;providers&#8217; =&amp;gt; [ \/\/ &#8230; thinkswooleServiceProvider::class, ], \u767b\u5f55\u540e\u590d\u5236 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aRPC\u670d\u52a1\u7c7b\uff0c\u7528\u4e8e\u5904\u7406\u8fdc\u7a0b\u8c03\u7528\u3002\u5728app\/swoole\/rpc\u76ee\u5f55\u4e0b\u521b\u5efaService.php\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a &lt;?php namespace appswoolepc; class Service { public function hello($name) { return &#8216;Hello, &#8216; . $name; } } \u767b\u5f55\u540e\u590d\u5236 \u6211\u4eec\u8fd8\u9700\u8981\u521b\u5efa\u4e00\u4e2aRPC\u670d\u52a1\u542f\u52a8\u811a\u672c\u3002\u5728app\/swoole\u76ee\u5f55\u4e0b\u521b\u5efarpc.php\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a &lt;?php use thinkswooleRPC; use appswoolepcService; require __DIR__ . &#8216;\/..\/vendor\/autoload.php&#8217;; $rpc = new RPC(); $rpc-&gt;service(&#8216;Appswoole pcService&#8217;) \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24665","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24665"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24665\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}