• go-micro使用Grpc


    go-micro使用Grpc

    服务端

    服务端目录

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yToLg8SX-1656157179131)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220625192733577.png)]

    Services/protos/Models.proto

    syntax = "proto3";
    package Services;
    option go_package = "./;Services";
    
    // 商品模型
    message ProdModel{
    
      int32 ProdID = 1;
    
      string ProdName = 2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Services/protos/ProdService.proto

    syntax = "proto3";
    package Services;
    option go_package = "./;Services";
    import "Models.proto";
    
    
    message ProdsRequest {
    
      int32 Size = 1;
    }
    
    message ProdListResponse{
      repeated ProdModel data = 1;
    }
    
    service ProdService {
      rpc GetProdsList(ProdsRequest) returns (ProdListResponse);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    启动gen.bat脚本

    cd Services/protos
    protoc --micro_out=../ --go_out=../ Models.proto
    protoc --micro_out=../ --go_out=../ ProdService.proto
    protoc-go-inject-tag -input=../Models.pb.go
    protoc-go-inject-tag -input=../ProdService.pb.go
    cd .. && cd ..
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ServiceImpl/ProdService.go 模拟数据

    package ServiceImpl
    
    import (
       "context"
       "fmt"
       "go_micro_grpc/Services"
       "strconv"
    )
    
    func NewProd(id int32, pname string) *Services.ProdModel {
       return &Services.ProdModel{
          ProdID:   id,
          ProdName: pname,
       }
    }
    
    type ProdService struct {
    }
    
    func (p *ProdService) GetProdsList(ctx context.Context, req *Services.ProdsRequest, resp *Services.ProdListResponse) error {
       ret := make([]*Services.ProdModel, 0)
       var i int32
       for i = 0; i < req.Size; i++ {
          ret = append(ret, NewProd(100+i, "prodname"+strconv.Itoa(100+int(i))))
       }
       resp.Data = ret
       fmt.Println(ret)
       return nil
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    main.go

    package main
    
    import (
       "github.com/asim/go-micro/plugins/registry/consul/v4"
       "go-micro.dev/v4"
       "go-micro.dev/v4/registry"
       "go_micro_grpc/ServiceImpl"
       "go_micro_grpc/Services"
    )
    
    func main() {
       consulReg := consul.NewRegistry(
          registry.Addrs("118.178.242.230:8500"),
       )
    
       prodService := micro.NewService(
          micro.Name("prodservice"),
          micro.Address(":8011"),
          micro.Registry(consulReg),
       )
    
       prodService.Init()
    
       Services.RegisterProdServiceHandler(prodService.Server(), new(ServiceImpl.ProdService))
    
       prodService.Run()
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    客户端

    客户端目录

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cMMAGQgY-1656157179133)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220625193401090.png)]

    Services/protos/Models.proto

    syntax = "proto3";
    package Services;
    option go_package = "./;Services";
    
    // 商品模型
    message ProdModel{
    
      int32 ProdID = 1;
    
      string ProdName = 2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Services/protos/ProdService.proto

    syntax = "proto3";
    package Services;
    option go_package = "./;Services";
    import "Models.proto";
    
    
    message ProdsRequest {
    
      int32 Size = 1;
    }
    
    message ProdListResponse{
      repeated ProdModel data = 1;
    }
    
    service ProdService {
      rpc GetProdsList(ProdsRequest) returns (ProdListResponse);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    启动gen.bat脚本

    cd Services/protos
    protoc --micro_out=../ --go_out=../ Models.proto
    protoc --micro_out=../ --go_out=../ ProdService.proto
    protoc-go-inject-tag -input=../Models.pb.go
    protoc-go-inject-tag -input=../ProdService.pb.go
    cd .. && cd ..
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    main.go

    package main
    
    import (
       "github.com/asim/go-micro/plugins/registry/consul/v4"
       "go-micro.dev/v4"
       "go-micro.dev/v4/registry"
       "go-micro.dev/v4/web"
       "go_micro1/Services"
       "go_micro1/Weblib"
    )
    
    
    
    func main() {
       consulReg := consul.NewRegistry(
          registry.Addrs("118.178.242.230:8500"),
       )
    
       myService := micro.NewService(
          micro.Name("prodservice.client"),
          
       )
       prodService := Services.NewProdService("prodservice", myService.Client())
    
       httpServer := web.NewService(
          web.Name("httpprodservice"),
          web.Address(":8001"),
          web.Handler(Weblib.NewGinRouter(prodService)),
          web.Registry(consulReg),
       )
    
       httpServer.Init()
    
       httpServer.Run()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    Weblib/router.go

    package Weblib
    
    import (
       "github.com/gin-gonic/gin"
       "go_micro1/Services"
    )
    
    func NewGinRouter(prodService Services.ProdService) *gin.Engine {
       ginRouter := gin.Default()
       ginRouter.Use(InitMiddleware(prodService))
    
       v1Group := ginRouter.Group("/v1")
       {
          v1Group.Handle("POST", "/prods", GetProdsList)
    
       }
       return ginRouter
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Weblib.handlers.go

    package Weblib
    
    import (
       "context"
       "fmt"
       "github.com/gin-gonic/gin"
       "go_micro1/Services"
    )
    
    func GetProdsList(ginCtx *gin.Context) {
       prodService := ginCtx.Keys["prodservice"].(Services.ProdService)
    
       var prodReq Services.ProdsRequest
       err := ginCtx.Bind(&prodReq)
    
       if err != nil {
          ginCtx.JSON(500, gin.H{"status": err.Error()})
       } else {
          prodResp, err := prodService.GetProdsList(context.Background(), &prodReq)
          if err != nil {
             fmt.Println(err)
          }
          ginCtx.JSON(200, gin.H{"data": prodResp.GetData()})
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    Weblib.middlewares.go

    package Weblib
    
    import (
       "github.com/gin-gonic/gin"
       "go_micro1/Services"
    )
    
    func InitMiddleware(prodService Services.ProdService) gin.HandlerFunc {
       return func(context *gin.Context) {
          context.Keys = make(map[string]interface{})
          context.Keys["prodservice"] = prodService
          context.Next()
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    注 : 使用中间件传递prodService.

    使用wapper装饰器 (添加日志等)

    package main
    
    import (
       "context"
       "fmt"
       "github.com/asim/go-micro/plugins/registry/consul/v4"
       "go-micro.dev/v4"
       "go-micro.dev/v4/client"
       "go-micro.dev/v4/registry"
       "go-micro.dev/v4/web"
       "go_micro1/Services"
       "go_micro1/Weblib"
    )
    
    type logWrapper struct {
       client.Client
    }
    
    func (this *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
       fmt.Println("调用接口")
       return this.Client.Call(ctx, req, rsp)
    }
    func NewLogWrapper(c client.Client) client.Client {
       return &logWrapper{c}
    }
    
    func main() {
       consulReg := consul.NewRegistry(
          registry.Addrs("118.178.242.230:8500"),
       )
    
       myService := micro.NewService(
          micro.Name("prodservice.client"),
          micro.WrapClient(NewLogWrapper),
       )
       prodService := Services.NewProdService("prodservice", myService.Client())
    
       httpServer := web.NewService(
          web.Name("httpprodservice"),
          web.Address(":8001"),
          web.Handler(Weblib.NewGinRouter(prodService)),
          web.Registry(consulReg),
       )
    
       httpServer.Init()
    
       httpServer.Run()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    使用零一万物 200K 模型和 Dify 快速搭建模型应用
    大数据量一次性导入MongoDB
    再见福昕,再见Adobe,最强PDF神器已现世!
    使用MONAI轻松加载医学公开数据集,包括医学分割十项全能挑战数据集和MedMNIST分类数据集
    【STL***vector容器一】
    几分钟上线一个项目文档网站,这款开源神器实在太香了~
    周围神经系统分哪几部分,神经系统按位置分类为
    konva系列教程3:自定义图形
    通信原理板块——时分复用
    408王道计算机网络强化——网络层
  • 原文地址:https://blog.csdn.net/qq_53267860/article/details/125463339