golang通用的grpc http基础开发框架使用快速入门

 更新时间:2023年09月03日 10:29:56   作者:莫大  
这篇文章主要为大家介绍了golang通用的grpc http基础开发框架使用快速入门详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

go-moda

golang 通用的 grpc http 基础开发框架

仓库地址: [https://github.com/webws/go-moda]

仓库一直在更新,欢迎大家吐槽和指点

特性

  • transport: 集成 http(echo、gin)和 grpc。
  • tracing: openTelemetry 实现微务链路追踪
  • pprof: 分析性能
  • config: 通用的配置文件读取模块,支持 toml、yaml 和 json 格式。

logger: 日志系统模块,基于 Zap,并支持全局日志和模块日志。

快速使用

conf.toml

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

启用http(gin) 和 grpc服务

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 服务 使用 gRPC 客户端调用 SayHello 方法

其他服务启用示例

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 性能分析

启动服务默认开启 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 实现微服务链路追踪,目前 exporter 支持 jaeger 

* 示例集成了docker 环境,支持 make deploy 同时启动 jaeger,api1,api2,api3,grpc 服务

* 详细示例请看:[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. 服务之间调用 产生的链路   

*  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基础开发框架使用快速入门的详细内容,更多关于golang grpc http开发框架的资料请关注脚本之家其它相关文章!

相关文章

  • 详解Golang中string的实现原理与高效使用

    详解Golang中string的实现原理与高效使用

    在Go语言中,无论是字符串常量、字符串变量还是代码中出现的字符串字面量,它们的类型都被统一设置为string,下面就跟随小编一起来了解一下Golang中string的实现原理与高效使用吧
    2024-01-01
  • 基于Go语言实现插入排序算法及优化

    基于Go语言实现插入排序算法及优化

    插入排序是一种简单的排序算法。这篇文章将利用Go语言实现冒泡排序算法,文中的示例代码讲解详细,对学习Go语言有一定的帮助,需要的可以参考一下
    2022-12-12
  • Go 中 time.After 可能导致的内存泄露问题解析

    Go 中 time.After 可能导致的内存泄露问题解析

    这篇文章主要介绍了Go 中 time.After 可能导致的内存泄露,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • GoLang中的timer定时器实现原理分析

    GoLang中的timer定时器实现原理分析

    Timer中对外暴露的只有一个channel,这个 channel也是定时器的核心。当计时结束时,Timer会发送值到channel中,外部环境在这个 channel 收到值的时候,就代表计时器超时了,可与select搭配执行一些超时逻辑
    2023-02-02
  • Ubuntu安装Go语言运行环境

    Ubuntu安装Go语言运行环境

    由于最近偏爱Ubuntu,在加上作为一门开源语言,在Linux上从源代码开始搭建环境更让人觉得有趣味性。让我们直接先从Go语言的环境搭建开始
    2015-04-04
  • GO语言基本类型String和Slice,Map操作详解

    GO语言基本类型String和Slice,Map操作详解

    这篇文章主要为大家介绍了GO语言基本类型String和Slice,Map操作示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Golang中的自定义函数详解

    Golang中的自定义函数详解

    函数构成代码执行的逻辑结构。在Go语言中,函数的基本组成为:关键字func、函数名、参数列表、返回值、函数体和返回语句。
    2018-10-10
  • Go语言中字符串赋值中的问题与解决方法

    Go语言中字符串赋值中的问题与解决方法

    这篇文章主要为大家详细介绍了Go语言中字符串赋值会出现的一些问题以及解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下
    2024-12-12
  • Go中阻塞以及非阻塞操作实现(Goroutine和main Goroutine)

    Go中阻塞以及非阻塞操作实现(Goroutine和main Goroutine)

    本文主要介绍了Go中阻塞以及非阻塞操作实现(Goroutine和main Goroutine),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05
  • golang用melody搭建轻量的websocket服务的示例代码

    golang用melody搭建轻量的websocket服务的示例代码

    在Go中,可以使用gin和melody库来搭建一个轻量级的WebSocket服务,gin是一个流行的Web框架,而melody是一个用于处理WebSocket的库,本文给大家演示如何使用gin和melody搭建WebSocket服务,感兴趣的朋友一起看看吧
    2023-10-10

最新评论