{"id":42345,"date":"2024-12-01T14:22:55","date_gmt":"2024-12-01T06:22:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42345\/"},"modified":"2024-12-01T14:22:55","modified_gmt":"2024-12-01T06:22:55","slug":"grpc%ef%bc%9a%e5%9f%ba%e4%ba%8e%e6%af%8f%e4%b8%aa-rpc-%e9%99%90%e5%88%b6-api-%e9%80%9f%e7%8e%87","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42345\/","title":{"rendered":"gRPC\uff1a\u57fa\u4e8e\u6bcf\u4e2a RPC \u9650\u5236 API \u901f\u7387"},"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>gRPC\uff1a\u57fa\u4e8e\u6bcf\u4e2a RPC \u9650\u5236 API \u901f\u7387<\/span><\/p>\n<h1>gRPC\uff1a\u57fa\u4e8e\u6bcf\u4e2a RPC \u9650\u5236 API \u901f\u7387<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 16:48:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300agRPC\uff1a\u57fa\u4e8e\u6bcf\u4e2a RPC \u9650\u5236 API \u901f\u7387\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5bfb\u627e\u4e00\u79cd\u4ee5\u9ad8\u7c92\u5ea6\u5355\u72ec\u9650\u5236 RPC \u901f\u7387\u7684\u65b9\u6cd5\uff0c\u4ee4\u6211\u6cae\u4e27\u7684\u662f\uff0c\u9488\u5bf9\u6b64\u95ee\u9898\u53ef\u7528\u7684\u9009\u9879\u5e76\u4e0d\u591a\u3002\u6211\u6b63\u5728\u5c1d\u8bd5\u7528 gRPC \u66ff\u6362 REST API\uff0c\u5bf9\u6211\u6765\u8bf4\u6700\u91cd\u8981\u7684\u529f\u80fd\u4e4b\u4e00\u662f\u80fd\u591f\u4e3a\u6bcf\u4e2a\u8def\u7531\u6dfb\u52a0\u4e2d\u95f4\u4ef6\u3002\u4e0d\u5e78\u7684\u662f\uff0cgo-grpc-middleware \u53ea\u5c06\u4e2d\u95f4\u4ef6\u5e94\u7528\u4e8e\u6574\u4e2a\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5728\u6211\u7684\u60f3\u8c61\u4e2d\uff0cgRPC \u7684\u7406\u60f3\u901f\u7387\u9650\u5236\u4e2d\u95f4\u4ef6\u5c06\u4f7f\u7528\u4e0e go-proto-validators \u7c7b\u4f3c\u7684\u6280\u5de7\uff0c\u5176\u4e2d proto \u6587\u4ef6\u5c06\u5305\u542b\u901f\u7387\u9650\u5236\u672c\u8eab\u7684\u914d\u7f6e\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u60f3\u6211\u53ef\u4ee5\u4f7f\u7528  <code>withunaryserverchain<\/code> \u548c\u4e00\u5143\u62e6\u622a\u5668\u53d1\u5e03\u4e00\u4e2a\u7247\u6bb5\u4ee5\u4f9b\u53c2\u8003\uff0c\u4ee5\u4e86\u89e3\u5b9e\u9645\u60c5\u51b5\u3002<\/p>\n<p>\u8fd9\u4e2a\u60f3\u6cd5\u662f\u5c06 <code>grpc.unaryinterceptor<\/code> \u6dfb\u52a0\u5230\u670d\u52a1\u5668\uff0c\u5b83\u5c06\u901a\u8fc7 <code>*grpc.unaryserverinfo<\/code> \u7684\u5b9e\u4f8b\u8fdb\u884c\u8c03\u7528\u3002\u8be5\u5bf9\u8c61\u5bfc\u51fa\u5b57\u6bb5 <code>fullmethod<\/code>\uff0c\u8be5\u5b57\u6bb5\u4fdd\u5b58\u6b63\u5728\u8c03\u7528\u7684 rpc \u65b9\u6cd5\u7684\u9650\u5b9a\u540d\u79f0\u3002<\/p>\n<p>\u5728\u62e6\u622a\u5668\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u5b9e\u9645\u8c03\u7528 rpc \u5904\u7406\u7a0b\u5e8f\u4e4b\u524d\u5b9e\u73b0\u4efb\u610f\u4ee3\u7801\uff0c\u5305\u62ec rpc \u7279\u5b9a\u7684\u901f\u7387\u9650\u5236\u903b\u8f91\u3002<\/p>\n<pre>\/\/ import grpc_middleware \"github.com\/grpc-ecosystem\/go-grpc-middleware\"\n\/\/ import \"google.golang.org\/grpc\"\n\n    grpcServer := grpc.NewServer(\n        \/\/ WithUnaryServerChain is a grpc.Server config option that accepts multiple unary interceptors.\n        grpc_middleware.WithUnaryServerChain(\n            \/\/ UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info\n            \/\/ contains all the information of this RPC the interceptor can operate on. And handler is the wrapper\n            \/\/ of the service method implementation. It is the responsibility of the interceptor to invoke handler\n            \/\/ to complete the RPC.\n            grpc.UnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {\n                \/\/ FullMethod is the full RPC method string, i.e., \/package.service\/method.\n                switch info.FullMethod {\n                case \"\/mypackage.someservice\/DoThings\":\n                    \/\/ ... rate limiting code\n                    \/\/ if all is good, then call the handler\n                    return handler(ctx, req)\n                }\n            }),\n            \/\/ other `grpc.ServerOption` opts\n        ),\n    )<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; gRPC\uff1a\u57fa\u4e8e\u6bcf\u4e2a RPC \u9650\u5236 API \u901f\u7387 gRPC\uff1a\u57fa\u4e8e\u6bcf\u4e2a RPC \u9650\u5236 API \u901f\u7387 \u6765\u6e90\uff1astackoverflow 2024-04-23 16:48:33 0\u6d4f\u89c8 \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300agRPC\uff1a\u57fa\u4e8e\u6bcf\u4e2a RPC \u9650\u5236 API \u901f\u7387\u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591aGolang\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~ \u95ee\u9898\u5185\u5bb9 \u6211\u6b63\u5728\u5bfb\u627e\u4e00\u79cd\u4ee5\u9ad8\u7c92\u5ea6\u5355\u72ec\u9650\u5236 RPC \u901f\u7387\u7684\u65b9\u6cd5\uff0c\u4ee4\u6211\u6cae\u4e27\u7684\u662f\uff0c\u9488\u5bf9\u6b64\u95ee\u9898\u53ef\u7528\u7684\u9009\u9879\u5e76\u4e0d\u591a\u3002\u6211\u6b63\u5728\u5c1d\u8bd5\u7528 gRPC \u66ff\u6362 REST API\uff0c\u5bf9\u6211\u6765\u8bf4\u6700\u91cd\u8981\u7684\u529f\u80fd\u4e4b\u4e00\u662f\u80fd\u591f\u4e3a\u6bcf\u4e2a\u8def\u7531\u6dfb\u52a0\u4e2d\u95f4\u4ef6\u3002\u4e0d\u5e78\u7684\u662f\uff0cgo-grpc-middleware \u53ea\u5c06\u4e2d\u95f4\u4ef6\u5e94\u7528\u4e8e\u6574\u4e2a\u670d\u52a1\u5668\u3002 \u5728\u6211\u7684\u60f3\u8c61\u4e2d\uff0cgRPC \u7684\u7406\u60f3\u901f\u7387\u9650\u5236\u4e2d\u95f4\u4ef6\u5c06\u4f7f\u7528\u4e0e go-proto-validators \u7c7b\u4f3c\u7684\u6280\u5de7\uff0c\u5176\u4e2d proto \u6587\u4ef6\u5c06\u5305\u542b\u901f\u7387\u9650\u5236\u672c\u8eab\u7684\u914d\u7f6e\u3002 \u89e3\u51b3\u65b9\u6848 \u6211\u60f3\u6211\u53ef\u4ee5\u4f7f\u7528 withunaryserverchain \u548c\u4e00\u5143\u62e6\u622a\u5668\u53d1\u5e03\u4e00\u4e2a\u7247\u6bb5\u4ee5\u4f9b\u53c2\u8003\uff0c\u4ee5\u4e86\u89e3\u5b9e\u9645\u60c5\u51b5\u3002 \u8fd9\u4e2a\u60f3\u6cd5\u662f\u5c06 grpc.unaryinterceptor \u6dfb\u52a0\u5230\u670d\u52a1\u5668\uff0c\u5b83\u5c06\u901a\u8fc7 *grpc.unaryserverinfo \u7684\u5b9e\u4f8b\u8fdb\u884c\u8c03\u7528\u3002\u8be5\u5bf9\u8c61\u5bfc\u51fa\u5b57\u6bb5 fullmethod\uff0c\u8be5\u5b57\u6bb5\u4fdd\u5b58\u6b63\u5728\u8c03\u7528\u7684 rpc \u65b9\u6cd5\u7684\u9650\u5b9a\u540d\u79f0\u3002 \u5728\u62e6\u622a\u5668\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u5b9e\u9645\u8c03\u7528 rpc \u5904\u7406\u7a0b\u5e8f\u4e4b\u524d\u5b9e\u73b0\u4efb\u610f\u4ee3\u7801\uff0c\u5305\u62ec [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42345","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42345","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=42345"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42345\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}