Go - how to write the ProtoBuf plug-in?

 

preface

Last article Go - how to write ProtoBuf plug-in (II) , shared the definition of the "interceptor" plug-in based on "custom options", and then in "HelloWorld The plug-in is used in proto , and finally the plug-in information is obtained in the , golang , code.

Continue to share.

Now that we have the plug-in information, we can use them. This article is mainly shared in grpc Grpc. In serveroption ( Unaryinterceptor.

Demo code

Or HelloWorld Proto.

// Generate HelloWorld pb. go
// Generate helloworld_grpc.pb.go
// The protocol -- version used is libprotocol 3.18.1
// The protocol Gen go -- version used is protocol Gen go v1 twenty-seven point one
// The used protocol Gen go grpc -- version is protocol Gen go grpc 1.1.0
// Execute the protoc ol command in the root directory

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

1, Modify the code based on the "options" obtained in the previous article, mainly by storing it into the structure.

// Demo code, structure

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),
}

2, In grpc Use interceptors in newServer #.

// Demo code

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

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

resolveFileDescriptor() // Resolve options extension

3, In the {unaryServerInterceptor() method, you can get the} options set correspondingly according to the currently requested service name and method name.

// Demo code

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

// Get service options
getServiceHandler(serviceName)

// Get method options
getMethodHandler(info.FullMethod)

4, Write a grpcclient call by yourself.

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

So far, in grpc Unaryinterceptor , you can get , options , and I won't post other demonstration codes.

Finally, you can execute the specific methods defined by yourself through the} options obtained.

Summary

Through the recent three articles on "how to write ProtoBuf plug-in", I believe you have a little understanding of writing ProtoBuf plug-in, and I hope it can be helpful to you.

Recommended reading

Author: Xinliang notes (attention to the official account, you can apply for WeChat friends).
source: https://www.cnblogs.com/xinliangcoder
The copyright of this article belongs to the author and the blog park. Reprint is welcome, but this statement must be retained without the consent of the author, and the original connection must be given in an obvious position on the article page, otherwise the right to investigate legal responsibility is reserved.

 
Turn https://www.cnblogs.com/xinliangcoder/p/15810637.html

Keywords: Go

Added by bskauge on Wed, 19 Jan 2022 11:16:01 +0200