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

golang通用的grpc?http基礎(chǔ)開發(fā)框架使用快速入門

 更新時間:2023年09月03日 10:29:56   作者:莫大  
這篇文章主要為大家介紹了golang通用的grpc?http基礎(chǔ)開發(fā)框架使用快速入門詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

go-moda

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

倉庫地址: [https://github.com/webws/go-moda]

倉庫一直在更新,歡迎大家吐槽和指點

特性

  • transport: 集成 http(echo、gin)和 grpc。
  • tracing: openTelemetry 實現(xiàn)微務(wù)鏈路追蹤
  • pprof: 分析性能
  • config: 通用的配置文件讀取模塊,支持 toml、yaml 和 json 格式。

logger: 日志系統(tǒng)模塊,基于 Zap,并支持全局日志和模塊日志。

快速使用

conf.toml

http_addr = ":8081"
grpc_addr = ":8082"

啟用http(gin) 和 grpc服務(wù)

package main
import (
    "context"
    "net/http"
    "github.com/gin-gonic/gin"
    app "github.com/webws/go-moda"
    "github.com/webws/go-moda/config"
    pbexample "github.com/webws/go-moda/example/pb/example"
    "github.com/webws/go-moda/logger"
    modagrpc "github.com/webws/go-moda/transport/grpc"
    modahttp "github.com/webws/go-moda/transport/http"
)
var ServerName string
type Config struct {
    HttpAddr string `json:"http_addr" toml:"http_addr"`
    GrpcAddr string `json:"grpc_addr" toml:"grpc_addr"`
}
func main() {
    conf := &Config{}
    if err := config.NewConfigWithFile("./conf.toml").Load(conf); err != nil {
        logger.Fatalw("NewConfigWithFile fail", "err", err)
    }
    // http server
    gin, httpSrv := modahttp.NewGinHttpServer(
        modahttp.WithAddress(conf.HttpAddr),
    )
    registerHttp(gin)
    // grpc server
    grpcSrv := modagrpc.NewServer(
        modagrpc.WithServerAddress(conf.GrpcAddr),
    )
    grecExample := &ExampleServer{}
    pbexample.RegisterExampleServiceServer(grpcSrv, grecExample)
    // app run
    a := app.New(
        app.Server(httpSrv, grpcSrv),
        app.Name(ServerName),
    )
    if err := a.Run(); err != nil {
        logger.Fatalw("app run error", "err", err)
    }
}
func registerHttp(g *gin.Engine) {
    g.GET("/helloworld", func(c *gin.Context) {
        logger.Debugw("Hello World")
        c.JSON(http.StatusOK, http.StatusText(http.StatusOK))
    })
}
type ExampleServer struct {
    pbexample.UnimplementedExampleServiceServer
}
func (s *ExampleServer) SayHello(ctx context.Context, req *pbexample.HelloRequest) (*pbexample.HelloResponse, error) {
    return &pbexample.HelloResponse{Message: "Hello " + req.Name}, nil
}

運行

go run ./ -c ./conf.toml

* 請求 http url http://localhost:8081/helloworld  

* grpc 服務(wù) 使用 gRPC 客戶端調(diào)用 SayHello 方法

其他服務(wù)啟用示例

1. echo http :[example_echo](https://github.com/webws/go-moda/tree/main/example/echohttp)
2. net http :[example_echo](https://github.com/webws/go-moda/blob/main/example/nethttp)
3. grpc [example_grpc](https://github.com/webws/go-moda/tree/main/example/grpc)

## pprof 性能分析

啟動服務(wù)默認(rèn)開啟 pprof 性能分析,瀏覽器打開 http://localhost:8081/debug/ 查看

可視化分析 gouroutine

go tool pprof 

http://localhost:8081/debug/pprof/goroutine

(pprof) web

可能提示 需要先安裝 graphviz, mac 下可以使用 brew 安裝

brew install graphviz

## tracing 鏈路追蹤

* 使用 opentelemetry 實現(xiàn)微服務(wù)鏈路追蹤,目前 exporter 支持 jaeger 

* 示例集成了docker 環(huán)境,支持 make deploy 同時啟動 jaeger,api1,api2,api3,grpc 服務(wù)

* 詳細(xì)示例請看:[tracing_example]

1. 初始化 jaeger tracing

import "github.com/webws/go-moda/tracing"
func main(){
//...
shutdown, err := tracing.InitJaegerProvider(conf.JaegerUrl, "grpc-server")
if err != nil {
    panic(err)
}
defer shutdown(context.Background())
//...
}

 2. 在代碼主動tracing start

ctx, span := tracing.Start(c.Request().Context(), "api1")
defer span.End()

3. 服務(wù)之間調(diào)用 產(chǎn)生的鏈路   

*  server端: 增加 WithTracing 即可

//...
gin, httpSrv := modahttp.NewGinHttpServer(
    modahttp.WithAddress(conf.HttpAddr),
    modahttp.WithTracing(true),
)

 * client端:  封裝了 CallAPI 方法, 已將span ctx 信息注入到請求頭

// ...
_, err := modahttp.CallAPI(ctx, url, "POST", nil)

以上就是golang通用的grpc http基礎(chǔ)開發(fā)框架使用快速入門的詳細(xì)內(nèi)容,更多關(guān)于golang grpc http開發(fā)框架的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解Golang中string的實現(xiàn)原理與高效使用

    詳解Golang中string的實現(xiàn)原理與高效使用

    在Go語言中,無論是字符串常量、字符串變量還是代碼中出現(xiàn)的字符串字面量,它們的類型都被統(tǒng)一設(shè)置為string,下面就跟隨小編一起來了解一下Golang中string的實現(xiàn)原理與高效使用吧
    2024-01-01
  • 基于Go語言實現(xiàn)插入排序算法及優(yōu)化

    基于Go語言實現(xiàn)插入排序算法及優(yōu)化

    插入排序是一種簡單的排序算法。這篇文章將利用Go語言實現(xiàn)冒泡排序算法,文中的示例代碼講解詳細(xì),對學(xué)習(xí)Go語言有一定的幫助,需要的可以參考一下
    2022-12-12
  • Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露問題解析

    Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露問題解析

    這篇文章主要介紹了Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • GoLang中的timer定時器實現(xiàn)原理分析

    GoLang中的timer定時器實現(xiàn)原理分析

    Timer中對外暴露的只有一個channel,這個 channel也是定時器的核心。當(dāng)計時結(jié)束時,Timer會發(fā)送值到channel中,外部環(huán)境在這個 channel 收到值的時候,就代表計時器超時了,可與select搭配執(zhí)行一些超時邏輯
    2023-02-02
  • Ubuntu安裝Go語言運行環(huán)境

    Ubuntu安裝Go語言運行環(huán)境

    由于最近偏愛Ubuntu,在加上作為一門開源語言,在Linux上從源代碼開始搭建環(huán)境更讓人覺得有趣味性。讓我們直接先從Go語言的環(huán)境搭建開始
    2015-04-04
  • GO語言基本類型String和Slice,Map操作詳解

    GO語言基本類型String和Slice,Map操作詳解

    這篇文章主要為大家介紹了GO語言基本類型String和Slice,Map操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Golang中的自定義函數(shù)詳解

    Golang中的自定義函數(shù)詳解

    函數(shù)構(gòu)成代碼執(zhí)行的邏輯結(jié)構(gòu)。在Go語言中,函數(shù)的基本組成為:關(guān)鍵字func、函數(shù)名、參數(shù)列表、返回值、函數(shù)體和返回語句。
    2018-10-10
  • Go語言中字符串賦值中的問題與解決方法

    Go語言中字符串賦值中的問題與解決方法

    這篇文章主要為大家詳細(xì)介紹了Go語言中字符串賦值會出現(xiàn)的一些問題以及解決方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2024-12-12
  • Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine)

    Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine)

    本文主要介紹了Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • golang用melody搭建輕量的websocket服務(wù)的示例代碼

    golang用melody搭建輕量的websocket服務(wù)的示例代碼

    在Go中,可以使用gin和melody庫來搭建一個輕量級的WebSocket服務(wù),gin是一個流行的Web框架,而melody是一個用于處理WebSocket的庫,本文給大家演示如何使用gin和melody搭建WebSocket服務(wù),感興趣的朋友一起看看吧
    2023-10-10

最新評論