贵州水利建设官方网站,性价比高的广州网站建设,学校招办网站怎么做,h5网站怎么做的吗go-moda
golang 通用的 grpc http 基础开发框架仓库地址: https://github.com/webws/go-moda仓库一直在更新,欢迎大家吐槽和指点
特性
transport: 集成 http#xff08;echo、gin#xff09;和 grpc。tracing: openTelemetry 实现微务链路追踪pprof: 分析性能config: 通用…go-moda
golang 通用的 grpc http 基础开发框架仓库地址: https://github.com/webws/go-moda仓库一直在更新,欢迎大家吐槽和指点
特性
transport: 集成 httpecho、gin和 grpc。tracing: openTelemetry 实现微务链路追踪pprof: 分析性能config: 通用的配置文件读取模块支持 toml、yaml 和 json 格式。logger: 日志系统模块基于 Zap,并支持全局日志和模块日志。
快速使用
conf.toml
http_addr :8081
grpc_addr :8082启用http(gin) 和 grpc服务
package mainimport (contextnet/httpgithub.com/gin-gonic/ginapp github.com/webws/go-modagithub.com/webws/go-moda/configpbexample github.com/webws/go-moda/example/pb/examplegithub.com/webws/go-moda/loggermodagrpc github.com/webws/go-moda/transport/grpcmodahttp github.com/webws/go-moda/transport/http
)var ServerName stringtype Config struct {HttpAddr string json:http_addr toml:http_addrGrpcAddr 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 servergin, httpSrv : modahttp.NewGinHttpServer(modahttp.WithAddress(conf.HttpAddr),)registerHttp(gin)// grpc servergrpcSrv : modagrpc.NewServer(modagrpc.WithServerAddress(conf.GrpcAddr),)grecExample : ExampleServer{}pbexample.RegisterExampleServiceServer(grpcSrv, grecExample)// app runa : 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/helloworldgrpc 服务 使用 gRPC 客户端调用 SayHello 方法
其他服务启用示例
echo http :example_echonet http :example_echogrpc example_grpc
pprof 性能分析
启动服务默认开启 pprof 性能分析浏览器打开 http://localhost:8081/debug/ 查看 可视化分析 gouroutine
go tool pprof http://localhost:8081/debug/pprof/goroutine
(pprof) web可能提示 需要先安装 graphviz, mac 下可以使用 brew 安装
brew install graphviztracing 链路追踪
使用 opentelemetry 实现微服务链路追踪目前 exporter 支持 jaeger示例集成了docker 环境,支持 make deploy 同时启动 jaeger,api1,api2,api3,grpc 服务详细示例请看:tracing_example
初始化 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())//...
}在代码主动tracing start ctx, span : tracing.Start(c.Request().Context(), api1)defer span.End()服务之间调用 产生的链路
server端: 增加 WithTracing 即可 //...gin, httpSrv : modahttp.NewGinHttpServer(modahttp.WithAddress(conf.HttpAddr),modahttp.WithTracing(true),)client端: 封装了 CallAPI 方法, 已将span ctx 信息注入到请求头 // ..._, err : modahttp.CallAPI(ctx, url, POST, nil)