gmsec是一款Golang微服务集成框架。
特点打通grpc+gin,同时支持grpc跟restful模式grpc,gin公用端口gorm嵌入,自动数据库代码生成golang微服务集成框架grpcgorm自动构建(gormt)gin参数自动绑定(ginrpc)dns注册发现(mdns)markdown/mindoc文档自动导出安装install
proto环境安装
makeinstall本地环境搭建(gmsec为例)makegen新建一个服务makenewservice=[服务名]proto定义syntax="proto3";//指定proto版本packageproto;//指定包名optiongo_package=".;proto";//指定路径//定义Hello服务serviceHello{//定义SayHello方法rpcSayHello(HelloRequest)returns(HelloReply){}}//HelloRequest请求结构messageHelloRequest{stringname=1;//名字}//HelloReply响应结构messageHelloReply{stringmessage=1;//消息}服务端代码示例packagemainimport("context""fmt"proto"gmsec/rpc/gmsec""github.com/gmsec/goplugins/api""github.com/gin-gonic/gin""github.com/gmsec/goplugins/plugin""github.com/gmsec/micro""github.com/xxjwxc/ginrpc")funcmain(){//grpc相关初始化服务service:=micro.NewService(micro.WithName("lp.srv.eg1"),)h:=new(hello)proto.RegisterHelloServer(service.Server(),h)//服务注册//-----------end//gin相关base:=ginrpc.New(ginrpc.WithCtx(api.NewAPIFunc),ginrpc.WithDebug(dev.IsDev()))router:=gin.Default()v1:=router.Group("/xxjwxc/api/v1")base.Register(v1,h)//对象注册//------endplg,_:=plugin.Run(plugin.WithMicro(service),//grpc入口plugin.WithGin(router),//http入口plugin.WithAddr(":8080"))//开始服务(公用端口)plg.Wait()//等待结束(ctrl+c)fmt.Println("done")}//Ctxgin.Context到context.Context的转换funcCtx(c*gin.Context)interface{}{returncontext.Background()}客户端代码:packagemainimport("context""fmt"proto"gmsec/rpc/gmsec""github.com/gmsec/micro")funcmain(){//reg:=registry.NewDNSNamingRegistry()//grpc相关注册服务发现等micro.NewService(micro.WithName("lp.srv.eg1"),//micro.WithRegisterTTL(time.Second*30),//指定服务注册时间//micro.WithRegisterInterval(time.Second*15),//让服务在指定时间内重新注册//micro.WithRegistryNaming(reg),)//-----------endsay:=proto.GetHelloClient()ctx:=context.Background()resp,_:=say.SayHello(ctx,&proto.HelloRequest{Name:"xxjwxc"})fmt.Println("result:",resp)}更多示例=> 传送门正在做etcdv3传送门
评论