1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        gmsecGolang 微服務(wù)集成框架

        聯(lián)合創(chuàng)作 · 2023-09-28 04:56

        gmsec 是一款 Golang 微服務(wù)集成框架。

        特點

        • 打通grpc + gin,同時支持grpc 跟 restful 模式
        • grpc , gin 公用端口
        • gorm 嵌入,自動數(shù)據(jù)庫代碼生成

        golang 微服務(wù)集成框架

        安裝

        • install

        • proto環(huán)境安裝

         make install 
        
        • 本地環(huán)境搭建(gmsec為例)
        make gen
        
        • 新建一個服務(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) // 對象注冊
        	// ------ 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ù)注冊時間
                // micro.WithRegisterInterval(time.Second*15), //讓服務(wù)在指定時間內(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

        傳送門

        瀏覽 22
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        編輯 分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        編輯 分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            91人人澡| 视频在线三区 | 男人操女人网站 | 欧洲a视频 | 青青草在线成人视频 | 国产主播大尺度精品福利软件 | 东京热久久 | 掀开裙子从后面猛地挺进公交车里 | 凉森996无码中出在线播放 | 与俄罗斯老妇做爰 |