{"id":207104,"date":"2025-07-08T08:44:58","date_gmt":"2025-07-08T00:44:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207104\/"},"modified":"2025-07-08T08:44:58","modified_gmt":"2025-07-08T00:44:58","slug":"%e6%97%a0%e6%b3%95%e5%88%b6%e4%bd%9c-grpc-gateway-gw-pb-%e5%b9%b6%e4%b8%94%e6%b2%a1%e6%9c%89%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207104\/","title":{"rendered":"\u65e0\u6cd5\u5236\u4f5c grpc-gateway.gw.pb \u5e76\u4e14\u6ca1\u6709\u9519\u8bef"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u65e0\u6cd5\u5236\u4f5c grpc-gateway.gw.pb \u5e76\u4e14\u6ca1\u6709\u9519\u8bef<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 08:15:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/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\">\u300a\u65e0\u6cd5\u5236\u4f5c grpc-gateway.gw.pb \u5e76\u4e14\u6ca1\u6709\u9519\u8bef\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> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u7f16\u5199\u4e86\u8fd9\u4e2a\u7b80\u5355\u7684 go \u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\uff0c\u5ba2\u6237\u7aef\u53d1\u9001\u4e24\u4e2a\u6570\u5b57\uff0c\u670d\u52a1\u5668\u56de\u590d\u603b\u548c\uff0c\u5e76\u4e14\u5b83\u53ef\u4ee5\u5de5\u4f5c\u3002\u73b0\u5728\u6211\u5c1d\u8bd5\u4f7f\u7528 grpc api \u914d\u7f6e\u6765\u8bbe\u7f6e grpc-gateway \u5e76\u5c06\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u4ece grpc \u66f4\u6539\u4e3a\u4f11\u606f\u3002<\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528\u672c\u6559\u7a0b\uff0c\u5728\u4e0b\u9762\u7684\u90e8\u5206\u4e2d\uff0c\u6211\u65e0\u6cd5\u521b\u5efa gw\uff0c\u4f46\u6ca1\u6709\u9519\u8bef\uff1a<\/p>\n<pre>protoc -i\/usr\/local\/include -i. \\\n  -i$gopath\/src \\\n  -i$gopath\/src\/github.com\/grpc-ecosystem\/grpc-gateway\/third_party\/googleapis \\\n  --grpc-gateway_out=logtostderr=true,grpc_api_configuration=path\/to\/your_service.yaml:. \\\n  path\/to\/your_service.proto\n<\/pre>\n<p>\u6211\u7528\u7684\u662f\u8fd9\u4e2a\uff1a<\/p>\n<pre>protoc -i\/usr\/local\/include -i.   -i$gopath\/src   -i$gopath\/src\/github.com\/grpc-ecosystem\/grpc-gateway\/third_party\/googleapis --grpc-gateway_out=logtostderr=true,grpc_api_configuration=$gopath\/src\/grpc-test\/sum.yaml:. .\/sum.proto\n<\/pre>\n<p>\u5728\u8fd9\u4e2a\u95ee\u9898\u4e4b\u540e\uff0c\u6211\u8fdb\u884c\u4e86\u641c\u7d22\uff0c\u53d1\u73b0\u4e86\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f46\u5b83\u4e0d\u8d77\u4f5c\u7528\uff08\u6ca1\u6709\u9519\u8bef\uff0c\u4e5f\u6ca1\u6709\u8f93\u51fa\uff01\uff09\uff1a<\/p>\n<pre>protoc -i\/usr\/local\/include -i. -i$gopath\/src -i$gopath\/src\/github.com\/grpc-ecosystem\/grpc-gateway\/third_party\/googleapis --plugin=protoc-gen-grpc-gateway=$gopath\/bin\/protoc-gen-grpc-gateway  --grpc-gateway_out=logtostderr=true,grpc_api_configuration=sum.yaml:. .\/sum.proto\n\n<\/pre>\n<p>\u6211\u5728 grpc-test \u76ee\u5f55\u4e2d\u8fd0\u884c\u5b83\uff0c\u8be5\u76ee\u5f55\u5177\u6709\u4ee5\u4e0b\u6811\uff1a<\/p>\n<p>\u90a3\u4e48\uff0c\u6211\u505a\u9519\u4e86\u4ec0\u4e48\uff1f<\/p>\n<p>\u7f16\u8f91\uff1a \u8fd9\u662f\u6211\u7684 sum.yaml\uff1a<\/p>\n<pre>type: google.api.service\nconfig_version: 3\n\nhttp:\n  rules:\n    - selector: example.yourservice.echo\n      post: \/v1\/example\/echo\n      body: \"*\"<\/pre>\n<p>\u8fd9\u662f sum.proto\uff1a<\/p>\n<pre>syntax = \"proto3\";\n\n\nservice ComputeSum {\n    rpc ComputeSum (SumRequest) returns (ResultReply) {\n    }\n}\n\nmessage SumRequest {\n    int32 firstOperand = 1;\n    int32 secondOperand = 2;\n}\n\nmessage ResultReply {\n    int32 result = 1;\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u95ee\u9898\u51fa\u5728\u9009\u62e9\u5668\u4e0a\uff0c\u5b83\u5e94\u8be5\u662f<code>ComputeSum.ComputeSum<\/code>\u3002 \u6211\u5728 grpc-channel \u4e2d\u63d0\u51fa\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0cJohan Brandhorst \u5e2e\u52a9\u4e86\u6211\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u65e0\u6cd5\u5236\u4f5c grpc-gateway.gw.pb \u5e76\u4e14\u6ca1\u6709\u9519\u8bef\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207104","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207104"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207104\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}