golang通用的grpc?http基礎(chǔ)開(kāi)發(fā)框架使用快速入門(mén)
go-moda
golang 通用的 grpc http 基礎(chǔ)開(kāi)發(fā)框架
倉(cāng)庫(kù)地址: [https://github.com/webws/go-moda]
倉(cāng)庫(kù)一直在更新,歡迎大家吐槽和指點(diǎn)
特性
- transport: 集成 http(echo、gin)和 grpc。
- tracing: openTelemetry 實(shí)現(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
}運(yùn)行
go run ./ -c ./conf.toml
* 請(qǐng)求 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 性能分析
啟動(dòng)服務(wù)默認(rèn)開(kāi)啟 pprof 性能分析,瀏覽器打開(kāi) http://localhost:8081/debug/ 查看

可視化分析 gouroutine
go tool pprof
http://localhost:8081/debug/pprof/goroutine
(pprof) web
可能提示 需要先安裝 graphviz, mac 下可以使用 brew 安裝
brew install graphviz
## tracing 鏈路追蹤
* 使用 opentelemetry 實(shí)現(xiàn)微服務(wù)鏈路追蹤,目前 exporter 支持 jaeger
* 示例集成了docker 環(huán)境,支持 make deploy 同時(shí)啟動(dòng) jaeger,api1,api2,api3,grpc 服務(wù)
* 詳細(xì)示例請(qǐng)看:[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. 在代碼主動(dòng)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 信息注入到請(qǐng)求頭
// ... _, err := modahttp.CallAPI(ctx, url, "POST", nil)
以上就是golang通用的grpc http基礎(chǔ)開(kāi)發(fā)框架使用快速入門(mén)的詳細(xì)內(nèi)容,更多關(guān)于golang grpc http開(kāi)發(fā)框架的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Golang中string的實(shí)現(xiàn)原理與高效使用
在Go語(yǔ)言中,無(wú)論是字符串常量、字符串變量還是代碼中出現(xiàn)的字符串字面量,它們的類型都被統(tǒng)一設(shè)置為string,下面就跟隨小編一起來(lái)了解一下Golang中string的實(shí)現(xiàn)原理與高效使用吧2024-01-01
基于Go語(yǔ)言實(shí)現(xiàn)插入排序算法及優(yōu)化
插入排序是一種簡(jiǎn)單的排序算法。這篇文章將利用Go語(yǔ)言實(shí)現(xiàn)冒泡排序算法,文中的示例代碼講解詳細(xì),對(duì)學(xué)習(xí)Go語(yǔ)言有一定的幫助,需要的可以參考一下2022-12-12
Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露問(wèn)題解析
這篇文章主要介紹了Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
GoLang中的timer定時(shí)器實(shí)現(xiàn)原理分析
Timer中對(duì)外暴露的只有一個(gè)channel,這個(gè) channel也是定時(shí)器的核心。當(dāng)計(jì)時(shí)結(jié)束時(shí),Timer會(huì)發(fā)送值到channel中,外部環(huán)境在這個(gè) channel 收到值的時(shí)候,就代表計(jì)時(shí)器超時(shí)了,可與select搭配執(zhí)行一些超時(shí)邏輯2023-02-02
Ubuntu安裝Go語(yǔ)言運(yùn)行環(huán)境
由于最近偏愛(ài)Ubuntu,在加上作為一門(mén)開(kāi)源語(yǔ)言,在Linux上從源代碼開(kāi)始搭建環(huán)境更讓人覺(jué)得有趣味性。讓我們直接先從Go語(yǔ)言的環(huán)境搭建開(kāi)始2015-04-04
GO語(yǔ)言基本類型String和Slice,Map操作詳解
這篇文章主要為大家介紹了GO語(yǔ)言基本類型String和Slice,Map操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Go中阻塞以及非阻塞操作實(shí)現(xiàn)(Goroutine和main Goroutine)
本文主要介紹了Go中阻塞以及非阻塞操作實(shí)現(xiàn)(Goroutine和main Goroutine),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
golang用melody搭建輕量的websocket服務(wù)的示例代碼
在Go中,可以使用gin和melody庫(kù)來(lái)搭建一個(gè)輕量級(jí)的WebSocket服務(wù),gin是一個(gè)流行的Web框架,而melody是一個(gè)用于處理WebSocket的庫(kù),本文給大家演示如何使用gin和melody搭建WebSocket服務(wù),感興趣的朋友一起看看吧2023-10-10

