Go - 如何编写 ProtoBuf 插件 (三) ?

前言

上篇文章《Go - 如何编写 ProtoBuf 插件 (二) 》,分享了基于 自定义选项 定义了 interceptor 插件,然后在 helloworld.proto 中使用了插件,最后在 golang 代码中获取到使用的插件信息。

接上篇,继续分享。

既然获取到了插件信息,我们就可以使用它们。本文主要分享在 grpc.ServerOption 中的 grpc.UnaryInterceptor 中使用。

演示代码

还是以上篇文章中 helloworld.proto 为例。

// 生成 helloworld.pb.go
// 生成 helloworld_grpc.pb.go
// 使用的 protoc --version 为 libprotoc 3.18.1
// 使用的 protoc-gen-go --version 为 protoc-gen-go v1.27.1
// 使用的 protoc-gen-go-grpc --version 为 protoc-gen-go-grpc 1.1.0
// 在根目录下执行 protoc 命令

protoc --go_out=helloworld/gen --go-grpc_out=helloworld/gen helloworld/helloworld.proto

一、基于上篇文章中获取 options 的代码进行修改,主要是将其存入到结构体即可。

// 演示代码,结构体

var handlers = &struct {
	Methods  map[string]*options.MethodHandler  // FullMethod : Handler
	Services map[string]*options.ServiceHandler // FullMethod : Handler
}{
	Methods:  make(map[string]*options.MethodHandler),
	Services: make(map[string]*options.ServiceHandler),
}

二、在 grpc.NewServer 中使用拦截器。

// 演示代码

serverOptions := []grpc.ServerOption{
	grpc.UnaryInterceptor(unaryServerInterceptor()),
}

srv := grpc.NewServer(serverOptions...)

resolveFileDescriptor() // 解析 options 扩展项

三、在 unaryServerInterceptor() 方法中,可以根据当前请求的服务名和方法名获取到对应设置的 options

// 演示代码

fullMethod := strings.Split(info.FullMethod, "/")
serviceName := fullMethod[1]

// 获取 service options
getServiceHandler(serviceName)

// 获取 method options
getMethodHandler(info.FullMethod)

四、自己写一个 grpcclient 调用一下即可。

--- /helloworld.Greeter/SayHello1 ---
service use interceptor authorization: login_token
method use interceptor whitelist: ip_whitelist
method use interceptor logger: true

至此,在 grpc.UnaryInterceptor 中就可以获取到 options 了,其他演示代码我就不贴了。

最后,通过获取到的 options,便可以执行自己定义的具体方法。

小结

通过最近的 “如何编写 ProtoBuf 插件” 这三篇文章,相信你对编写 ProtoBuf 插件有一点小的认识,希望对你能够有所帮助。

推荐阅读

posted @ 2022-01-16 17:06  程序员新亮  阅读(390)  评论(0编辑  收藏  举报