国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

golang 工程組件:grpc-gateway 環(huán)境安裝+默認(rèn)網(wǎng)關(guān)測(cè)試

這篇具有很好參考價(jià)值的文章主要介紹了golang 工程組件:grpc-gateway 環(huán)境安裝+默認(rèn)網(wǎng)關(guān)測(cè)試。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

grpc-gateway

grpc-gateway 顧名思義是專門是grpc的網(wǎng)關(guān)。也是一個(gè)protobuf的編譯器,是一個(gè)proto的插件。 grpc-gateway就是將http請(qǐng)求處理后轉(zhuǎn)發(fā)到對(duì)應(yīng)grpc服務(wù)上。很多瀏覽器,或者客戶端開箱不支持grpc,只支持傳統(tǒng)的restful API。 grpc網(wǎng)關(guān)而且也支持負(fù)載,兼容不同版本。

官方文檔

grpc-gateway

源碼

架構(gòu)

golang 工程組件:grpc-gateway 環(huán)境安裝+默認(rèn)網(wǎng)關(guān)測(cè)試,golang,gateway,開發(fā)語言

大致流程如下

  • 寫好服務(wù)的proto文件。(代理+grpc)

  • 根據(jù)proto文件生成反向代理服務(wù)代碼

  • 根據(jù)proto文件生成grpc服務(wù)存根

  • 啟動(dòng)反向代理和grpc

  • 客戶端使用http json訪問 或別的restful api形式

環(huán)境安裝

protobuf

protobuf鏈接

下載對(duì)應(yīng)環(huán)境的porotbuf。解壓后bin路徑配置環(huán)境變量

插件安裝

博主 go 用的 1.19 + windows,預(yù)先安裝好protobuf

go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc- gateway@v2.12.0 
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2

可以把對(duì)應(yīng)GO_PATH bin下插件的二進(jìn)制文件拷到對(duì)應(yīng)go安裝的bin目錄下

grpc+默認(rèn)網(wǎng)關(guān)測(cè)試

proto文件

echo.proto

syntax = "proto3";
package  echo;
option go_package = "echo/proto";


message User{
  int64 id = 1;
  string name = 2;
  int32 age = 3;
  string phone = 4;
  Addr addr = 5;
}
message Addr {
  string province = 1;
  string city = 2;
  string county = 3;
}

service Echo{
  rpc Get(User) returns (User) {}
  rpc AddOrUpdate(User) returns (User) {}
  rpc Delete(User) returns (User) {}
}
生成grpc stub
# 生成message 
protoc --proto_path=proto --go_out=proto --go_opt=paths=source_relative proto/echo.proto 
# 生成grpc service 
protoc --proto_path=proto --go-grpc_out=proto --go-grpc_opt=paths=source_relative proto/echo.proto
生成默認(rèn)網(wǎng)關(guān)
# 生成gateway protoc --proto_path=proto  --grpc-gateway_out=proto  --grpc-gateway_opt logtostderr=true  --grpc-gateway_opt paths=source_relative  --grpc-gateway_opt generate_unbound_methods=true  proto/echo.proto
grpc服務(wù)器代碼

server.go

package server

import (
    "context"
    "echo/proto"
    "fmt"
)

type echoServer struct {
    proto.UnimplementedEchoServer
}

func NewServer() proto.EchoServer {
    return &echoServer{}
}
func (s *echoServer) Get(ctx context.Context, in *proto.User) (*proto.User, error) {
    fmt.Printf("%+v\n", in)
    return in, nil
}
func (s *echoServer) AddOrUpdate(ctx context.Context, in *proto.User) (*proto.User, error) {
    fmt.Printf("%+v\n", in)
    return in, nil
}
func (s *echoServer) Delete(ctx context.Context, in *proto.User) (*proto.User, error) {
    fmt.Printf("%+v\n", in)
    return in, nil
}
gateway代碼

這里直接用官網(wǎng)http代理的代碼。需要修改端口和引用自己的grpc服務(wù)和網(wǎng)關(guān)package

gateway.go

package gateway

import (
    "context"
    "flag"
    "net/http"

    "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    _ "google.golang.org/grpc/grpclog"

    gw "echo/proto"  // Update
)

var (
    // command-line options:
    // gRPC server endpoint
    grpcServerEndpoint = flag.String("grpc-server-endpoint",  "localhost:50051", "gRPC server endpoint")
)

func Run() error {
    ctx := context.Background()
    ctx, cancel := context.WithCancel(ctx)
    defer cancel()

    // Register gRPC server endpoint
    // Note: Make sure the gRPC server is running properly and accessible
    mux := runtime.NewServeMux()
    opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
    // 注冊(cè)對(duì)應(yīng)grpc服務(wù)端點(diǎn)handler
    err := gw.RegisterEchoHandlerFromEndpoint(ctx, mux,  *grpcServerEndpoint, opts)
    if err != nil {
        return err
    }

    // Start HTTP server (and proxy calls to gRPC server endpoint)
    return http.ListenAndServe(":8081", mux)
}


