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实战详解
- 上一篇
- Go语言pointer及switch fallthrough实战详解
- 下一篇
- GoGrpcGateway兼容HTTP协议文档自动生成网关
-
- 大意的钻石
- 这篇技术贴真及时,太全面了,赞 ??,已加入收藏夹了,关注楼主了!希望楼主能多写Golang相关的文章。
- 2023-03-04 03:07:10
-
- 怕孤独的黑猫
- 这篇博文真是及时雨啊,太详细了,写的不错,mark,关注作者了!希望作者能多写Golang相关的文章。
- 2022-12-28 17:55:40
-
- Golang · Go教程 | 22分钟前 | 标准库 · HTTP服务 · Go教程 · 可观测性 · Go expvar expvar.Publish 运行指标 expvar.Func debug vars
- Go expvar.Publish 怎么暴露自定义运行指标
- 466浏览 收藏
-
- Golang · Go教程 | 47分钟前 |
- Go errors.AsType 怎么从错误链提取具体类型
- 127浏览 收藏
-
- Golang · Go教程 | 1小时前 | 标准库 · 流式处理 · Go教程 · Go token 流式解析 encoding/xml xml.Decoder 大型XML
- Go xml.Decoder.Token 怎么流式处理大型 XML
- 326浏览 收藏
-
- Golang · Go教程 | 1小时前 |
- Go pem.Decode 怎么连续读取多个 PEM 区块
- 344浏览 收藏
-
- Golang · Go教程 | 1小时前 | go · Go encoding/json json.Decoder InputOffset
- Go json.Decoder.InputOffset 怎么定位解析错误附近字节
- 298浏览 收藏
-
- Golang · Go教程 | 2小时前 | go · Go io.Writer encoding/hex hex.Dumper
- Go hex.Dumper 怎么流式输出可读十六进制内容
- 159浏览 收藏
-
- Golang · Go教程 | 2小时前 | 切片 · go · Go encoding/binary binary.AppendUvarint varint
- Go binary.AppendUvarint 怎么追加变长整数
- 156浏览 收藏
-
- Golang · Go教程 | 3小时前 | 标准库 · 性能优化 · Go教程 · base64 Go encoding/base64 切片复用 AppendEncode EncodedLen
- Go base64.Encoding.AppendEncode 怎么复用目标缓冲区
- 285浏览 收藏
-
- Golang · Go教程 | 3小时前 |
- Go base32.Encoding.WithPadding 怎么生成无填充编码
- 473浏览 收藏
-
- Golang · Go教程 | 4小时前 |
- Go ascii85.NewDecoder 怎么流式解码分段数据
- 398浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 326次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 385次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 376次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 343次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 168次使用
-
- Golang实现HTTP编程请求和响应
- 2022-12-28 101浏览
-
- golangNewRequest/gorequest实现http请求的示例代码
- 2023-01-24 343浏览
-
- 一文详解Golang中net/http包的实现原理
- 2022-12-29 419浏览
-
- 快速掌握Go语言HTTP标准库的实现方法
- 2022-12-30 327浏览
-
- Go http请求排队处理实战示例
- 2022-12-23 265浏览

