V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xxjwxc
V2EX  ›  程序员

推荐一款 golang 微服务集成框架

  •  
  •   xxjwxc ·
    xxjwxc · 2020-05-13 11:50:05 +08:00 · 2665 次点击
    这是一个创建于 1416 天前的主题,其中的信息可能已经有所发展或是发生改变。

    golang 微服务集成框架

    gmsec

    特点

    • 打通 grpc + gin,同时支持 grpc 跟 restful 模式
    • grpc , gin 公用端口
    • gorm 嵌入,自动数据库代码生成

    支持列表

    安装

    • install

    • proto 环境安装

     make install 
    
    • 本地环境搭建(gmsec 为例)
    make gen
    

    proto 定义

    syntax = "proto3"; // 指定 proto 版本
    package proto;     // 指定包名
    option go_package = ".;proto"; // 指定路径
    
    // 定义 Hello 服务
    service Hello {
        // 定义 SayHello 方法
        rpc SayHello(HelloRequest) returns (HelloReply) {}
    }
    // HelloRequest 请求结构
    message HelloRequest {
        string name = 1; // 名字
    }
    // HelloReply 响应结构
    message HelloReply {
        string message = 1; // 消息
    }
    

    服务端代码示例

    package main
    
    import (
    	"context"
    	"fmt"
    	proto "gmsec/rpc"
    
    	"github.com/gin-gonic/gin"
    	"github.com/gmsec/goplugins/plugin"
    	"github.com/gmsec/micro"
    	"github.com/xxjwxc/ginrpc"
    )
    
    func main() {
    	// grpc 相关 初始化服务
    	service := micro.NewService(
    		micro.WithName("lp.srv.eg1"),
    	)
    	h := new(hello)
    	proto.RegisterHelloServer(service.Server(), h) // 服务注册
    	// ----------- end
    
    	// gin 相关
    	base := ginrpc.New(ginrpc.WithCtx(Ctx), ginrpc.WithDebug(true), ginrpc.WithGroup("xxjwxc"))
    	router := gin.Default()  // gen 对象
    	base.Register(router, h) // genrpc 对象注册
    	// ------ end
    
    	plg, _ := plugin.Run(plugin.WithMicro(service),// grpc 入口
    		plugin.WithGin(router), // http 入口
            plugin.WithAddr(":8080")) // 开始服务(公用端口)
        
        plg.Wait() // 等待结束(ctrl+c)
        
    	fmt.Println("done")
    }
    
    // Ctx gin.Context 到 context.Context 的转换
    func Ctx(c *gin.Context) interface{} {
    	return context.Background()
    }
    

    客户端代码:

    package main
    
    import (
    	"context"
    	"fmt"
    	proto "gmsec/rpc"
    
    	"github.com/gmsec/micro"
    )
    
    func main() {
        // reg := registry.NewDNSNamingRegistry()
    	// grpc 相关 注册服务发现等
    	micro.NewService(
            micro.WithName("lp.srv.eg1"),
            // micro.WithRegisterTTL(time.Second*30),      //指定服务注册时间
            // micro.WithRegisterInterval(time.Second*15), //让服务在指定时间内重新注册
            // micro.WithRegistryNameing(reg),
    	)
    	// ----------- end
    
    	say := proto.GetHelloClient()
    	ctx := context.Background()
    	resp, _ := say.SayHello(ctx, &proto.HelloRequest{Name:"xxjwxc"})
    	fmt.Println("result:", resp)
    }
    

    更多示例 => 传送门

    正在做

    • etcdv3

    欢迎一起共建共享

    传送门

    4 条回复    2020-05-13 19:19:41 +08:00
    qq1340691923
        1
    qq1340691923  
       2020-05-13 17:16:39 +08:00
    顶一个!
    xxjwxc
        2
    xxjwxc  
    OP
       2020-05-13 17:45:09 +08:00
    @qq1340691923 感谢 ✨
    yph007595
        3
    yph007595  
       2020-05-13 18:58:33 +08:00
    go-micro
    xxjwxc
        4
    xxjwxc  
    OP
       2020-05-13 19:19:41 +08:00
    @yph007595 这个框架就是参考的 go-micro,去掉了很多 go-micro 复杂的功能。比如自定义 rpc
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2428 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 15:57 · PVG 23:57 · LAX 08:57 · JFK 11:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.