{"id":331419,"date":"2022-04-02T21:00:25","date_gmt":"2022-04-02T21:00:25","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=331419"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=331419","title":{"rendered":"<span>Rest \u0448\u043b\u044e\u0437 \u0434\u043b\u044f gRPC \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043d\u0430 \u0433\u043e \u2014 gRPC-gateway<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442!<\/p>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0433\u0430\u0439\u0434\u043e\u043c \u043f\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044e REST \u043f\u0440\u043e\u043a\u0441\u0438 \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 gRPC \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 gRPC \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 REST API, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u043d\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0432 swagger \u0444\u043e\u0440\u043c\u0430\u0442\u0435.<\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e:<\/p>\n<ul>\n<li>\n<p>\u0423\u043c\u0435\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 go<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b gRPC (proto, \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f, \u0442\u0438\u043f\u044b &#8230;)<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f REST \u0448\u043b\u044e\u0437\u0430 \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e gRPC API \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043d\u0430\u044f (\u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f, \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u0440.), \u043e\u0434\u043d\u0430\u043a\u043e \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 gRPC \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0445\u0432\u0430\u0442\u0430\u0435\u0442.<\/p>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 go gRPC \u0441\u0435\u0440\u0432\u0438\u0441 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e (\u043e\u0442 ~10 \u043c\u0438\u043d\u0443\u0442 \u0434\u043e \u0441\u0443\u0442\u043e\u043a) \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e API, \u0438 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u043f\u0440\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0441\u0442\u0438 REST \u043f\u0440\u043e\u043a\u0441\u0438, \u043e\u0434\u043d\u0430\u043a\u043e \u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e (\u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044f \u0441 \u0440\u0443\u0447\u043d\u044b\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u043e\u0432 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 `\u043f\u0435\u0440\u0435\u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e gRPC`) \u0431\u044b\u0441\u0442\u0440\u043e.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u0438\u0432\u044b\u0445 \u0435\u0441\u0442\u044c <a href=\"https:\/\/github.com\/Dancheg97\/gateway\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u043a\u0441\u0438. \u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u044b\u0439 \u0433\u0430\u0439\u0434 \u043a\u0430\u043a \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0440\u043e\u043a\u0441\u0438 \u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0443.<\/p>\n<h2>1 &#8212; \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b<\/h2>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u043f\u0430\u043a\u0435\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c (\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f go) \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 gateway \u043a\u043e\u0434\u0430 \u0432 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a \u043a\u043e\u0434\u0443 gRPC<\/p>\n<pre><code class=\"go\">\/\/ +build tools package tools import ( _ \"github.com\/grpc-ecosystem\/grpc-gateway\/v2\/protoc-gen-grpc-gateway\" _ \"github.com\/grpc-ecosystem\/grpc-gateway\/v2\/protoc-gen-openapiv2\" _ \"google.golang.org\/grpc\/cmd\/protoc-gen-go-grpc\" _ \"google.golang.org\/protobuf\/cmd\/protoc-gen-go\" )<\/code><\/pre>\n<pre><code class=\"bash\">go get github.com\/grpc-ecosystem\/grpc-gateway\/v2\/protoc-gen-grpc-gateway go get github.com\/grpc-ecosystem\/grpc-gateway\/v2\/protoc-gen-openapiv2 go get google.golang.org\/protobuf\/cmd\/protoc-gen-go go get google.golang.org\/grpc\/cmd\/protoc-gen-go-grpc<\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 <a href=\"https:\/\/github.com\/grpc-ecosystem\/grpc-gateway\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.  <\/p>\n<h2>2 &#8212; \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e (\u0434\u043b\u044f swagger) &#8212; \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c rest \u043e\u043f\u0446\u0438\u0438 \u0432 proto \u0444\u0430\u0439\u043b<\/h2>\n<p><strong>\u0412\u0430\u0436\u043d\u044b\u0439 \u043d\u044e\u0430\u043d\u0441 &#8212;  google\/api\/annotaitons.proto &#8212; \u044d\u0442\u043e \u0444\u0430\u0439\u043b \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u043e\u0442\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u043c\u0435\u0442\u0441\u0435 \u0441 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u043a\u0441\u0438, \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u0430\u0431\u043e\u0447\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u0441 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c proto \u0444\u0430\u0439\u043b\u043e\u043c.<\/strong><\/p>\n<p>\u0412 <a href=\"https:\/\/github.com\/Dancheg97\/gateway\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438<\/a> \u0435\u0441\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u043c\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0438 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0442\u0430\u043c. \u0414\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0438 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u044f \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u043f\u0443\u0442\u0435\u0439 \u043d\u0443\u0436\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0444\u0430\u0439\u043b\u044b:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/112\/71e\/00e\/11271e00ecfcc79f4a12f96844546052.png\" width=\"162\" height=\"89\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/112\/71e\/00e\/11271e00ecfcc79f4a12f96844546052.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412\u043e\u0442 \u0441\u0441\u044b\u043b\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/Dancheg97\/gateway\/blob\/main\/google\/api\/annotations.proto\" rel=\"noopener noreferrer nofollow\">annotations.proto<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/Dancheg97\/gateway\/blob\/main\/google\/api\/http.proto\" rel=\"noopener noreferrer nofollow\">http.proto<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/Dancheg97\/gateway\/blob\/main\/google\/api\/httpbody.proto\" rel=\"noopener noreferrer nofollow\">httpbody.proto<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u043f\u0446\u0438\u0438 \u0432 proto \u0444\u0430\u0439\u043b \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435:<\/p>\n<pre><code class=\"css\">syntax = \"proto3\"; package pb; option go_package = \"\/pb\"; import \"google\/api\/annotations.proto\";  service Gateway {   rpc PostExample(Message) returns (Message) {     option (google.api.http) = {       post: \"\/post\"       body: \"*\"     };   }   rpc GetExample(Message) returns (Message) {     option (google.api.http) = {       get: \"\/get\/{id}\"     };   }   rpc DeleteExample(Message) returns (Message) {     option (google.api.http) = {       delete: \"\/delete\/{id}\"     };   }   rpc PutExample(Message) returns (Message) {     option (google.api.http) = {       put: \"\/put\"       body: \"*\"     };   }   rpc PatchExample(Message) returns (Message) {     option (google.api.http) = {       patch: \"\/patch\"       body: \"*\"     };   } }  message Message {   uint64 id = 1; } <\/code><\/pre>\n<p>\u041a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c <em>rpc <\/em>\u043c\u0435\u0442\u043e\u0434\u0430\u043c \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043e\u043f\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f `\u043f\u0443\u0442\u0438` \u0432 \u0441\u0441\u044b\u043b\u043a\u0430\u0445.<\/p>\n<h2>3 &#8212; \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c rest proxy<\/h2>\n<p>\u0412\u043e\u0442 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<pre><code class=\"bash\">protoc --go_out=. --go-grpc_out=. --grpc-gateway_out=. --grpc-gateway_opt generate_unbound_methods=true --openapiv2_out . api.prorototo<\/code><\/pre>\n<p>\u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/github.com\/grpc-ecosystem\/grpc-gateway\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<h2>4 &#8212; \u041f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u043f\u0440\u043e\u043a\u0441\u0438 \u0441\u0435\u0440\u0432\u0435\u0440<\/h2>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 rest \u0441\u0435\u0440\u0432\u0435\u0440 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a gRPC \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"go\"> func runRest() { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() mux := runtime.NewServeMux() opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} err := pb.RegisterGatewayHandlerFromEndpoint(ctx, mux, \"localhost:12201\", opts) if err != nil { panic(err) } log.Printf(\"server listening at 8081\") if err := http.ListenAndServe(\":8081\", mux); err != nil { panic(err) } }<\/code><\/pre>\n<h2>5 &#8212; \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/h2>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c:<\/p>\n<pre><code class=\"go\"> func main() { go runRest() runGrpc() } <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/77e\/fef\/39a\/77efef39aa5982e7214d3466e88f916b.png\" alt=\" \" title=\" \" width=\"807\" height=\"468\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/77e\/fef\/39a\/77efef39aa5982e7214d3466e88f916b.png\"\/><figcaption> <\/figcaption><\/figure>\n<h2>6 &#8212; \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e &#8212; \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0432 swagger<\/h2>\n<p>\u0411\u0435\u0440\u0435\u043c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <em>api.swagger.json<\/em> \u0438 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432 <a href=\"https:\/\/editor.swagger.io\/\" rel=\"noopener noreferrer nofollow\">swagger editor<\/a>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/179\/9cb\/5e4\/1799cb5e4d43192f8106ecef1725d5c1.png\" width=\"929\" height=\"848\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/179\/9cb\/5e4\/1799cb5e4d43192f8106ecef1725d5c1.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0445\u043e\u0440\u043e\u0448\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 <em>openapi.<\/em><\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0430\u0448\u0435\u043c\u0443 gRPC \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043c\u0430\u043b\u043e\u0439 \u043a\u0440\u043e\u0432\u044c\u044e (\u043f\u0440\u043e\u0449\u0435 \u0447\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u044b), \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0432 \u0440\u044f\u0434\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442: <a href=\"https:\/\/github.com\/grpc-ecosystem\/grpc-gateway\" rel=\"noopener noreferrer nofollow\">grpc-gateway<\/a><\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c: <a href=\"https:\/\/github.com\/Dancheg97\/gateway\" rel=\"noopener noreferrer nofollow\">gateway<\/a><\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 go:<\/p>\n<pre><code class=\"go\">package main  import ( \"context\" \"fmt\" \"log\" \"net\" \"net\/http\"  \"gateway\/pb\"  \"github.com\/grpc-ecosystem\/grpc-gateway\/v2\/runtime\" \"google.golang.org\/grpc\" \"google.golang.org\/grpc\/credentials\/insecure\" )    type server struct { pb.UnimplementedGatewayServer }  func (s *server) PostExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func (s *server) GetExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func (s *server) DeleteExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func (s *server) PutExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func (s *server) PatchExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func runRest() { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() mux := runtime.NewServeMux() opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} err := pb.RegisterGatewayHandlerFromEndpoint(ctx, mux, \"localhost:12201\", opts) if err != nil { panic(err) } log.Printf(\"server listening at 8081\") if err := http.ListenAndServe(\":8081\", mux); err != nil { panic(err) } }  func runGrpc() { lis, err := net.Listen(\"tcp\", \":12201\") if err != nil { log.Fatalf(\"failed to listen: %v\", err) } s := grpc.NewServer() pb.RegisterGatewayServer(s, &amp;server{}) log.Printf(\"server listening at %v\", lis.Addr()) if err := s.Serve(lis); err != nil { panic(err) } }  func main() { go runRest() runGrpc() } <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/658769\/\"> https:\/\/habr.com\/ru\/post\/658769\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442!<\/p>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0433\u0430\u0439\u0434\u043e\u043c \u043f\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044e REST \u043f\u0440\u043e\u043a\u0441\u0438 \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 gRPC \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 gRPC \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 REST API, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u043d\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0432 swagger \u0444\u043e\u0440\u043c\u0430\u0442\u0435.<\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e:<\/p>\n<ul>\n<li>\n<p>\u0423\u043c\u0435\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 go<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b gRPC (proto, \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f, \u0442\u0438\u043f\u044b &#8230;)<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f REST \u0448\u043b\u044e\u0437\u0430 \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e gRPC API \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043d\u0430\u044f (\u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f, \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u0440.), \u043e\u0434\u043d\u0430\u043a\u043e \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 gRPC \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0445\u0432\u0430\u0442\u0430\u0435\u0442.<\/p>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 go gRPC \u0441\u0435\u0440\u0432\u0438\u0441 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e (\u043e\u0442 ~10 \u043c\u0438\u043d\u0443\u0442 \u0434\u043e \u0441\u0443\u0442\u043e\u043a) \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e API, \u0438 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u043f\u0440\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0441\u0442\u0438 REST \u043f\u0440\u043e\u043a\u0441\u0438, \u043e\u0434\u043d\u0430\u043a\u043e \u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e (\u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044f \u0441 \u0440\u0443\u0447\u043d\u044b\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u043e\u0432 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 `\u043f\u0435\u0440\u0435\u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e gRPC`) \u0431\u044b\u0441\u0442\u0440\u043e.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u0438\u0432\u044b\u0445 \u0435\u0441\u0442\u044c <a href=\"https:\/\/github.com\/Dancheg97\/gateway\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u043a\u0441\u0438. \u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u044b\u0439 \u0433\u0430\u0439\u0434 \u043a\u0430\u043a \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0440\u043e\u043a\u0441\u0438 \u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0443.<\/p>\n<h2>1 &#8212; \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b<\/h2>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u043f\u0430\u043a\u0435\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c (\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f go) \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 gateway \u043a\u043e\u0434\u0430 \u0432 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a \u043a\u043e\u0434\u0443 gRPC<\/p>\n<pre><code class=\"go\">\/\/ +build tools package tools import ( _ \"github.com\/grpc-ecosystem\/grpc-gateway\/v2\/protoc-gen-grpc-gateway\" _ \"github.com\/grpc-ecosystem\/grpc-gateway\/v2\/protoc-gen-openapiv2\" _ \"google.golang.org\/grpc\/cmd\/protoc-gen-go-grpc\" _ \"google.golang.org\/protobuf\/cmd\/protoc-gen-go\" )<\/code><\/pre>\n<pre><code class=\"bash\">go get github.com\/grpc-ecosystem\/grpc-gateway\/v2\/protoc-gen-grpc-gateway go get github.com\/grpc-ecosystem\/grpc-gateway\/v2\/protoc-gen-openapiv2 go get google.golang.org\/protobuf\/cmd\/protoc-gen-go go get google.golang.org\/grpc\/cmd\/protoc-gen-go-grpc<\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 <a href=\"https:\/\/github.com\/grpc-ecosystem\/grpc-gateway\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.  <\/p>\n<h2>2 &#8212; \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e (\u0434\u043b\u044f swagger) &#8212; \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c rest \u043e\u043f\u0446\u0438\u0438 \u0432 proto \u0444\u0430\u0439\u043b<\/h2>\n<p><strong>\u0412\u0430\u0436\u043d\u044b\u0439 \u043d\u044e\u0430\u043d\u0441 &#8212;  google\/api\/annotaitons.proto &#8212; \u044d\u0442\u043e \u0444\u0430\u0439\u043b \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u043e\u0442\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u043c\u0435\u0442\u0441\u0435 \u0441 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u043a\u0441\u0438, \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u0430\u0431\u043e\u0447\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u0441 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c proto \u0444\u0430\u0439\u043b\u043e\u043c.<\/strong><\/p>\n<p>\u0412 <a href=\"https:\/\/github.com\/Dancheg97\/gateway\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438<\/a> \u0435\u0441\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u043c\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0438 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0442\u0430\u043c. \u0414\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0438 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u044f \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u043f\u0443\u0442\u0435\u0439 \u043d\u0443\u0436\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0444\u0430\u0439\u043b\u044b:<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u043e\u0442 \u0441\u0441\u044b\u043b\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/Dancheg97\/gateway\/blob\/main\/google\/api\/annotations.proto\" rel=\"noopener noreferrer nofollow\">annotations.proto<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/Dancheg97\/gateway\/blob\/main\/google\/api\/http.proto\" rel=\"noopener noreferrer nofollow\">http.proto<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/Dancheg97\/gateway\/blob\/main\/google\/api\/httpbody.proto\" rel=\"noopener noreferrer nofollow\">httpbody.proto<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u043f\u0446\u0438\u0438 \u0432 proto \u0444\u0430\u0439\u043b \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435:<\/p>\n<pre><code class=\"css\">syntax = \"proto3\"; package pb; option go_package = \"\/pb\"; import \"google\/api\/annotations.proto\";  service Gateway {   rpc PostExample(Message) returns (Message) {     option (google.api.http) = {       post: \"\/post\"       body: \"*\"     };   }   rpc GetExample(Message) returns (Message) {     option (google.api.http) = {       get: \"\/get\/{id}\"     };   }   rpc DeleteExample(Message) returns (Message) {     option (google.api.http) = {       delete: \"\/delete\/{id}\"     };   }   rpc PutExample(Message) returns (Message) {     option (google.api.http) = {       put: \"\/put\"       body: \"*\"     };   }   rpc PatchExample(Message) returns (Message) {     option (google.api.http) = {       patch: \"\/patch\"       body: \"*\"     };   } }  message Message {   uint64 id = 1; } <\/code><\/pre>\n<p>\u041a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c <em>rpc <\/em>\u043c\u0435\u0442\u043e\u0434\u0430\u043c \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043e\u043f\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f `\u043f\u0443\u0442\u0438` \u0432 \u0441\u0441\u044b\u043b\u043a\u0430\u0445.<\/p>\n<h2>3 &#8212; \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c rest proxy<\/h2>\n<p>\u0412\u043e\u0442 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<pre><code class=\"bash\">protoc --go_out=. --go-grpc_out=. --grpc-gateway_out=. --grpc-gateway_opt generate_unbound_methods=true --openapiv2_out . api.prorototo<\/code><\/pre>\n<p>\u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/github.com\/grpc-ecosystem\/grpc-gateway\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<h2>4 &#8212; \u041f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u043f\u0440\u043e\u043a\u0441\u0438 \u0441\u0435\u0440\u0432\u0435\u0440<\/h2>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 rest \u0441\u0435\u0440\u0432\u0435\u0440 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a gRPC \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"go\"> func runRest() { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() mux := runtime.NewServeMux() opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} err := pb.RegisterGatewayHandlerFromEndpoint(ctx, mux, \"localhost:12201\", opts) if err != nil { panic(err) } log.Printf(\"server listening at 8081\") if err := http.ListenAndServe(\":8081\", mux); err != nil { panic(err) } }<\/code><\/pre>\n<h2>5 &#8212; \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/h2>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c:<\/p>\n<pre><code class=\"go\"> func main() { go runRest() runGrpc() } <\/code><\/pre>\n<figure class=\"full-width\"><figcaption> <\/figcaption><\/figure>\n<h2>6 &#8212; \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e &#8212; \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0432 swagger<\/h2>\n<p>\u0411\u0435\u0440\u0435\u043c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <em>api.swagger.json<\/em> \u0438 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432 <a href=\"https:\/\/editor.swagger.io\/\" rel=\"noopener noreferrer nofollow\">swagger editor<\/a>.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0445\u043e\u0440\u043e\u0448\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 <em>openapi.<\/em><\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0430\u0448\u0435\u043c\u0443 gRPC \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043c\u0430\u043b\u043e\u0439 \u043a\u0440\u043e\u0432\u044c\u044e (\u043f\u0440\u043e\u0449\u0435 \u0447\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u044b), \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0432 \u0440\u044f\u0434\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442: <a href=\"https:\/\/github.com\/grpc-ecosystem\/grpc-gateway\" rel=\"noopener noreferrer nofollow\">grpc-gateway<\/a><\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c: <a href=\"https:\/\/github.com\/Dancheg97\/gateway\" rel=\"noopener noreferrer nofollow\">gateway<\/a><\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 go:<\/p>\n<pre><code class=\"go\">package main  import ( \"context\" \"fmt\" \"log\" \"net\" \"net\/http\"  \"gateway\/pb\"  \"github.com\/grpc-ecosystem\/grpc-gateway\/v2\/runtime\" \"google.golang.org\/grpc\" \"google.golang.org\/grpc\/credentials\/insecure\" )    type server struct { pb.UnimplementedGatewayServer }  func (s *server) PostExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func (s *server) GetExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func (s *server) DeleteExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func (s *server) PutExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func (s *server) PatchExample(ctx context.Context, in *pb.Message) (*pb.Message, error) { fmt.Println(in) return &amp;pb.Message{Id: in.Id}, nil }  func runRest() { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() mux := runtime.NewServeMux() opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} err := pb.RegisterGatewayHandlerFromEndpoint(ctx, mux, \"localhost:12201\", opts) if err != nil { panic(err) } log.Printf(\"server listening at 8081\") if err := http.ListenAndServe(\":8081\", mux); err != nil { panic(err) } }  func runGrpc() { lis, err := net.Listen(\"tcp\", \":12201\") if err != nil { log.Fatalf(\"failed to listen: %v\", err) } s := grpc.NewServer() pb.RegisterGatewayServer(s, &amp;server{}) log.Printf(\"server listening at %v\", lis.Addr()) if err := s.Serve(lis); err != nil { panic(err) } }  func main() { go runRest() runGrpc() } <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/658769\/\"> https:\/\/habr.com\/ru\/post\/658769\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-331419","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=331419"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331419\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}