dongsheng1698 2018-04-23 03:57
浏览 45

微型包装器即服务

Question is about using go-micro wrapper as a separate service - if anyone knows how to use it properly please let me know. my example - authWrapper, so all api services should be able to use it, it should be discovered via standard service discovery, to make any changes to authWrapper only 1 service should be rebuild (I didn't find a way how to properly pass context.Context from api service to authWrapper via rpc call)

go-micro docs

go-micro wrapper examples

api's code where authWrapper gets called:

func main() {
    service := micro.NewService(
        micro.Name("go.micro.api.account"),
        micro.WrapHandler(AuthWrapper),
    )
    fmt.Println("service created")
    service.Init()
    account.RegisterAccountHandler(service.Server(),
        &handler.Account{
            ProfileServiceClient: profile.NewProfileServiceClient("go.micro.srv.profile", service.Client()),
            AuthServiceClient:    auth.NewAuthServiceClient("go.micro.srv.auth", service.Client()),
        })

    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

and authWrapper:

var methodsWithoutAuth = map[string]bool{"Account.Auth": true, "Account.Create": true}

func AuthWrapper(fn server.HandlerFunc) server.HandlerFunc {
    return func(ctx context.Context, req server.Request, resp interface{}) error {
        fmt.Printf("AuthWrapper, req: %+v", req)

        method := req.Method()
        fmt.Printf("checking if method allowed, method: %+v", method)
        if _, ok := methodsWithoutAuth[method]; ok {
            return fn(ctx, req, resp)
        }

        fmt.Printf("validating token")

        authClient := auth.NewAuthServiceClient("go.micro.srv.auth", client.DefaultClient)

        meta, ok := metadata.FromContext(ctx)
        if !ok {
            return errors.New("no auth meta-data found in request")
        }

        token := meta["Token"]
        log.Println("Authenticating with token: ", token)

        newCtx := context.WithValue(ctx, "Method", req.Method())

        _, err := authClient.ValidateToken(newCtx, &auth.Token{Token: token})
        if err != nil {
            return err
        }

        prof, err := authClient.Decode(newCtx, &auth.Token{Token: token})
        if err != nil {
            return err
        }

        newCtxWithProf := context.WithValue(newCtx, "Profile", prof.Profile)

        return fn(newCtxWithProf, req, resp)
    }
}
  • 写回答

1条回答 默认 最新

  • doudou6719 2018-06-19 20:23
    关注

    You can write service wrappers by incorporating the go-micro client in the wrapper code. You find on github many examples how to write a go-micro client, I believe there is one in the greeter example in the go-micro repository.

    I use a wrapper to disclose a grpc-interface to a rest-service wrapper by using the client boilerplate.

    You can write wrappers to a micro-service for almost any purpose in this way.

    Don't worry about the ports the client code needs to address, Consul can handle this just fine for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图