{"id":48306,"date":"2024-12-02T17:38:28","date_gmt":"2024-12-02T09:38:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48306\/"},"modified":"2024-12-02T17:38:28","modified_gmt":"2024-12-02T09:38:28","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-grpc-%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48306\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1\uff1a\u5b89\u88c5 PHP gRPC \u5e93\u3002\u5b9a\u4e49 proto3 \u6d88\u606f\u3002\u4f7f\u7528 protoc \u547d\u4ee4\u751f\u6210 PHP \u7c7b\u3002\u7f16\u5199 PHP \u51fd\u6570\u548c gRPC \u670d\u52a1\u3002\u542f\u52a8 gRPC \u670d\u52a1\u5668\u3002\u4f7f\u7528 gRPC \u5ba2\u6237\u7aef\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730011839671de2bfcbe13.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 PHP \u51fd\u6570\u901a\u8fc7 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>gRPC \u662f\u4e00\u79cd\u9ad8\u6027\u80fd\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff08RPC\uff09\u6846\u67b6\uff0c\u53ef\u7528\u4e8e\u5728\u4e0d\u540c\u670d\u52a1\u6216\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u3002PHP \u63d0\u4f9b\u4e86 gRPC \u5e93\uff0c\u5141\u8bb8\u60a8\u8f7b\u677e\u5730\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1\u3002<\/p>\n<p><strong>\u5b89\u88c5 gRPC \u5e93<\/strong><\/p>\n<p>\u8981\u4f7f\u7528 PHP gRPC \u5e93\uff0c\u60a8\u9700\u8981\u5148\u5b89\u88c5\u5b83\uff1a<\/p>\n<pre>composer require grpc\/grpc<\/pre>\n<p><strong>\u521b\u5efa PHP \u51fd\u6570<\/strong><\/p>\n<p>\u60a8\u7684 PHP \u51fd\u6570\u5e94\u8be5\u662f\u4e00\u4e2a\u666e\u901a\u7684 PHP \u51fd\u6570\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a proto3 \u6d88\u606f\u4f5c\u4e3a\u8f93\u5165\u5e76\u8fd4\u56de\u4e00\u4e2a proto3 \u6d88\u606f\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>function echoMessage($request)\n{\n    return $request;\n}<\/pre>\n<p><strong>\u521b\u5efa gRPC \u670d\u52a1<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a gRPC \u670d\u52a1\u7c7b\uff0c\u8be5\u7c7b\u5c06\u5305\u542b\u60a8\u7684 PHP \u51fd\u6570\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>class EchoService extends PBServices\\Echo\\EchoServicer\n{\n    public function EchoMessage(PBServices\\Echo\\EchoMessage $request, gRPC\\Streaming\\ServerCallContext $context): PBServices\\Echo\\EchoMessage\n    {\n        return echoMessage($request);\n    }\n}<\/pre>\n<p><strong>\u6ce8\u518c gRPC \u670d\u52a1<\/strong><\/p>\n<p>\u5c06\u60a8\u7684 gRPC \u670d\u52a1\u6ce8\u518c\u5230 gRPC \u670d\u52a1\u5668\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>$server = new gRPC\\Server();\n$server-&gt;addService(new EchoService());\n$server-&gt;start();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 gRPC \u670d\u52a1\uff0c\u8be5\u670d\u52a1\u5c06\u901a\u8fc7 gRPC \u8c03\u7528 echo \u4e00\u4e2a\u6d88\u606f\u3002<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u5b9a\u4e49 proto3 \u6d88\u606f<\/strong><\/p>\n<p>\u5b9a\u4e49\u4e00\u4e2a proto3 \u6d88\u606f\u6765\u8868\u793a\u6211\u4eec\u8981\u901a\u8fc7 gRPC \u4f20\u9012\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>syntax = \"proto3\";\n\npackage PServices.Echo;\n\nmessage EchoMessage {\n  string message = 1;\n}<\/pre>\n<p><strong>\u6b65\u9aa4 2\uff1a\u751f\u6210 PHP \u7c7b<\/strong><\/p>\n<p>\u4f7f\u7528 protoc \u547d\u4ee4\u751f\u6210 PHP \u7c7b\uff1a<\/p>\n<pre>protoc --php_out=. echo.proto<\/pre>\n<p><strong>\u6b65\u9aa4 3\uff1a\u7f16\u5199 PHP \u51fd\u6570\u548c gRPC \u670d\u52a1<\/strong><\/p>\n<p>\u6b63\u5982\u524d\u9762\u6240\u8ff0\uff0c\u7f16\u5199\u60a8\u7684 PHP \u51fd\u6570\u548c gRPC \u670d\u52a1\u3002<\/p>\n<p><strong>\u6b65\u9aa4 4\uff1a\u542f\u52a8 gRPC \u670d\u52a1\u5668<\/strong><\/p>\n<p>\u542f\u52a8 gRPC \u670d\u52a1\u5668\u4ee5\u4fa6\u542c\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002<\/p>\n<p><strong>\u6b65\u9aa4 5\uff1a\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1<\/strong><\/p>\n<p>\u4f7f\u7528 gRPC \u5ba2\u6237\u7aef\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>$client = new PBServices\\Echo\\EchoClient();\n$request = new PBServices\\Echo\\EchoMessage();\n$request-&gt;setMessage(\"Hello world!\");\n$response = $client-&gt;EchoMessage($request);\necho $response-&gt;getMessage();<\/pre>\n<p>\u901a\u8fc7\u9075\u5faa\u6b64\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 PHP \u51fd\u6570\u8f7b\u677e\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 \u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1\uff1a\u5b89\u88c5 PHP gRPC \u5e93\u3002\u5b9a\u4e49 proto3 \u6d88\u606f\u3002\u4f7f\u7528 protoc \u547d\u4ee4\u751f\u6210 PHP \u7c7b\u3002\u7f16\u5199 PHP \u51fd\u6570\u548c gRPC \u670d\u52a1\u3002\u542f\u52a8 gRPC \u670d\u52a1\u5668\u3002\u4f7f\u7528 gRPC \u5ba2\u6237\u7aef\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1\u3002 \u4f7f\u7528 PHP \u51fd\u6570\u901a\u8fc7 gRPC \u8c03\u7528\u5916\u90e8\u51fd\u6570 \u524d\u8a00 gRPC \u662f\u4e00\u79cd\u9ad8\u6027\u80fd\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff08RPC\uff09\u6846\u67b6\uff0c\u53ef\u7528\u4e8e\u5728\u4e0d\u540c\u670d\u52a1\u6216\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u3002PHP \u63d0\u4f9b\u4e86 gRPC \u5e93\uff0c\u5141\u8bb8\u60a8\u8f7b\u677e\u5730\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8 gRPC \u670d\u52a1\u3002 \u5b89\u88c5 gRPC \u5e93 \u8981\u4f7f\u7528 PHP gRPC \u5e93\uff0c\u60a8\u9700\u8981\u5148\u5b89\u88c5\u5b83\uff1a composer [&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-48306","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48306","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=48306"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48306\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}