gmsecGolang 微服務(wù)集成框架
gmsec 是一款 Golang 微服務(wù)集成框架。
特點(diǎn)
- 打通grpc + gin,同時(shí)支持grpc 跟 restful 模式
- grpc , gin 公用端口
- gorm 嵌入,自動(dòng)數(shù)據(jù)庫代碼生成
golang 微服務(wù)集成框架
- grpc
- gorm 自動(dòng)構(gòu)建(gormt)
- gin 參數(shù)自動(dòng)綁定(ginrpc)
- dns 注冊發(fā)現(xiàn)(mdns)
- markdown/mindoc 文檔自動(dòng)導(dǎo)出
安裝
-
install
-
proto環(huán)境安裝
make install
- 本地環(huán)境搭建(gmsec為例)
make gen
- 新建一個(gè)服務(wù)
make new service=[服務(wù)名]
proto定義
syntax = "proto3"; // 指定proto版本
package proto; // 指定包名
option go_package = ".;proto"; // 指定路徑
// 定義Hello服務(wù)
service Hello {
// 定義SayHello方法
rpc SayHello(HelloRequest) returns (HelloReply) {}
}
// HelloRequest 請求結(jié)構(gòu)
message HelloRequest {
string name = 1; // 名字
}
// HelloReply 響應(yīng)結(jié)構(gòu)
message HelloReply {
string message = 1; // 消息
}
服務(wù)端代碼示例
package main
import (
"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"
)
func main() {
// grpc 相關(guān) 初始化服務(wù)
service := micro.NewService(
micro.WithName("lp.srv.eg1"),
)
h := new(hello)
proto.RegisterHelloServer(service.Server(), h) // 服務(wù)注冊
// ----------- end
// gin 相關(guān)
base := ginrpc.New(ginrpc.WithCtx(api.NewAPIFunc), ginrpc.WithDebug(dev.IsDev()))
router := gin.Default()
v1 := router.Group("/xxjwxc/api/v1")
base.Register(v1, h) // 對(duì)象注冊
// ------ end
plg, _ := plugin.Run(plugin.WithMicro(service),// grpc 入口
plugin.WithGin(router), // http 入口
plugin.WithAddr(":8080")) // 開始服務(wù)(公用端口)
plg.Wait() // 等待結(jié)束(ctrl+c)
fmt.Println("done")
}
// Ctx gin.Context 到 context.Context 的轉(zhuǎn)換
func Ctx(c *gin.Context) interface{} {
return context.Background()
}
客戶端代碼:
package main
import (
"context"
"fmt"
proto "gmsec/rpc/gmsec"
"github.com/gmsec/micro"
)
func main() {
// reg := registry.NewDNSNamingRegistry()
// grpc 相關(guān) 注冊服務(wù)發(fā)現(xiàn)等
micro.NewService(
micro.WithName("lp.srv.eg1"),
// micro.WithRegisterTTL(time.Second*30), //指定服務(wù)注冊時(shí)間
// micro.WithRegisterInterval(time.Second*15), //讓服務(wù)在指定時(shí)間內(nèi)重新注冊
// micro.WithRegistryNaming(reg),
)
// ----------- end
say := proto.GetHelloClient()
ctx := context.Background()
resp, _ := say.SayHello(ctx, &proto.HelloRequest{Name:"xxjwxc"})
fmt.Println("result:", resp)
}
更多示例 => 傳送門
正在做
- etcdv3
傳送門
評(píng)論
圖片
表情
