脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - Go Grpc Gateway兼容HTTP协议文档自动生成网关

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

2022-10-21 12:01文振熙 Golang

这篇文章主要为大家介绍了Go Grpc Gateway兼容HTTP协议文档自动生成网关示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

调用,让客户端可以更具自身情况自由选择,服务端工作只需要做一份呢?还别说真还有一个准备好的轮子那就是今天的主角《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(还可以作为调试工具),何乐而不为呢?

以上就是Go Grpc Gateway兼容HTTP协议文档自动生成网关的详细内容,更多关于Go Grpc Gateway兼容HTTP的资料请关注服务器之家其它相关文章!

原文地址:https://juejin.cn/post/6844904074454925319

延伸 · 阅读

精彩推荐
  • Golang服务器端Go程序对长短链接的处理及运行参数的保存

    服务器端Go程序对长短链接的处理及运行参数的保存

    这篇文章主要介绍了服务器端Go程序对长短链接的处理及运行参数的保存,这里针对使用Go语言编写的Socket服务器进行实例说明,需要的朋友可以参考下 ...

    G1SLu2802020-04-29
  • GolangGo语言hello world实例

    Go语言hello world实例

    这篇文章主要介绍了Go语言hello world实例,本文先是给出了hello world的代码实例,然后对一些知识点和技巧做了解释,需要的朋友可以参考下 ...

    junjie3162020-04-08
  • Golanggolang 实现菜单树的生成方式

    golang 实现菜单树的生成方式

    这篇文章主要介绍了golang 实现菜单树的生成方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    飞渡浮舟~~9632021-06-05
  • Golanggolang中的并发和并行

    golang中的并发和并行

    这篇文章主要介绍了golang中的并发和并行用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    盼盼编程9512021-06-25
  • Golanggoland2020.2.x永久激活码破解详细教程亲测可用(Windows Linux Mac)

    goland2020.2.x永久激活码破解详细教程亲测可用(Windows Linux Mac)

    这篇文章主要介绍了goland2020.2.x永久激活码破解详细教程亲测可用(Windows Linux Mac) ,对goland激活码注册码相关知识感兴趣的朋友跟随小编一起看看吧...

    中关村老大爷37902021-02-05
  • Golang一文带你掌握Go语言运算符的使用

    一文带你掌握Go语言运算符的使用

    运算符用于在程序运行时执行数学或逻辑运算。Go 语言内置的运算符有:算术运算符、关系运算符、逻辑运算符、位运算符、赋值运算符、其他运算符。本...

    隐姓埋名48698402022-09-27
  • Golanggolang抓取网页并分析页面包含的链接方法

    golang抓取网页并分析页面包含的链接方法

    今天小编就为大家分享一篇golang抓取网页并分析页面包含的链接方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    仰天笑8852020-05-27
  • Golanggo语言beego框架jwt身份认证实现示例

    go语言beego框架jwt身份认证实现示例

    这篇文章主要为大家介绍了go语言beego框架jwt身份认证实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪...

    Jeff的技术栈10782022-09-24