Go - 如何编写 ProtoBuf 插件(二)?
2024-09-03 02:52:36
前言
上篇文章《Go - 如何编写 ProtoBuf 插件 (一) 》,分享了使用 proto3
的 自定义选项
可以实现插件的编写,说到基于 MethodOptions
和 ServiceOptions
选项去实现 method
和 service
自定义设置拦截器。
接上篇文章,继续分享。
定义插件
// plugin/interceptor/options/interceptor.proto
syntax = "proto3";
package interceptor;
option go_package = "./;interceptor/options";
import "google/protobuf/descriptor.proto";
extend google.protobuf.MethodOptions {
optional MethodHandler method_handler = 63500;
}
extend google.protobuf.ServiceOptions {
optional ServiceHandler service_handler = 63501;
}
message MethodHandler {
optional string authorization = 1; // login token
optional string whitelist = 2; // ip whitelist
optional bool logger = 3; // logger
}
message ServiceHandler {
optional string authorization = 1; // login token
optional string whitelist = 2; // ip whitelist
optional bool logger = 3; // logger
}
接下来根据 interceptor.proto
生成 interceptor.pb.go
// 生成 interceptor.pb.go
// 使用的 protoc --version 为 libprotoc 3.18.1
// 使用的 protoc-gen-go --version 为 protoc-gen-go v1.27.1
// 在 plugin/interceptor/options 目录下执行 protoc 命令
protoc --go_out=. interceptor.proto
使用插件
// helloworld/helloworld.proto
syntax = "proto3";
package helloworld;
option go_package = "./;helloworld";
import "plugin/interceptor/options/interceptor.proto";
service Greeter {
option (interceptor.service_handler) = {
authorization : "login_token",
};
rpc SayHello1 (HelloRequest) returns (HelloReply) {
option (interceptor.method_handler) = {
whitelist : "ip_whitelist",
logger: true,
};
}
rpc SayHello2 (HelloRequest) returns (HelloReply) {
option (interceptor.method_handler) = {
logger: false,
};
}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
接下来根据 helloworld.proto
生成 helloworld.pb.go
// 生成 helloworld.pb.go
// 使用的 protoc --version 为 libprotoc 3.18.1
// 使用的 protoc-gen-go --version 为 protoc-gen-go v1.27.1
// 在根目录下执行 protoc 命令
protoc --go_out=helloworld/gen helloworld/helloworld.proto
获取自定义选项
// main.go
// 演示代码
package main
import (
"fmt"
"strconv"
_ "github.com/xinliangnote/protobuf/helloworld/gen"
"github.com/xinliangnote/protobuf/plugin/interceptor/options"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
func main() {
protoregistry.GlobalFiles.RangeFiles(func(fd protoreflect.FileDescriptor) bool {
services := fd.Services()
for i := 0; i < services.Len(); i++ {
service := services.Get(i)
if serviceHandler, _ := proto.GetExtension(service.Options(), options.E_ServiceHandler).(*options.ServiceHandler); serviceHandler != nil {
fmt.Println()
fmt.Println("--- service ---")
fmt.Println("service name: " + string(service.FullName()))
if serviceHandler.Authorization != nil && *serviceHandler.Authorization != "" {
fmt.Println("use interceptor authorization: " + *serviceHandler.Authorization)
}
fmt.Println("--- service ---")
}
methods := service.Methods()
for k := 0; k < methods.Len(); k++ {
method := methods.Get(k)
if methodHandler, _ := proto.GetExtension(method.Options(), options.E_MethodHandler).(*options.MethodHandler); methodHandler != nil {
fmt.Println()
fmt.Println("--- method ---")
fmt.Println("method name: " + string(method.FullName()))
if methodHandler.Whitelist != nil && *methodHandler.Whitelist != "" {
fmt.Println("use interceptor whitelist: " + *methodHandler.Whitelist)
}
if methodHandler.Logger != nil {
fmt.Println("use interceptor logger: " + strconv.FormatBool(*methodHandler.Logger))
}
fmt.Println("--- method ---")
}
}
}
return true
})
}
输出:
--- service ---
service name: helloworld.Greeter
use interceptor authorization: login_token
--- service ---
--- method ---
method name: helloworld.Greeter.SayHello1
use interceptor whitelist: ip_whitelist
use interceptor logger: true
--- method ---
--- method ---
method name: helloworld.Greeter.SayHello2
use interceptor logger: false
--- method ---
小结
本文主要内容是基于 自定义选项
定义了 interceptor
插件,然后在 helloworld.proto
中使用了插件,最后在 golang
代码中获取到使用的插件信息。
接下来,要对获取到的插件信息进行使用,主要用在 grpc.ServerOption
中,例如在 grpc.UnaryInterceptor
和 grpc.StreamInterceptor
中使用。
推荐阅读
- Go - 如何编写 ProtoBuf 插件 (一) ?
- Go - 关于 protoc 工具的小疑惑
- Go - 关于 .proto 文件的小思考
- Go - 基于逃逸分析来提升程序性能
- Go - 使用 sync.Map 解决 map 并发安全问题
最新文章
- Pig语言基础-【持续更新中】
- git 学习使用总结三(远程仓库操作)
- cf 710E dp
- 手机触摸touch事件
- java io流 创建文件、写入数据、设置输出位置
- Motorola C118修改滤波器组件
- 《Velocity 模板使用指南》中文版[转]
- UltraChart导出图片
- TCP粘包拆包问题
- wemall app商城源码中基于PHP的ThinkPHP惯例配置文件代码
- Ubuntu16.04下Office替代品Office Online
- hdu 5909 Tree Cutting [树形DP fwt]
- ftp研究
- java框架之SpringBoot(16)-分布式及整合Dubbo
- day9--队列queue
- 前端和后台BUG区分方法
- 使用JAX-WS开发WebService
- CSS3 过渡动画
- JAVA字符串格式化-String.format()的使用【转】
- 葡萄城报表模板库再次更新!补充医院Dashboard及房地产销售行业报表
热门文章
- [atAGC050A]AtCoder Jumper
- web渗透工程师学习
- 从 [P4240 毒瘤之神的考验] 谈 OI 中的美学
- 区分wsgi、uWSGI、uwsgi、php-fpm、CGI、FastCGI
- 毕业设计压力测试——jmeter
- android Fragment跳转Fragment
- Python中pymysql基本使用
- C++面试基础篇(二)
- Beautiful Soup解析库的安装和使用
- Spring Cloud 2021.0.0 正式发布,第一个支持Spring Boot 2.6的版本!