golang通用的grpc?http基礎(chǔ)開發(fā)框架使用快速入門
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)原理與高效使用
在Go語言中,無論是字符串常量、字符串變量還是代碼中出現(xiàn)的字符串字面量,它們的類型都被統(tǒng)一設(shè)置為string,下面就跟隨小編一起來了解一下Golang中string的實現(xiàn)原理與高效使用吧2024-01-01Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露問題解析
這篇文章主要介紹了Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine)
本文主要介紹了Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05golang用melody搭建輕量的websocket服務(wù)的示例代碼
在Go中,可以使用gin和melody庫來搭建一個輕量級的WebSocket服務(wù),gin是一個流行的Web框架,而melody是一個用于處理WebSocket的庫,本文給大家演示如何使用gin和melody搭建WebSocket服務(wù),感興趣的朋友一起看看吧2023-10-10