{"id":24692,"date":"2024-11-21T08:00:33","date_gmt":"2024-11-21T00:00:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24692\/"},"modified":"2024-11-21T08:00:33","modified_gmt":"2024-11-21T00:00:33","slug":"%e4%bd%bf%e7%94%a8thinkphp6%e5%92%8cswoole%e6%9e%84%e5%bb%ba%e7%9a%84rpc%e6%9c%8d%e5%8a%a1%e5%ae%9e%e7%8e%b0%e9%ab%98%e6%95%88%e6%95%b0%e6%8d%ae%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24692\/","title":{"rendered":"\u4f7f\u7528ThinkPHP6\u548cSwoole\u6784\u5efa\u7684RPC\u670d\u52a1\u5b9e\u73b0\u9ad8\u6548\u6570\u636e\u540c\u6b65"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169709232131006.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528ThinkPHP6\u548cSwoole\u6784\u5efa\u7684RPC\u670d\u52a1\u5b9e\u73b0\u9ad8\u6548\u6570\u636e\u540c\u6b65\u63d2\u56fe\" alt=\"\u4f7f\u7528ThinkPHP6\u548cSwoole\u6784\u5efa\u7684RPC\u670d\u52a1\u5b9e\u73b0\u9ad8\u6548\u6570\u636e\u540c\u6b65\u63d2\u56fe\" \/><\/p>\n<p>\u4f7f\u7528ThinkPHP6\u548cSwoole\u6784\u5efa\u7684RPC\u670d\u52a1\u5b9e\u73b0\u9ad8\u6548\u6570\u636e\u540c\u6b65<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u731b\u53d1\u5c55\u548c\u5927\u6570\u636e\u7684\u666e\u53ca\u5e94\u7528\uff0c\u6570\u636e\u7684\u540c\u6b65\u548c\u4f20\u8f93\u6210\u4e3a\u4e86\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u63d0\u9ad8\u6570\u636e\u540c\u6b65\u7684\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528RPC\uff08Remote Procedure Call\uff09\u6765\u5b9e\u73b0\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff0c\u800c\u7ed3\u5408ThinkPHP6\u548cSwoole\u6846\u67b6\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u6784\u5efa\u4e00\u4e2aRPC\u670d\u52a1\u6765\u5b9e\u73b0\u6570\u636e\u7684\u540c\u6b65\u64cd\u4f5c\u3002<\/p>\n<p>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/p>\n<ol>\n<li>\u5b89\u88c5ThinkPHP6\u548cSwoole<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5ThinkPHP6\u548cSwoole\u6846\u67b6\u3002\u53ef\u4ee5\u4f7f\u7528Composer\u6765\u5b89\u88c5ThinkPHP6\u548cSwoole\uff0c\u4ee5\u4e0b\u662f\u5b89\u88c5\u547d\u4ee4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>composer create-project topthink\/think tp6<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>composer require swoole\/swoole<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u521b\u5efa\u9879\u76ee<\/li>\n<\/ol>\n<p>\u5728\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ThinkPHP6\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684ThinkPHP6\u9879\u76ee\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php think create:project sync_project<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u5165\u9879\u76ee\u6839\u76ee\u5f55\uff0c\u7136\u540e\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u542f\u52a8Swoole\u670d\u52a1\uff1a<\/p>\n<pre>php think swoole:server<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u51c6\u5907\u5de5\u4f5c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u6784\u5efa\u6211\u4eec\u7684RPC\u670d\u52a1\u4e86\u3002<\/p>\n<p>\u4e8c\u3001\u6784\u5efaRPC\u670d\u52a1<\/p>\n<ol>\n<li>\u521b\u5efaRPC\u670d\u52a1\u7c7b<\/li>\n<\/ol>\n<p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aRpcService.php\u6587\u4ef6\uff0c\u4f5c\u4e3a\u6211\u4eec\u7684RPC\u670d\u52a1\u7c7b\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace appindexservice;\n\nuse thinkService;\nuse SwooleServer;\n\nclass RpcService extends Service\n{\n    protected $server;\n\n    public function __construct(Server $server)\n    {\n        $this-&gt;server = $server;\n    }\n\n    public function register()\n    {\n        $this-&amp;gt;app-&amp;gt;bind('RpcService', function() {\n            return $this;\n        });\n    }\n\n    public function start()\n    {\n        $this-&amp;gt;server-&amp;gt;on('receive', [$this, 'onReceive']);\n        $this-&amp;gt;server-&amp;gt;start();\n    }\n\n    public function onReceive(Server $server, $fd, $from_id, $data)\n    {\n        \/\/ \u5904\u7406RPC\u8c03\u7528\u8bf7\u6c42\n        $result = $this-&amp;gt;processData($data);\n        \n        \/\/ \u5c06\u5904\u7406\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\n        $server-&amp;gt;send($fd, $result);\n    }\n\n    public function processData($data)\n    {\n        \/\/ \u89e3\u6790\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\n        \/\/ \u6839\u636e\u8bf7\u6c42\u53c2\u6570\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5728RpcService\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u4f20\u5165\u4e86SwooleServer\u5b9e\u4f8b\uff0c\u7528\u4e8e\u542f\u52a8Swoole\u670d\u52a1\u3002\u7136\u540e\u5728register\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528app-&gt;bind\u65b9\u6cd5\u5c06RpcService\u7c7b\u7ed1\u5b9a\u5230\u5bb9\u5668\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u53ef\u4ee5\u901a\u8fc7\u5bb9\u5668\u6765\u83b7\u53d6RpcService\u7684\u5b9e\u4f8b\u3002\u63a5\u4e0b\u6765\uff0c\u5728start\u65b9\u6cd5\u4e2d\u6211\u4eec\u6ce8\u518c\u4e86Swoole\u670d\u52a1\u7684onReceive\u4e8b\u4ef6\u3002\u5728onReceive\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5904\u7406RPC\u8c03\u7528\u8bf7\u6c42\uff0c\u5e76\u5c06\u5904\u7406\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002\u6700\u540e\uff0c\u5728processData\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u5e76\u8fd4\u56de\u5904\u7406\u7ed3\u679c\u3002<\/p>\n<ol>\n<li>\u6ce8\u518cRPC\u670d\u52a1<\/li>\n<\/ol>\n<p>\u5728\u9879\u76ee\u7684\u5165\u53e3\u6587\u4ef6\uff08public\/index.php\uff09\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6ce8\u518c\u6211\u4eec\u7684RPC\u670d\u52a1\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>...\n\n\/\/ \u6ce8\u518cRPC\u670d\u52a1\n$app-&amp;gt;register(ppindexserviceRpcService::class);\n\n...<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u5c06RpcService\u7c7b\u6ce8\u518c\u5230\u5bb9\u5668\u4e2d\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528RPC\u8c03\u7528<\/li>\n<\/ol>\n<p>\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528RPC\u8c03\u7528\u7684\u5730\u65b9\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bb9\u5668\u6765\u83b7\u53d6RpcService\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u8fdb\u884cRPC\u8c03\u7528\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>public function syncData()\n{\n    \/\/ \u83b7\u53d6RpcService\u5b9e\u4f8b\n    $rpcService = app('RpcService');\n\n    \/\/ \u6784\u9020\u8981\u53d1\u9001\u7684\u6570\u636e\n    $data = [\n        \/\/ \u6570\u636e\u5185\u5bb9\n    ];\n\n    \/\/ \u53d1\u9001RPC\u8c03\u7528\u8bf7\u6c42\uff0c\u5e76\u63a5\u6536\u5904\u7406\u7ed3\u679c\n    $result = $rpcService-&amp;gt;processData($data);\n\n    \/\/ \u5904\u7406RPC\u8c03\u7528\u7ed3\u679c\n    \/\/ ...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5728\u9879\u76ee\u4e2d\u8fdb\u884cRPC\u8c03\u7528\u5e76\u83b7\u53d6\u5904\u7406\u7ed3\u679c\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u4f7f\u7528\u4e86ThinkPHP6\u548cSwoole\u6846\u67b6\u6784\u5efa\u4e86\u4e00\u4e2aRPC\u670d\u52a1\u6765\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u540c\u6b65\u3002\u901a\u8fc7RPC\u8c03\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u670d\u52a1\u4e4b\u95f4\u5b9e\u73b0\u6570\u636e\u7684\u540c\u6b65\u548c\u4f20\u8f93\uff0c\u4ece\u800c\u63d0\u9ad8\u6570\u636e\u540c\u6b65\u7684\u6548\u7387\u3002\u540c\u65f6\uff0c\u501f\u52a9Swoole\u6846\u67b6\u7684\u9ad8\u6027\u80fd\u7279\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684RPC\u670d\u52a1\u3002<\/p>\n<p>\u6ce8\uff1a\u4ee5\u4e0a\u4ee3\u7801\u4e3a\u793a\u4f8b\u4ee3\u7801\uff0c\u5177\u4f53\u7684RPC\u8c03\u7528\u65b9\u5f0f\u548c\u6570\u636e\u5904\u7406\u903b\u8f91\u9700\u8981\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528ThinkPHP6\u548cSwoole\u6784\u5efa\u7684RPC\u670d\u52a1\u5b9e\u73b0\u9ad8\u6548\u6570\u636e\u540c\u6b65\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>\u4f7f\u7528ThinkPHP6\u548cSwoole\u6784\u5efa\u7684RPC\u670d\u52a1\u5b9e\u73b0\u9ad8\u6548\u6570\u636e\u540c\u6b65 \u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u731b\u53d1\u5c55\u548c\u5927\u6570\u636e\u7684\u666e\u53ca\u5e94\u7528\uff0c\u6570\u636e\u7684\u540c\u6b65\u548c\u4f20\u8f93\u6210\u4e3a\u4e86\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u63d0\u9ad8\u6570\u636e\u540c\u6b65\u7684\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528RPC\uff08Remote Procedure Call\uff09\u6765\u5b9e\u73b0\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff0c\u800c\u7ed3\u5408ThinkPHP6\u548cSwoole\u6846\u67b6\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u6784\u5efa\u4e00\u4e2aRPC\u670d\u52a1\u6765\u5b9e\u73b0\u6570\u636e\u7684\u540c\u6b65\u64cd\u4f5c\u3002 \u4e00\u3001\u51c6\u5907\u5de5\u4f5c \u5b89\u88c5ThinkPHP6\u548cSwoole \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5ThinkPHP6\u548cSwoole\u6846\u67b6\u3002\u53ef\u4ee5\u4f7f\u7528Composer\u6765\u5b89\u88c5ThinkPHP6\u548cSwoole\uff0c\u4ee5\u4e0b\u662f\u5b89\u88c5\u547d\u4ee4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b composer create-project topthink\/think tp6 \u767b\u5f55\u540e\u590d\u5236 composer require swoole\/swoole \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u9879\u76ee \u5728\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ThinkPHP6\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684ThinkPHP6\u9879\u76ee\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u5982\u4e0b\u547d\u4ee4\uff1a php think create:project sync_project \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u5165\u9879\u76ee\u6839\u76ee\u5f55\uff0c\u7136\u540e\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u542f\u52a8Swoole\u670d\u52a1\uff1a php think swoole:server \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7\u4ee5\u4e0a\u51c6\u5907\u5de5\u4f5c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u6784\u5efa\u6211\u4eec\u7684RPC\u670d\u52a1\u4e86\u3002 \u4e8c\u3001\u6784\u5efaRPC\u670d\u52a1 \u521b\u5efaRPC\u670d\u52a1\u7c7b \u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aRpcService.php\u6587\u4ef6\uff0c\u4f5c\u4e3a\u6211\u4eec\u7684RPC\u670d\u52a1\u7c7b\u3002\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php namespace appindexservice; use thinkService; use SwooleServer; class RpcService extends Service { protected $server; public function __construct(Server $server) { $this-&gt;server = $server; [&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-24692","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24692","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=24692"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24692\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}