測(cè)試

main.go

package main

import (
    "context"
    "echo/echo_server/gateway"
    "echo/echo_server/server"
    "echo/proto"
    "fmt"
    "google.golang.org/grpc"
    "log"
    "net"
    "os"
    "os/signal"
    "time"
)

func main() {
    // 先啟動(dòng)grpc service
    go func() {
        if err := run(); err != nil {
            log.Fatal(err)
        }
    }()
    time.Sleep(time.Second * 2)
    //后啟動(dòng)gateway
    go func() {
        if err := gateway.Run(); err != nil {
            log.Fatal(err)
        }
    }()

    ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
    defer stop()
    <-ctx.Done()
}

func run() error {
    lis, err := net.Listen("tcp", ":50051")
    if err != nil {
        log.Fatal(err)
    }
    s := grpc.NewServer()
    userServiceServer := server.NewServer()
    proto.RegisterEchoServer(s, userServiceServer)
    fmt.Println("listening ")
    return s.Serve(lis)
}
默認(rèn)路由

路由為proto文件中{包名}.{服務(wù)名}/{方法}。 gateway對(duì)外默認(rèn)是post方法

PS D:\GIT\gorun\grpc-gateway-practice\echo> Invoke-RestMethod -Uri "http://10.5.81.57:8081/echo.Echo/Get" -Method Post


id    : 0
name  :
age   : 0
phone :
addr  :

用postman更方便些

總結(jié)

  • grpc-gateway 只是提供一個(gè)反向代理,可以通過配置進(jìn)行g(shù)rpc版本兼容。

  • grpc-gateway對(duì)外提供restful API風(fēng)格的http接口,更好兼容各種客戶端接入,無需grpc客戶端文章來源地址http://www.zghlxwxcb.cn/news/detail-719417.html

