Golang gRPC HTTP协议转换示例
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《Golang gRPC HTTP协议转换示例》,介绍一下HTTP、gRPC、协议转换,希望对大家的知识积累有所帮助,助力实战开发!
gRPC HTTP协议转换
正当有这个需求的时候,就看到了这个实现姿势。源自coreos的一篇博客,转载到了grpc官方博客gRPC with REST and Open APIs。
etcd3改用grpc后为了兼容原来的api,同时要提供http/json方式的API,为了满足这个需求,要么开发两套API,要么实现一种转换机制,他们选择了后者,而我们选择跟随他们的脚步。
他们实现了一个协议转换的网关,对应github上的项目grpc-gateway,这个网关负责接收客户端请求,然后决定直接转发给grpc服务还是转给http服务,当然,http服务也需要请求grpc服务获取响应,然后转为json响应给客户端。结构如图:
下面我们就直接实战吧。基于hello-tls项目扩展,客户端改动不大,服务端和proto改动较大。
安装grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
项目结构:
$GOPATH/src/grpc-go-practice/ example/ |—— hello-http-2/ |—— client/ |—— main.go // 客户端 |—— server/ |—— main.go // 服务端 |—— keys/ // 证书目录 |—— server.key |—— server.pem |—— proto/ |—— google // googleApi http-proto定义 |—— api |—— annotations.proto |—— annotations.pb.go |—— http.proto |—— http.pb.go |—— hello_http.proto // proto描述文件 |—— hello_http.pb.go // proto编译后文件 |—— hello_http_pb.gw.go // gateway编译后文件
这里用到了google官方Api中的两个proto描述文件,直接拷贝不要做修改,里面定义了protocol buffer扩展的HTTP option,为grpc的http转换提供支持。
示例代码
proto/hello_http.proto
syntax = "proto3"; // 指定proto版本 package proto; // 指定包名 import "google/api/annotations.proto"; // 定义Hello服务 service HelloHttp { // 定义SayHello方法 rpc SayHello(HelloHttpRequest) returns (HelloHttpReply) { // http option option (google.api.http) = { post: "/example/echo" body: "*" }; } } // HelloRequest 请求结构 message HelloHttpRequest { string name = 1; } // HelloReply 响应结构 message HelloHttpReply { string message = 1; }
这里在原来的SayHello
方法定义中增加了http option, POST方式,路由为"/example/echo"。
编译proto
cd $GOPATH/src/grpc-go-practice/example/hello-http-2/proto # 编译google.api protoc -I . --go_out=plugins=grpc,Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. google/api/*.proto # 编译hello_http.proto protoc -I . --go_out=plugins=grpc,Mgoogle/api/annotations.proto=git.vodjk.com/go-grpc/example/proto/google/api:. ./*.proto # 编译hello_http.proto gateway protoc --grpc-gateway_out=logtostderr=true:. ./hello_http.proto
注意这里需要编译google/api中的两个proto文件,同时在编译hello_http.proto时指定引入包名,最后使用grpc-gateway编译生成hello_http_pb.gw.go
文件,这个文件就是用来做协议转换的,查看文件可以看到里面生成的http handler,处理上面定义的路由"example/echo"接收POST参数,调用HelloHTTP服务的客户端请求grpc服务并响应结果。
server/main.go
package main import ( "crypto/tls" "fmt" "io/ioutil" "log" "net" "net/http" "strings" "github.com/grpc-ecosystem/grpc-gateway/runtime" "golang.org/x/net/context" "google.golang.org/grpc" pb "git.vodjk.com/go-grpc/example/proto" "google.golang.org/grpc/credentials" "google.golang.org/grpc/grpclog" ) // 定义helloHttpService并实现约定的接口 type helloHttpService struct{} // HelloHttpService ... var HelloHttpService = helloHttpService{} func (h helloHttpService) SayHello(ctx context.Context, in *pb.HelloHttpRequest) (*pb.HelloHttpReply, error) { resp := new(pb.HelloHttpReply) resp.Message = "Hello " + in.Name + "." return resp, nil } // grpcHandlerFunc 检查请求协议并返回http handler func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // TODO(tamird): point to merged gRPC code rather than a PR. // This is a partial recreation of gRPC's internal checks https://github.com/grpc/grpc-go/pull/514/files#diff-95e9a25b738459a2d3030e1e6fa2a718R61 if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { grpcServer.ServeHTTP(w, r) } else { otherHandler.ServeHTTP(w, r) } }) } func main() { endpoint := "127.0.0.1:50052" // 实例化标准grpc server creds, err := credentials.NewServerTLSFromFile("../../keys/server.pem", "../../keys/server.key") if err != nil { grpclog.Fatalf("Failed to generate credentials %v", err) } conn, _ := net.Listen("tcp", endpoint) grpcServer := grpc.NewServer(grpc.Creds(creds)) pb.RegisterHelloHttpServer(grpcServer, HelloHttpService) // http-grpc gateway ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() dcreds, err := credentials.NewClientTLSFromFile("../../keys/server.pem", "server name") if err != nil { grpclog.Fatalf("Failed to create TLS credentials %v", err) } dopts := []grpc.DialOption{grpc.WithTransportCredentials(dcreds)} gwmux := runtime.NewServeMux() err = pb.RegisterHelloHttpHandlerFromEndpoint(ctx, gwmux, endpoint, dopts) if err != nil { fmt.Printf("serve: %v\n", err) return } mux := http.NewServeMux() mux.Handle("/", gwmux) if err != nil { panic(err) } // 开启HTTP服务 cert, _ := ioutil.ReadFile("../../keys/server.pem") key, _ := ioutil.ReadFile("../../keys/server.key") var demoKeyPair *tls.Certificate pair, err := tls.X509KeyPair(cert, key) if err != nil { panic(err) } demoKeyPair = &pair srv := &http.Server{ Addr: endpoint, Handler: grpcHandlerFunc(grpcServer, mux), TLSConfig: &tls.Config{ Certificates: []tls.Certificate{*demoKeyPair}, }, } fmt.Printf("grpc and https on port: %d\n", 50052) err = srv.Serve(tls.NewListener(conn, srv.TLSConfig)) if err != nil { log.Fatal("ListenAndServe: ", err) } return }
好吧,这么大一坨。核心就是开启了一个http server,收到请求后检查请求是grpc还是http,然后决定是由grpc服务直接处理还是交给gateway做转发处理。其中grpcHandlerFunc
函数负责处理决定用哪个handler处理请求,这个方法是直接Copy过来用的,原文的注释说他们也是从别处Copy的。感谢贡献者。
基本流程:
- 实例化标准grpc server
- 将grpc server注册给gateway
- 开启http服务,handler指定给grpcHandlerFunc方法
注意:必须开启HTTPS
运行结果
开启服务:
# hello-http-2/server go run main.go > grpc and https on port: 50052
调用grpc客户端:
# hello-http-2/client go run main.go > Hello gRPC.
请求https:
curl -X POST -k https://localhost:50052/example/echo -d '{"name": "gRPC-HTTP is working!"}' > {"message":"Hello gRPC-HTTP is working!."}
为什么是hello-http-2,因为1是个不完整的实现姿势,可以不用https,但是需要分别开启grpc服务和http服务,这里不做说明了。
本系列示例代码 go-grpc-tutorial
今天带大家了解了HTTP、gRPC、协议转换的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

