最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年06月16日 11:15:13   作者:Coldstar  
這篇文章主要為大家介紹了Golang?gRPC?HTTP協(xié)議轉(zhuǎn)換示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

gRPC HTTP協(xié)議轉(zhuǎn)換

正當(dāng)有這個(gè)需求的時(shí)候,就看到了這個(gè)實(shí)現(xiàn)姿勢。源自coreos的一篇博客,轉(zhuǎn)載到了grpc官方博客gRPC with REST and Open APIs。

etcd3改用grpc后為了兼容原來的api,同時(shí)要提供http/json方式的API,為了滿足這個(gè)需求,要么開發(fā)兩套API,要么實(shí)現(xiàn)一種轉(zhuǎn)換機(jī)制,他們選擇了后者,而我們選擇跟隨他們的腳步。

他們實(shí)現(xiàn)了一個(gè)協(xié)議轉(zhuǎn)換的網(wǎng)關(guān),對應(yīng)github上的項(xiàng)目grpc-gateway,這個(gè)網(wǎng)關(guān)負(fù)責(zé)接收客戶端請求,然后決定直接轉(zhuǎn)發(fā)給grpc服務(wù)還是轉(zhuǎn)給http服務(wù),當(dāng)然,http服務(wù)也需要請求grpc服務(wù)獲取響應(yīng),然后轉(zhuǎn)為json響應(yīng)給客戶端。結(jié)構(gòu)如圖:

下面我們就直接實(shí)戰(zhàn)吧?;趆ello-tls項(xiàng)目擴(kuò)展,客戶端改動不大,服務(wù)端和proto改動較大。

安裝grpc-gateway

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

項(xiàng)目結(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中的兩個(gè)proto描述文件,直接拷貝不要做修改,里面定義了protocol buffer擴(kuò)展的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中的兩個(gè)proto文件,同時(shí)在編譯hello_http.proto時(shí)指定引入包名,最后使用grpc-gateway編譯生成hello_http_pb.gw.go文件,這個(gè)文件就是用來做協(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并實(shí)現(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"
    // 實(shí)例化標(biāo)準(zhǔn)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
}

好吧,這么大一坨。核心就是開啟了一個(gè)http server,收到請求后檢查請求是grpc還是http,然后決定是由grpc服務(wù)直接處理還是交給gateway做轉(zhuǎn)發(fā)處理。其中grpcHandlerFunc函數(shù)負(fù)責(zé)處理決定用哪個(gè)handler處理請求,這個(gè)方法是直接Copy過來用的,原文的注釋說他們也是從別處Copy的。感謝貢獻(xiàn)者。

基本流程:

  • 實(shí)例化標(biāo)準(zhǔn)grpc server
  • 將grpc server注冊給gateway
  • 開啟http服務(wù),handler指定給grpcHandlerFunc方法

注意:必須開啟HTTPS

運(yùn)行結(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,因?yàn)?是個(gè)不完整的實(shí)現(xiàn)姿勢,可以不用https,但是需要分別開啟grpc服務(wù)和http服務(wù),這里不做說明了。

本系列示例代碼 go-grpc-tutorial

以上就是Golang gRPC HTTP協(xié)議轉(zhuǎn)換示例的詳細(xì)內(nèi)容,更多關(guān)于Golang gRPC HTTP協(xié)議轉(zhuǎn)換的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Go語言繼承功能使用結(jié)構(gòu)體實(shí)現(xiàn)代碼重用

    Go語言繼承功能使用結(jié)構(gòu)體實(shí)現(xiàn)代碼重用

    今天我來給大家介紹一下在?Go?語言中如何實(shí)現(xiàn)類似于繼承的功能,讓我們的代碼更加簡潔和可重用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Go語言接口用法實(shí)例

    Go語言接口用法實(shí)例

    這篇文章主要介紹了Go語言接口用法,實(shí)例分析了Go語言接口的功能、定義及使用技巧,需要的朋友可以參考下
    2015-02-02
  • 詳解Golang如何使用Debug庫優(yōu)化代碼

    詳解Golang如何使用Debug庫優(yōu)化代碼

    這篇文章將針對Golang的debug庫進(jìn)行全面解讀,涵蓋其核心組件、高級功能和實(shí)戰(zhàn)技巧,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下
    2024-02-02
  • golang為什么要統(tǒng)一錯(cuò)誤處理

    golang為什么要統(tǒng)一錯(cuò)誤處理

    這篇文章主要介紹了golang為什么要統(tǒng)一錯(cuò)誤處理,統(tǒng)一錯(cuò)誤處理的目的是為了前端開發(fā)接收到后端的statuscode,之后便于前端邏輯上開發(fā)以及開發(fā),下文具體操作過程需要的小伙伴可以參考一下
    2022-04-04
  • Go語言范圍Range的具體使用

    Go語言范圍Range的具體使用

    range關(guān)鍵字在for循環(huán)中用于遍歷數(shù)組,切片,通道或映射的項(xiàng)目,本文主要介紹了Go語言范圍Range的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • golang fmt占位符的使用詳解

    golang fmt占位符的使用詳解

    這篇文章主要介紹了golang fmt占位符的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • GO語言ini配置文件的讀取的操作

    GO語言ini配置文件的讀取的操作

    這篇文章主要介紹了GO語言ini配置文件的讀取的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • 記一次go語言使用time.Duration類型踩過的坑

    記一次go語言使用time.Duration類型踩過的坑

    本文主要介紹了記一次go語言使用time.Duration類型踩過的坑,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 詳解Golang中下劃線的使用方法

    詳解Golang中下劃線的使用方法

    這篇文章主要介紹了詳解Golang中下劃線的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Golang技巧之重試機(jī)制詳解

    Golang技巧之重試機(jī)制詳解

    重試機(jī)制是一種在程序執(zhí)行過程中出現(xiàn)錯(cuò)誤后重新嘗試執(zhí)行程序的一種機(jī)制,可以減少程序運(yùn)行過程中出現(xiàn)的錯(cuò)誤,從而提高程序的可靠性,本文就來講講Golang中是如何實(shí)現(xiàn)重試機(jī)制的吧
    2023-05-05

最新評論

山阴县| 庄河市| 波密县| 基隆市| 东乡族自治县| 城固县| 武强县| 永平县| 大宁县| 崇明县| 神池县| 平谷区| 高雄市| 大邑县| 宜州市| 图们市| 新田县| 星子县| 新化县| 阳信县| 乐业县| 岚皋县| 长泰县| 宽甸| 双柏县| 长岛县| 读书| 博野县| 梅河口市| 商都县| 平山县| 喜德县| 延庆县| 贵阳市| 枞阳县| 卢龙县| 张家口市| 延津县| 金坛市| 醴陵市| 曲沃县|