到了這里,關(guān)于golang 工程組件:grpc-gateway 環(huán)境安裝+默認(rèn)網(wǎng)關(guān)測(cè)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • gRPC-GateWay Swagger 實(shí)戰(zhàn)

    gRPC-GateWay Swagger 實(shí)戰(zhàn)

    上一次我們分享了關(guān)于 gRPC-Gateway 快速實(shí)戰(zhàn) ,可以查看地址來進(jìn)行回顧 : 也可以查看關(guān)于 gRPC 的歷史文章: gRPC介紹 gRPC 客戶端調(diào)用服務(wù)端需要連接池嗎? gRPC的攔截器 gRPC的認(rèn)證 分享一下 gRPC- HTTP網(wǎng)關(guān) I 今天主要是分享關(guān)于 gRPC-Gateway Swagger 的實(shí)戰(zhàn)部分,文章大體分為如下幾個(gè)

    2024年02月10日
    瀏覽(18)
  • gRPC之gRPC Gateway

    gRPC之gRPC Gateway

    etcd3 API全面升級(jí)為gRPC后,同時(shí)要提供REST API服務(wù),維護(hù)兩個(gè)版本的服務(wù)顯然不太合理,所以 grpc-gateway 誕生了。通過protobuf的自定義option實(shí)現(xiàn)了一個(gè)網(wǎng)關(guān),服務(wù)端同時(shí)開啟gRPC和HTTP服務(wù), HTTP服務(wù)接收客戶端請(qǐng)求后轉(zhuǎn)換為grpc請(qǐng)求數(shù)據(jù),獲取響應(yīng)后轉(zhuǎn)為json數(shù)據(jù)返回給客戶端。結(jié)構(gòu)

    2024年02月07日
    瀏覽(22)
  • win10系統(tǒng) C++環(huán)境 安裝編譯GRPC

    win10系統(tǒng) C++環(huán)境 安裝編譯GRPC

    為了依賴的成功安裝,采用gitee進(jìn)行下載與更新。記得需要安裝git軟件。 安裝命令: 在自己指定的目錄下,鼠標(biāo)右鍵,選擇 git Bash Here 打開命令行 在grpc的目錄下修改配置文件:.gitmodules 復(fù)制下面內(nèi)容替換.gitmodules內(nèi)容: 在grpc目錄下,在git 上使用更新命令 使用cmake對(duì)grpc進(jìn)行

    2024年02月07日
    瀏覽(20)
  • 【微服務(wù)筆記16】微服務(wù)組件之Gateway服務(wù)網(wǎng)關(guān)基礎(chǔ)環(huán)境搭建、高可用網(wǎng)關(guān)環(huán)境搭建

    【微服務(wù)筆記16】微服務(wù)組件之Gateway服務(wù)網(wǎng)關(guān)基礎(chǔ)環(huán)境搭建、高可用網(wǎng)關(guān)環(huán)境搭建

    這篇文章,主要介紹微服務(wù)組件之Gateway服務(wù)網(wǎng)關(guān)基礎(chǔ)環(huán)境搭建、高可用網(wǎng)關(guān)環(huán)境搭建。 目錄 一、Gateway服務(wù)網(wǎng)關(guān) 1.1、什么是Gateway 1.2、Gateway基礎(chǔ)環(huán)境搭建 (1)基礎(chǔ)環(huán)境介紹 (2)引入依賴 (3)添加路由配置信息 (4)運(yùn)行測(cè)試 1.3、Gateway三個(gè)核心概念 1.4、Gateway配置信息 (

    2024年02月07日
    瀏覽(28)
  • [golang 微服務(wù)] 4.  gRPC介紹,Protobuf結(jié)合gRPC 創(chuàng)建微服務(wù)

    [golang 微服務(wù)] 4. gRPC介紹,Protobuf結(jié)合gRPC 創(chuàng)建微服務(wù)

    gRPC是一個(gè) 高性能 、 開源 和 通用 的 RPC 框架 , 面向移動(dòng)端 和 HTTP/2 設(shè)計(jì),目前提供 C、Java 和 Go語言版本,分別是:grpc, grpc-java, grpc-go, 其中 C 版本支持 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C# 支持 (1).提供幾乎所有主流語言的實(shí)現(xiàn), 打破語言隔閡 (2). 基于 HTTP/2 標(biāo)準(zhǔn)設(shè)計(jì)

    2024年02月04日
    瀏覽(19)
  • golang grpc配置使用實(shí)戰(zhàn)教程

    golang grpc配置使用實(shí)戰(zhàn)教程

    RPC是遠(yuǎn)程過程調(diào)用(Remote Procedure Call)的縮寫形式, RPC 的主要功能目標(biāo)是讓構(gòu)建分布式計(jì)算(應(yīng)用)更容易,在提供強(qiáng)大的遠(yuǎn)程調(diào)用能力時(shí)不損失本地調(diào)用的語義簡潔性。通俗地講, 使用RPC進(jìn)行通信,調(diào)用遠(yuǎn)程函數(shù)就像調(diào)用本地函數(shù)一樣,RPC底層會(huì)做好數(shù)據(jù)的序列化與傳輸。

    2024年02月03日
    瀏覽(25)
  • golang 通用的 grpc http 基礎(chǔ)開發(fā)框架

    golang 通用的 grpc http 基礎(chǔ)開發(fā)框架

    golang 通用的 grpc http 基礎(chǔ)開發(fā)框架 倉庫地址: https://github.com/webws/go-moda 倉庫一直在更新,歡迎大家吐槽和指點(diǎn) transport: 集成 http(echo、gin)和 grpc。 tracing: openTelemetry 實(shí)現(xiàn)微務(wù)鏈路追蹤 pprof: 分析性能 config: 通用的配置文件讀取模塊,支持 toml、yaml 和 json 格式。 logger: 日志系統(tǒng)

    2024年02月10日
    瀏覽(15)
  • golang gRPC:根據(jù).protobuf文件生成go代碼

    安裝 protoc 編譯器。如果沒有安裝,可以參考官方文檔進(jìn)行安裝。 使用 protoc 命令生成 gRPC 代碼: 此命令將生成 .pb.go 和 _grpc.pb.go 文件,其中包含 protobuf 和 gRPC 的代碼實(shí)現(xiàn). –go_out選項(xiàng)會(huì)生成純粹的Protocol Buffer消息代碼,這包括Go語言的消息結(jié)構(gòu)體和一些輔助方法。如果你只

    2024年02月14日
    瀏覽(20)
  • [golang 微服務(wù)] 6. GRPC微服務(wù)集群+Consul集群+grpc-consul-resolver案例演示

    [golang 微服務(wù)] 6. GRPC微服務(wù)集群+Consul集群+grpc-consul-resolver案例演示

    上一節(jié)講解了consul集群: [golang 微服務(wù)] 5. 微服務(wù)服務(wù)發(fā)現(xiàn)介紹,安裝以及consul的使用,Consul集群,這樣的話,當(dāng)一臺(tái)server掛掉之后,集群就會(huì)從另一臺(tái)server中獲取服務(wù),這就保證了客戶端訪問consul集群的負(fù)載均衡性. 這里還有一個(gè)問題: 就是當(dāng)終端的對(duì)應(yīng)的 微服務(wù)掛掉 了,consul集群se

    2024年02月09日
    瀏覽(21)
  • go get google.golang.org/grpc報(bào)錯(cuò)

    go get google.golang.org/grpc報(bào)錯(cuò)

    win10環(huán)境,報(bào)錯(cuò)完整內(nèi)容如下 go get google.golang.org/grpc: module google.golang.org/grpc: Get https://proxy.golang.org/google.golang.org/grpc/@v/list: dial tcp [2404:6800:4012:3::2011]:443: connectex: A connection attempt failed because the connected party did no t properly respond after a period of time, or established connection failed because c

    2024年02月11日
    瀏覽(39)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包