欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Golang?gRPC?HTTP協(xié)議轉(zhuǎn)換示例

 更新時間:2022年06月16日 11:15:13   作者:Coldstar  
這篇文章主要為大家介紹了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)文章!

相關(guān)文章

  • Goland 生成可執(zhí)行文件的操作

    Goland 生成可執(zhí)行文件的操作

    這篇文章主要介紹了Goland 生成可執(zhí)行文件的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 淺析Golang中變量與常量的聲明與使用

    淺析Golang中變量與常量的聲明與使用

    變量、常量的聲明與使用是掌握一門編程語言的基礎(chǔ),這篇文章主要為大家詳細介紹了Golang中變量與常量的聲明與使用,需要的可以參考一下
    2023-04-04
  • 淺談Go語言中字符串和數(shù)組

    淺談Go語言中字符串和數(shù)組

    這篇文章主要簡單介紹了Go語言中字符串和數(shù)組的使用方法和申明方式,需要的朋友可以參考下
    2015-01-01
  • 云端golang開發(fā),無需本地配置,能上網(wǎng)就能開發(fā)和運行

    云端golang開發(fā),無需本地配置,能上網(wǎng)就能開發(fā)和運行

    這篇文章主要介紹了云端golang開發(fā),無需本地配置,能上網(wǎng)就能開發(fā)和運行的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • Go語言基礎(chǔ)之網(wǎng)絡(luò)編程全面教程示例

    Go語言基礎(chǔ)之網(wǎng)絡(luò)編程全面教程示例

    這篇文章主要為大家介紹了Go語言基礎(chǔ)之網(wǎng)絡(luò)編程全面教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • Go語言利用接口實現(xiàn)鏈表插入功能詳解

    Go語言利用接口實現(xiàn)鏈表插入功能詳解

    這篇文章主要為大家介紹了Go語言中的接口,以及如何利用接口實現(xiàn)鏈表插入功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-04-04
  • 使用go來操作redis的方法示例

    使用go來操作redis的方法示例

    今天小編就為大家分享一篇關(guān)于使用go來操作redis的方法示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • go流程控制代碼詳解

    go流程控制代碼詳解

    這篇文章主要介紹了go流程控制,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-05-05
  • golang關(guān)閉chan通道的方法示例

    golang關(guān)閉chan通道的方法示例

    在go語言中,通道(channel)是一個非常重要的概念,通道提供了一種在不同 goroutine 之間安全地傳遞數(shù)據(jù)的方式,在本文中,我們將討論如何關(guān)閉通道以及在關(guān)閉通道時需要考慮的事項,需要的朋友可以參考下
    2024-02-02
  • GoLang編程必備:GoFrame?GoLand插件介紹

    GoLang編程必備:GoFrame?GoLand插件介紹

    掌握GoLang編程必備工具,我們來深入了解GoFrame?GoLand插件,這個小巧但強大的插件將極大提升你的開發(fā)效率,讓代碼流暢如行云流水,一起跟隨本指南,解鎖更智能的編程之旅!
    2023-12-12

最新評論