- 上一篇
- Go语言pointer及switch fallthrough实战详解

- 下一篇
- GoGrpcGateway兼容HTTP协议文档自动生成网关
-
- 大意的钻石
- 这篇技术贴真及时,太全面了,赞 👍👍,已加入收藏夹了,关注楼主了!希望楼主能多写Golang相关的文章。
- 2023-03-04 03:07:10
-
- 怕孤独的黑猫
- 这篇博文真是及时雨啊,太详细了,写的不错,mark,关注作者了!希望作者能多写Golang相关的文章。
- 2022-12-28 17:55:40
-
- Golang · Go教程 | 6小时前 |
- DebianOpenSSL安装失败的终极解决方案
- 501浏览 收藏
-
- Golang · Go教程 | 7小时前 |
- Debian数据快速提取技巧
- 216浏览 收藏
-
- Golang · Go教程 | 10小时前 |
- Debian系统JS依赖管理终极攻略
- 218浏览 收藏
-
- Golang · Go教程 | 11小时前 |
- Debian上Hadoop作业调度实用技巧
- 100浏览 收藏
-
- Golang · Go教程 | 12小时前 |
- Go语言闭包误区与匿名函数深度解析
- 222浏览 收藏
-
- Golang · Go教程 | 12小时前 |
- Debian系统安全回收数据的正确攻略
- 111浏览 收藏
-
- Golang · Go教程 | 14小时前 |
- Debian高效fetch技巧与使用攻略
- 125浏览 收藏
-
- Golang · Go教程 | 20小时前 |
- Debian邮件服务器升级维护攻略
- 474浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 笔灵AI生成答辩PPT
- 探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
- 15次使用
-
- 知网AIGC检测服务系统
- 知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
- 24次使用
-
- AIGC检测-Aibiye
- AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
- 30次使用
-
- 易笔AI论文
- 易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
- 40次使用
-
- 笔启AI论文写作平台
- 笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
- 35次使用
-
- HTTP 的 response 中的响应体和头部是分开发送的吗?
- 2023-01-28 387浏览
-
- B站等视频网站的弹幕用的是 websocket 还是轮询?
- 2023-02-16 447浏览
-
- Golang实现HTTP编程请求和响应
- 2022-12-28 101浏览
-
- golangNewRequest/gorequest实现http请求的示例代码
- 2023-01-24 343浏览
-
- 一文详解Golang中net/http包的实现原理
- 2022-12-29 419浏览