Go Grpc Gateway兼容HTTP协议文档自动生成网关

作者:文振熙 时间:2024-05-21 10:27:16 

前言

调用,让客户端可以更具自身情况自由选择,服务端工作只需要做一份呢?还别说真还有一个准备好的轮子那就是今天的主角《grpc-gateway》。

附上:

博文实例demo:https://github.com/sunmi-OS/grpc-gateway-demo

grpc-gateway官网:https://github.com/grpc-ecosystem/grpc-gateway

一,grpc-gateway介绍

grpc-gateway是protoc的一个插件 。它读取Grpc服务定义,并生成反向代理服务器,将RESTful JSON API请求转换为Grpc的方式调用。主要是根据 google.api.http定义中思想完成的,一下就是grpc-gateway结构图:

Go Grpc Gateway兼容HTTP协议文档自动生成网关

二,grpc-gateway环境准备

grpc-gateway使用完全的Go语言进行开发,所以安装起来也非常简单,首先需要获取相关的依赖包

PS:需要先准备好准备好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
cd $GOPATH/src/
mkdir -p grpc-gateway-demo/gateway
cd grpc-gateway-demo/gateway
vim gateway.proto
syntax = "proto3";
package gateway;
# 新增以下引入
import "google/api/annotations.proto";
message StringMessage {
   string value = 1;
}
# 修改方法增加http定义
# service Gateway {
#   rpc SayHello Echo(StringMessage) returns (StringMessage) {}
# }
service Gateway {
  rpc Echo(StringMessage) returns (StringMessage) {
      option (google.api.http) = {
          post: "/v1/example/echo"
          body: "*"
      };
  }
}

生成grpc结构文件和gateway文件:

protoc --proto_path=../ -I/usr/local/include -I. -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis --go_out=plugins=grpc:. gateway.proto

protoc --proto_path=../ -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:. gateway.proto

最终可以看到以下文件

Go Grpc Gateway兼容HTTP协议文档自动生成网关

二,编写grpc-gateway服务

服务端代码:

cd ..
vim grpc_service.go
package main
import (
   "log"
   "net"
   pb "grpc-gateway-demo/gateway"
   "google.golang.org/grpc"
   "golang.org/x/net/context"
)
const (
   PORT = ":9192"
)
type server struct {}
func (s *server) Echo(ctx context.Context, in *pb.StringMessage) (*pb.StringMessage, error) {
   log.Println("request: ", in.Value)
   return &pb.StringMessage{Value: "Hello " + in.Value}, nil
}
func main() {
   lis, err := net.Listen("tcp", PORT)
   if err != nil {
       log.Fatalf("failed to listen: %v", err)
   }
   s := grpc.NewServer()
   pb.RegisterGatewayServer(s, &server{})
   log.Println("rpc服务已经开启")
   s.Serve(lis)
}

运行grpc服务端:

go build grpc_service.go
./grpc_service

Go Grpc Gateway兼容HTTP协议文档自动生成网关

编写gateway服务

vim grpc_gateway.go
package main
import (
   "flag"
   "net/http"
   "log"
   "github.com/golang/glog"
   "golang.org/x/net/context"
   "github.com/grpc-ecosystem/grpc-gateway/runtime"
   "google.golang.org/grpc"
   gw "grpc-gateway-demo/gateway"
)
var (
   echoEndpoint = flag.String("echo_endpoint", "localhost:9192", "endpoint of Gateway")
)
func run() error {
   ctx := context.Background()
   ctx, cancel := context.WithCancel(ctx)
   defer cancel()
   mux := runtime.NewServeMux()
   opts := []grpc.DialOption{grpc.WithInsecure()}
   err := gw.RegisterGatewayHandlerFromEndpoint(ctx, mux, *echoEndpoint, opts)
   if err != nil {
       return err
   }
   log.Println("服务开启")
   return http.ListenAndServe(":8080", mux)
}
func main() {
   flag.Parse()
   defer glog.Flush()
   if err := run(); err != nil {
       glog.Fatal(err)
   }
}

运行网关程序

go build grpc_gateway.go
./grpc_gateway

Go Grpc Gateway兼容HTTP协议文档自动生成网关

使用http的方式调用网关:

curl -X POST -k http://localhost:8080/v1/example/echo -d '{"value":" world"}'
{"value":"Hello  world"}

四,使用gateway生成swagger文档

cd gateway
protoc -I/usr/local/include -I. \
 -I$GOPATH/src \
 -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
 --swagger_out=logtostderr=true:. \
 gateway.proto

Go Grpc Gateway兼容HTTP协议文档自动生成网关

五,性能对比

对比以下两项:

http -> go -> grpc -> go

http -> go -> http -> grpc_gateway -> grpc -> go

全程使用ab 带 -k进行压测

http -> go -> grpc -> go

Go Grpc Gateway兼容HTTP协议文档自动生成网关

Go Grpc Gateway兼容HTTP协议文档自动生成网关

http -> go -> http -> grpc_gateway -> grpc -> go

Go Grpc Gateway兼容HTTP协议文档自动生成网关

Go Grpc Gateway兼容HTTP协议文档自动生成网关

六,总结

在GO的场景下基本上4倍差距,但是考虑到本身Go在grpc和http上本身就有3.5倍的差距,本身在同等HTTP的情况下经过grpc-gateway和不经过直接到API差距大概在20~30%左右,这样的性能消耗带来的是兼容HTTP并且还可以自动生成swagger(还可以作为调试工具),何乐而不为呢?

来源:https://juejin.cn/post/6844904074454925319

标签:Go,Grpc,Gateway,HTTP协议,生成网关
0
投稿

猜你喜欢

  • PHP开发实现快递查询功能详解

    2023-11-24 12:19:39
  • MySQL8.0.18配置多主一从

    2024-01-15 01:23:15
  • php递归删除目录与文件的方法

    2023-09-12 02:02:02
  • Centos8(最小化安装)全新安装Python3.8+pip的方法教程

    2022-11-09 06:00:27
  • 如何使用repr调试python程序

    2023-10-11 21:39:15
  • vue项目中常见问题及解决方案(推荐)

    2024-04-26 17:37:47
  • python PaddleSpeech实现婴儿啼哭识别

    2023-08-22 22:25:31
  • python线程池的四种好处总结

    2023-01-27 11:09:55
  • 解决MySQL数据库链接超时报1129错误问题

    2024-01-17 12:15:04
  • js中eval详解

    2024-04-19 10:01:30
  • 查看python安装路径及pip安装的包列表及路径

    2021-10-25 01:36:48
  • ASP生成html的新方法

    2011-04-02 11:04:00
  • F.conv2d pytorch卷积计算方式

    2021-06-26 07:38:41
  • Python面向对象编程之类的封装

    2022-10-20 05:29:49
  • js实现GIF动图分解成多帧图片上传

    2023-08-15 00:06:38
  • Scrapy-Redis结合POST请求获取数据的方法示例

    2023-08-25 06:31:56
  • 微信小程序上传图片功能(附后端代码)

    2023-07-24 04:21:40
  • Python用20行代码实现完整邮件功能

    2023-04-06 12:20:49
  • python:批量统计xml中各类目标的数量案例

    2021-11-17 05:22:44
  • 终端能到import模块 解决jupyter notebook无法导入的问题

    2022-11-19 19:26:29
  • asp之家 网络编程 m.aspxhome.com