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

前言

上篇文章《Go - 如何编写 ProtoBuf 插件 (一) 》,分享了使用 proto3自定义选项 可以实现插件的编写,说到基于 MethodOptionsServiceOptions 选项去实现 methodservice 自定义设置拦截器。

接上篇文章,继续分享。

定义插件

// 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.UnaryInterceptorgrpc.StreamInterceptor 中使用。

推荐阅读


新亮笔记
技术的深度和广度只能靠自己努力去发掘,谁也不能替你学习,在这里希望你能有所收获。

日拱一卒

2.9k 声望
1.2k 粉丝
0 条评论
推荐阅读
关于项目中 Repository 层的思考
维护这一层的开发者,可以称为 仓库管理员 ,当使用者需要查询数据的时候,需要告诉仓库管理员,由仓库管理员拿给他,至于仓库管理员从哪拿的数据,使用者无需关系。

新亮2阅读 1.8k评论 1

前端如何入门 Go 语言
类比法是一种学习方法,它是通过将新知识与已知知识进行比较,从而加深对新知识的理解。在学习 Go 语言的过程中,我发现,通过类比已有的前端知识,可以更好地理解 Go 语言的特性。

robin23阅读 3.3k评论 6

封面图
Golang 中 []byte 与 string 转换
string 类型和 []byte 类型是我们编程时最常使用到的数据结构。本文将探讨两者之间的转换方式,通过分析它们之间的内在联系来拨开迷雾。

机器铃砍菜刀24阅读 58.1k评论 2

年度最佳【golang】map详解
这篇文章主要讲 map 的赋值、删除、查询、扩容的具体执行过程,仍然是从底层的角度展开。结合源码,看完本文一定会彻底明白 map 底层原理。

去去100216阅读 11.6k评论 2

年度最佳【golang】GMP调度详解
Golang最大的特色可以说是协程(goroutine)了, 协程让本来很复杂的异步编程变得简单, 让程序员不再需要面对回调地狱, 虽然现在引入了协程的语言越来越多, 但go中的协程仍然是实现的是最彻底的. 这篇文章将通过分析...

去去100215阅读 11.9k评论 4

万字详解,吃透 MongoDB!
MongoDB 是一个基于 分布式文件存储 的开源 NoSQL 数据库系统,由 C++ 编写的。MongoDB 提供了 面向文档 的存储方式,操作起来比较简单和容易,支持“无模式”的数据建模,可以存储比较复杂的数据类型,是一款非常...

JavaGuide8阅读 1.7k

封面图
数据结构与算法:二分查找
一、常见数据结构简单数据结构(必须理解和掌握)有序数据结构:栈、队列、链表。有序数据结构省空间(储存空间小)无序数据结构:集合、字典、散列表,无序数据结构省时间(读取时间快)复杂数据结构树、 堆图二...

白鲸鱼9阅读 5.3k

日拱一卒

2.9k 声望
1.2k 粉丝
宣传栏