34

gRPC Gateway

 5 years ago
source link: https://blog.lizebang.top/2018/10/grpc-gateway/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本文将向你介绍 gRPC Gateway。代码示例: demo

简介

grpc-gateway 是一个 protoc 的插件。它读取 gRPC 服务定义,生成反向代理服务器,将 RESTful JSON API 翻译成 gRPC。

grpc-gateway.png

安装

源码编译安装 Protocol Buffers v3

mkdir tmp
cd tmp
git clone https://github.com/google/protobuf
cd protobuf
./autogen.sh
./configure
make
make check
sudo make install

安装所需的 protoc 插件

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go

使用

grpc-gateway 有两种使用方式:

  • 注解
  • 配置

原服务

例如我有下面这样一个 gRPC 服务。

syntax = "proto3";
package example;
message StringMessage {
  string value = 1;
}

service YourService {
  rpc Echo(StringMessage) returns (StringMessage) {}
}

使用注解

按照 +/- 提示修改 .proto 文件,给服务添加一个 option。

syntax = "proto3";
 package example;
+
+import "google/api/annotations.proto";
+
 message StringMessage {
   string value = 1;
 }

 service YourService {
-  rpc Echo(StringMessage) returns (StringMessage) {}
+  rpc Echo(StringMessage) returns (StringMessage) {
+    option (google.api.http) = {
+      post: "/v1/example/echo"
+      body: "*"
+    };
+  }
 }

使用配置

保持 .proto 文件不变,新建一个 your_service.yaml 写入服务相关配置。

type: google.api.Service
config_version: 3

http:
  rules:
    - selector: example.YourService.Echo
      post: /v1/example/echo
      body: '*'

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK