{"id":24662,"date":"2024-11-21T09:41:39","date_gmt":"2024-11-21T01:41:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24662\/"},"modified":"2024-11-21T09:41:39","modified_gmt":"2024-11-21T01:41:39","slug":"%e5%9f%ba%e4%ba%8etp6-think-swoole%e7%9a%84%e5%88%86%e5%b8%83%e5%bc%8frpc%e6%9c%8d%e5%8a%a1%e6%9e%b6%e6%9e%84%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24662\/","title":{"rendered":"\u57fa\u4e8eTP6 Think-Swoole\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169707900132380.jpg\" class=\"aligncenter\" title=\"\u57fa\u4e8eTP6 Think-Swoole\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1\u63d2\u56fe\" alt=\"\u57fa\u4e8eTP6 Think-Swoole\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1\u63d2\u56fe\" \/><\/p>\n<p>\u57fa\u4e8eTP6 Think-Swoole\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u9700\u6c42\u65e5\u76ca\u589e\u52a0\u3002\u5206\u5e03\u5f0f\u7cfb\u7edf\u53ef\u4ee5\u5c06\u5404\u4e2a\u6a21\u5757\u5206\u5f00\u90e8\u7f72\u5728\u4e0d\u540c\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7684\u53ef\u6269\u5c55\u6027\u548c\u53ef\u9760\u6027\u3002\u800cRPC\uff08Remote Procedure Call\uff09\u4f5c\u4e3a\u4e00\u79cd\u5e38\u7528\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u7684\u8fdc\u7a0b\u8c03\u7528\uff0c\u8fdb\u4e00\u6b65\u4fc3\u8fdb\u4e86\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u53d1\u5c55\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u57fa\u4e8eTP6 Think-Swoole\u6846\u67b6\u8bbe\u8ba1\u4e00\u4e2a\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p><strong>1. \u67b6\u6784\u8bbe\u8ba1<\/strong><br \/>\u6211\u4eec\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u5c06\u5305\u62ec\u4e09\u4e2a\u4e3b\u8981\u7ec4\u4ef6\uff1a\u670d\u52a1\u63d0\u4f9b\u8005\u3001\u670d\u52a1\u6d88\u8d39\u8005\u548c\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\u3002<\/p>\n<p>\u670d\u52a1\u63d0\u4f9b\u8005\uff1a\u8d1f\u8d23\u66b4\u9732\u670d\u52a1\u63a5\u53e3\uff0c\u63a5\u6536\u5e76\u5904\u7406RPC\u8bf7\u6c42\u3002<br \/>\u670d\u52a1\u6d88\u8d39\u8005\uff1a\u8d1f\u8d23\u53d1\u8d77RPC\u8bf7\u6c42\uff0c\u5e76\u83b7\u5f97\u670d\u52a1\u63d0\u4f9b\u8005\u7684\u54cd\u5e94\u3002<br \/>\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\uff1a\u8d1f\u8d23\u7ba1\u7406\u670d\u52a1\u63d0\u4f9b\u8005\u7684\u5730\u5740\u4fe1\u606f\u3002<\/p>\n<p><strong>2. \u5b9e\u73b0\u6b65\u9aa4<\/strong><\/p>\n<p>\uff081\uff09\u914d\u7f6e\u6587\u4ef6<br \/>\u9996\u5148\uff0c\u5728TP6\u6846\u67b6\u4e2d\u521b\u5efaconfig\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efarpc.php\u4f5c\u4e3aRPC\u914d\u7f6e\u6587\u4ef6\u3002\u914d\u7f6e\u6587\u4ef6\u4e2d\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>return [\n    'server' =&amp;gt; [\n        'host' =&amp;gt; '127.0.0.1',\n        'port' =&amp;gt; 9501,\n    ],\n    'registry' =&amp;gt; [\n        'host' =&amp;gt; '127.0.0.1',\n        'port' =&amp;gt; 2181,\n    ],\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\uff082\uff09\u670d\u52a1\u63d0\u4f9b\u8005\u7aef\u5b9e\u73b0<br \/>\u5728\u670d\u52a1\u63d0\u4f9b\u8005\u7aef\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aServer\u7c7b\u6765\u5904\u7406RPC\u8bf7\u6c42\uff0c\u5e76\u5c06\u670d\u52a1\u5730\u5740\u6ce8\u518c\u5230\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace apppcserver;\n\nuse thinkswooleServer;\n\nclass RpcServer extends Server\n{\n    protected $rpcService;\n\n    public function __construct($host, $port)\n    {\n        parent::__construct($host, $port);\n        $this-&gt;rpcService = new RpcService(); \/\/ \u81ea\u5b9a\u4e49\u7684\u670d\u52a1\u7c7b\n    }\n\n    public function onReceive(SwooleServer $server, int $fd, int $reactor_id, string $data)\n    {\n        \/\/ \u5904\u7406RPC\u8bf7\u6c42\n        $result = $this-&amp;gt;rpcService-&amp;gt;handleRequest($data);\n        \n        \/\/ \u53d1\u9001\u54cd\u5e94\u7ed3\u679c\u7ed9\u5ba2\u6237\u7aef\n        $server-&amp;gt;send($fd, $result);\n    }\n\n    public function onWorkerStart(SwooleServer $server, int $worker_id)\n    {\n        \/\/ \u6ce8\u518c\u670d\u52a1\u5230\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\n        $this-&amp;gt;registerService();\n    }\n\n    private function registerService()\n    {\n        \/\/ \u83b7\u53d6\u6ce8\u518c\u4e2d\u5fc3\u7684\u5730\u5740\u4fe1\u606f\n        $registryHost = config('rpc.registry.host');\n        $registryPort = config('rpc.registry.port');\n\n        \/\/ \u4f7f\u7528Zookeeper\u7b49\u65b9\u5f0f\u6ce8\u518c\u670d\u52a1\n        \/\/ ...\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\uff083\uff09\u670d\u52a1\u6d88\u8d39\u8005\u7aef\u5b9e\u73b0<br \/>\u5728\u670d\u52a1\u6d88\u8d39\u8005\u7aef\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aClient\u7c7b\u6765\u53d1\u8d77RPC\u8bf7\u6c42\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace apppcclient;\n\nuse thinkswooleRpc;\nuse thinkswooleRpcClient;\nuse thinkswooleRpcService;\nuse thinkswooleRpcProtocol;\n\nclass RpcClient\n{\n    protected $client;\n\n    public function __construct()\n    {\n        $this-&gt;client = new Client(new Protocol(), new Service());\n    }\n\n    public function request($service, $method, $params = [])\n    {\n        \/\/ \u521b\u5efaRPC\u8bf7\u6c42\u5e76\u53d1\u9001\n        $rpc = new Rpc($service, $method, $params);\n        $response = $this-&amp;gt;client-&amp;gt;sendAndRecv($rpc);\n        \n        \/\/ \u5904\u7406\u54cd\u5e94\u7ed3\u679c\u5e76\u8fd4\u56de\n        return $response-&amp;gt;getResult();\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\uff084\uff09\u6ce8\u518c\u4e2d\u5fc3\u5b9e\u73b0<br \/>\u5728\u6ce8\u518c\u4e2d\u5fc3\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Zookeeper\u4f5c\u4e3a\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace apppcegistry;\n\nuse zookeeper;\n\nclass Registry\n{\n    protected $zk;\n\n    public function __construct($host, $port)\n    {\n        $this-&gt;zk = new zookeeper($host . ':' . $port);\n    }\n\n    public function register($path, $data)\n    {\n        \/\/ \u521b\u5efa\u8282\u70b9\u5e76\u6ce8\u518c\u670d\u52a1\u5730\u5740\u4fe1\u606f\n        $this-&amp;gt;zk-&amp;gt;create($path, $data, []);\n    }\n\n    public function getServiceUrl($path)\n    {\n        \/\/ \u83b7\u53d6\u670d\u52a1\u5730\u5740\u4fe1\u606f\n        return $this-&amp;gt;zk-&amp;gt;get($path);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<p>\uff081\uff09\u5728\u670d\u52a1\u63d0\u4f9b\u8005\u7aef\u542f\u52a8RPC\u670d\u52a1\u5668<\/p>\n<pre>$rpcServer = new pppcserverRpcServer(config('rpc.server.host'), config('rpc.server.port'));\n$rpcServer-&amp;gt;start();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\uff082\uff09\u5728\u670d\u52a1\u6d88\u8d39\u8005\u7aef\u53d1\u8d77RPC\u8bf7\u6c42<\/p>\n<pre>$rpcClient = new pppcclientRpcClient();\n$result = $rpcClient-&amp;gt;request('app\rpcserverRpcService', 'hello', ['name' =&amp;gt; 'John']);\necho $result;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\uff083\uff09\u5728\u6ce8\u518c\u4e2d\u5fc3\u6ce8\u518c\u670d\u52a1<\/p>\n<pre>$registry = new pppcegistryRegistry(config('rpc.registry.host'), config('rpc.registry.port'));\n$registry-&amp;gt;register('\/rpc\/services\/RpcService', '127.0.0.1:9501');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u57fa\u4e8eTP6 Think-Swoole\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1\u7684\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002\u901a\u8fc7\u8fd9\u6837\u7684\u67b6\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u7684\u8fdc\u7a0b\u8c03\u7528\uff0c\u63d0\u5347\u7cfb\u7edf\u7684\u53ef\u6269\u5c55\u6027\u548c\u53ef\u9760\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u5206\u5e03\u5f0fRPC\u670d\u52a1\u7684\u8bbe\u8ba1\u548c\u5b9e\u73b0\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u57fa\u4e8eTP6 Think-Swoole\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1\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>\u57fa\u4e8eTP6 Think-Swoole\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1 \u968f\u7740\u4e92\u8054\u7f51\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u9700\u6c42\u65e5\u76ca\u589e\u52a0\u3002\u5206\u5e03\u5f0f\u7cfb\u7edf\u53ef\u4ee5\u5c06\u5404\u4e2a\u6a21\u5757\u5206\u5f00\u90e8\u7f72\u5728\u4e0d\u540c\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7684\u53ef\u6269\u5c55\u6027\u548c\u53ef\u9760\u6027\u3002\u800cRPC\uff08Remote Procedure Call\uff09\u4f5c\u4e3a\u4e00\u79cd\u5e38\u7528\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u7684\u8fdc\u7a0b\u8c03\u7528\uff0c\u8fdb\u4e00\u6b65\u4fc3\u8fdb\u4e86\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u53d1\u5c55\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u57fa\u4e8eTP6 Think-Swoole\u6846\u67b6\u8bbe\u8ba1\u4e00\u4e2a\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 1. \u67b6\u6784\u8bbe\u8ba1\u6211\u4eec\u7684\u5206\u5e03\u5f0fRPC\u670d\u52a1\u67b6\u6784\u5c06\u5305\u62ec\u4e09\u4e2a\u4e3b\u8981\u7ec4\u4ef6\uff1a\u670d\u52a1\u63d0\u4f9b\u8005\u3001\u670d\u52a1\u6d88\u8d39\u8005\u548c\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\u3002 \u670d\u52a1\u63d0\u4f9b\u8005\uff1a\u8d1f\u8d23\u66b4\u9732\u670d\u52a1\u63a5\u53e3\uff0c\u63a5\u6536\u5e76\u5904\u7406RPC\u8bf7\u6c42\u3002\u670d\u52a1\u6d88\u8d39\u8005\uff1a\u8d1f\u8d23\u53d1\u8d77RPC\u8bf7\u6c42\uff0c\u5e76\u83b7\u5f97\u670d\u52a1\u63d0\u4f9b\u8005\u7684\u54cd\u5e94\u3002\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\uff1a\u8d1f\u8d23\u7ba1\u7406\u670d\u52a1\u63d0\u4f9b\u8005\u7684\u5730\u5740\u4fe1\u606f\u3002 2. \u5b9e\u73b0\u6b65\u9aa4 \uff081\uff09\u914d\u7f6e\u6587\u4ef6\u9996\u5148\uff0c\u5728TP6\u6846\u67b6\u4e2d\u521b\u5efaconfig\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efarpc.php\u4f5c\u4e3aRPC\u914d\u7f6e\u6587\u4ef6\u3002\u914d\u7f6e\u6587\u4ef6\u4e2d\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a return [ &#8216;server&#8217; =&amp;gt; [ &#8216;host&#8217; =&amp;gt; &#8216;127.0.0.1&#8217;, &#8216;port&#8217; =&amp;gt; 9501, ], &#8216;registry&#8217; =&amp;gt; [ &#8216;host&#8217; =&amp;gt; &#8216;127.0.0.1&#8217;, &#8216;port&#8217; =&amp;gt; 2181, ], ]; \u767b\u5f55\u540e\u590d\u5236 \uff082\uff09\u670d\u52a1\u63d0\u4f9b\u8005\u7aef\u5b9e\u73b0\u5728\u670d\u52a1\u63d0\u4f9b\u8005\u7aef\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aServer\u7c7b\u6765\u5904\u7406RPC\u8bf7\u6c42\uff0c\u5e76\u5c06\u670d\u52a1\u5730\u5740\u6ce8\u518c\u5230\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php namespace apppcserver; use thinkswooleServer; class RpcServer extends Server { protected $rpcService; public function __construct($host, $port) { [&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-24662","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24662","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=24662"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24662\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}