Golang?gRPC?HTTP協(xié)議轉(zhuǎn)換示例
gRPC HTTP協(xié)議轉(zhuǎn)換
正當有這個需求的時候,就看到了這個實現(xiàn)姿勢。源自coreos的一篇博客,轉(zhuǎn)載到了grpc官方博客gRPC with REST and Open APIs。
etcd3改用grpc后為了兼容原來的api,同時要提供http/json方式的API,為了滿足這個需求,要么開發(fā)兩套API,要么實現(xiàn)一種轉(zhuǎn)換機制,他們選擇了后者,而我們選擇跟隨他們的腳步。
他們實現(xiàn)了一個協(xié)議轉(zhuǎn)換的網(wǎng)關(guān),對應(yīng)github上的項目grpc-gateway,這個網(wǎng)關(guān)負責(zé)接收客戶端請求,然后決定直接轉(zhuǎn)發(fā)給grpc服務(wù)還是轉(zhuǎn)給http服務(wù),當然,http服務(wù)也需要請求grpc服務(wù)獲取響應(yīng),然后轉(zhuǎn)為json響應(yīng)給客戶端。結(jié)構(gòu)如圖:
下面我們就直接實戰(zhàn)吧。基于hello-tls項目擴展,客戶端改動不大,服務(wù)端和proto改動較大。
安裝grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
項目結(jié)構(gòu):
$GOPATH/src/grpc-go-practice/ example/ |—— hello-http-2/ |—— client/ |—— main.go // 客戶端 |—— server/ |—— main.go // 服務(wù)端 |—— 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轉(zhuǎn)換提供支持。
示例代碼
proto/hello_http.proto
syntax = "proto3"; // 指定proto版本 package proto; // 指定包名 import "google/api/annotations.proto"; // 定義Hello服務(wù) service HelloHttp { // 定義SayHello方法 rpc SayHello(HelloHttpRequest) returns (HelloHttpReply) { // http option option (google.api.http) = { post: "/example/echo" body: "*" }; } } // HelloRequest 請求結(jié)構(gòu) message HelloHttpRequest { string name = 1; } // HelloReply 響應(yīng)結(jié)構(gòu) 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
文件,這個文件就是用來做協(xié)議轉(zhuǎn)換的,查看文件可以看到里面生成的http handler,處理上面定義的路由"example/echo"接收POST參數(shù),調(diào)用HelloHTTP服務(wù)的客戶端請求grpc服務(wù)并響應(yīng)結(jié)果。
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并實現(xiàn)約定的接口 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 檢查請求協(xié)議并返回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服務(wù) 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服務(wù)直接處理還是交給gateway做轉(zhuǎn)發(fā)處理。其中grpcHandlerFunc
函數(shù)負責(zé)處理決定用哪個handler處理請求,這個方法是直接Copy過來用的,原文的注釋說他們也是從別處Copy的。感謝貢獻者。
基本流程:
- 實例化標準grpc server
- 將grpc server注冊給gateway
- 開啟http服務(wù),handler指定給grpcHandlerFunc方法
注意:必須開啟HTTPS
運行結(jié)果
開啟服務(wù):
# hello-http-2/server go run main.go > grpc and https on port: 50052
調(diào)用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是個不完整的實現(xiàn)姿勢,可以不用https,但是需要分別開啟grpc服務(wù)和http服務(wù),這里不做說明了。
本系列示例代碼 go-grpc-tutorial
以上就是Golang gRPC HTTP協(xié)議轉(zhuǎn)換示例的詳細內(nèi)容,更多關(guān)于Golang gRPC HTTP協(xié)議轉(zhuǎn)換的資料請關(guān)注腳本之家其它相關(guān)文章!
- golang通用的grpc?http基礎(chǔ)開發(fā)框架使用快速入門
- Go創(chuàng)建Grpc鏈接池實現(xiàn)過程詳解
- go?doudou開發(fā)gRPC服務(wù)快速上手實現(xiàn)詳解
- Go Grpc Gateway兼容HTTP協(xié)議文檔自動生成網(wǎng)關(guān)
- Go?gRPC進階教程gRPC轉(zhuǎn)換HTTP
- Go?gRPC服務(wù)proto數(shù)據(jù)驗證進階教程
- Go?gRPC服務(wù)進階middleware使用教程
- Go?gRPC進階教程服務(wù)超時設(shè)置
- Go語言與gRPC的完美結(jié)合實戰(zhàn)演練
相關(guān)文章
云端golang開發(fā),無需本地配置,能上網(wǎng)就能開發(fā)和運行
這篇文章主要介紹了云端golang開發(fā),無需本地配置,能上網(wǎng)就能開發(fā)和運行的相關(guān)資料,需要的朋友可以參考下2023-10-10Go語言基礎(chǔ)之網(wǎng)絡(luò)編程全面教程示例
這篇文章主要為大家介紹了Go語言基礎(chǔ)之網(wǎng)絡(luò)編程全面教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12