Go-Micro微服务装饰器wrapper使用

xiaohai 2021-05-09 22:31:20 2295人围观 标签: Go 
简介Go-Micro微服务装饰器wrapper使用

go-micro装饰器wrapper使用

package main

import (
    "context"
    "github.com/gin-gonic/gin"
    "github.com/micro/go-micro"
    "github.com/micro/go-micro/client"
    "github.com/micro/go-micro/registry"
    "github.com/micro/go-micro/web"
    "github.com/micro/go-plugins/registry/consul"
    "log"
    services "micro-home/service/model"
    "strconv"
)

func InitMiddleware(service services.StudentListService) gin.HandlerFunc {
    return func(context *gin.Context) {
        context.Keys = make(map[string]interface{})
        context.Keys["student_service"] = service
        context.Next()
    }
}

//装饰器
type logWrapper struct {
    client.Client
}

//每次调用都会执行这个方法
func (c *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
    log.Print("请求")
    return c.Client.Call(ctx, req, rsp)
}
func NewLogWrapper(c client.Client) client.Client {
    return &logWrapper{c}
}

func main() {
    register := consul.NewRegistry(func(options *registry.Options) {
        options.Addrs = []string{
            "192.168.1.171:8500",
            "192.168.1.177:8500",
            "192.168.1.178:8500",
        }
    })
    //微服务需要注册
    homeService := micro.NewService(
        micro.Name("micro-home-client"),
        micro.Registry(register),
        micro.WrapClient(NewLogWrapper), //在注册时只需要传入方法名即可,底层会自动给这个方法传入client
    )
    //实例化服务
    studentListService := services.NewStudentListService("student-service", homeService.Client())

    routers := gin.Default()

    routers.Use(InitMiddleware(studentListService)) //使用中间件,将服务传递到context中

    routers.GET("/", func(context *gin.Context) {
        service := context.Keys["student_service"].(services.StudentListService)
        number := context.DefaultQuery("number", "0")
        num, err := strconv.Atoi(number)
        if err != nil {
            num = 1
        }
        if num <= 0 {
            num = 1
        }
        studentListResponse, err := service.GetStudentListService(context, &services.StudentListRequest{
            Num: int32(num),
        })
        if err != nil {
            context.JSON(200, gin.H{
                "status": "",
                "data":   err.Error(),
            })
        } else {
            context.JSON(200, gin.H{
                "status": studentListResponse.Status,
                "data":   studentListResponse.Data,
            })
        }
    })

    service := web.NewService(
        web.Name("home-service-api"),
        web.Address(":8081"),
        web.Handler(routers),
        web.Registry(register),
    )

    service.Run()
}