golang grpc UnaryServerInterceptor用法

有的时候,当进行grpc调用的时候,并不希望客户端与服务端建立连接后直接就进入对应的方法体内。比如需要验证签名来确认客户端的身份,再执行相应的方法。这个时候就可以哟拿到Interceptor。

golang grpc的拦截器(Interceptor)为UnaryServerInterceptor,为一个指向函数的指针。

UnaryServerInterceptor在服务端对于一次RPC调用进行拦截。UnaryServerInterceptor是一个函数指针,当客户端进行grpc调用的时候,首先并不执行用户调用的方法,先执行UnaryServerInterceptor所指的函数,随后再进入真正要执行的函数。

grpc的UnaryServerInterceptor定义如下:

// UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info
// contains all the information of this RPC the interceptor can operate on. And handler is the wrapper
// of the service method implementation. It is the responsibility of the interceptor to invoke handler
// to complete the RPC.
type UnaryServerInterceptor func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error)
req就是用户的请求参数,info中包含了此次调用的信息,handler就是客户端此次实际要调用的函数。

UnaryServerInfo的定义如下:

// UnaryServerInfo consists of various information about a unary RPC on
// server side. All per-rpc information may be mutated by the interceptor.
type UnaryServerInfo struct {
// Server is the service implementation the user provides. This is read-only.
Server interface{}
// FullMethod is the full RPC method string, i.e., /package.service/method.
FullMethod string
}
主要包含两个部分,Server成员,是客户编写的服务器端的服务实现,这个成员是只读的,FullMethod成员是要调用的方法名,这个方法名interceptor可以进行修改。所以说,如果需要进行服务端方法调用之前需要验签的话,interceptor可以这么写:

var interceptor grpc.UnaryServerInterceptor
interceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
//对req中的签名进行验证
//如果失败直接返回Nil,err=验证签名失败

//handler是客户端原来打算调用的方法,如果验证成功,执行真正的方法
return handler(ctx, req)
}
相应的服务端初始化程序就是类似这种写法:

lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
panic(err)
return
}

var opts []grpc.ServerOption//grpc为使用的第三方的grpc包
opts = append(opts, grpc.UnaryInterceptor(interceptor))
server := grpc.NewServer(opts...)

chatprt.RegisterChatServer()//填入相关参数
server.Serve(lis)

原文地址:https://www.cnblogs.com/ExMan/p/12269786.html

时间: 2024-07-30 10:09:58

golang grpc UnaryServerInterceptor用法的相关文章

golang的select用法

早期的select函数是用来监控一系列的文件句柄,一旦其中一个文件句柄发生IO操作,该select调用就会被返回.golang在语言级别直接支持select,用于处理异步IO问题. select用法同switch类似,如下: timeout := make (chan bool, 1)ch := make(chan int) select { case <-ch: case <-timeout: fmt.Println("timeout!") default: fmt.Pr

Golang gRPC 示例

1.安装gRPC runtime go get google.golang.org/grpc 为了自动生成Golang的gRPC代码,需要安装protocal buffers compiler以及对应的GoLang插件 2.protocal buffer安装 从https://github.com/google/protobuf/releases下载安装包,例如:protobuf-cpp-3.0.0-beta-3.zip,解压后 ./configure make && make insta

golang grpc demo

grpm安装: git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc proto,protoc-gen-go安装: go get -u github.com/golang/protobuf/{proto,protoc-gen-go} 原文地址:https://www.cnblogs.com/wangjq19920210/p/11572283.html

golang grpc 双向

使用grpc双向模式,可以实现客户端随时发送消息给服务端,服务端也可以随时发送消息到客户端,不再是一问一答的模式. grpc_stream/hellowoldstream/helloworldstream.proto syntax = "proto3"; package pb; message HelloRequest { string username = 1; } message HelloResponse { string message = 1; } // + message

Golang gRPC中间件:拦截器链接,验证,日志记录,重试等

gRPC Go Middleware: interceptors, helpers, utilities. Middleware gRPC Go recently acquired support for Interceptors, i.e. middleware that is executed either on the gRPC Server before the request is passed onto the user's application logic, or on the

golang之range用法

range表达式迭代语法 range在go中主要是用来做迭代用的,它可以迭代:array,slice,string,map,channel.但是在迭代的过程中有些注意事项是需要牢记的,否则在实际使用过程中可能出现和你预期不符合的问题,而且很难排插到. 语法 0. var  x Type :x = range expresstion 1. x := range expression 2. x,y := range expression range 右侧的expresstion,它可以是数组,指向数

golang插件

本文记载下了golang插件的用法, 原文: https://code.tutsplus.com/tutorials/writing-plugins-in-go--cms-29101 这个例子使用了两个排序: 快速排序和冒泡排序,不追求算法的高效,只为了说明插件的生成和使用. 而且只在linux下,build和运行.(windows环境没有测试过, 原文在windows下要使用docker) 目录: plugins├── bubble_sort│   └── bubble_sort_plugin

Dapr Pub/Sub 集成 RabbitMQ 、Golang、Java、DotNet Core

前置条件: <Dapr运用> <Dapr 运用之 Java gRPC 调用篇> <Dapr 运用之集成 Asp.Net Core Grpc 调用篇> 搭建 RabbitMQ Docker 搭建 RabbitMQ 服务 docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management 创建 rabbiqmq.yaml apiVers

golang_elasticsearch 多精确值匹配

问题 比如要查找属于两种类型的物品,这个时候,term查询就不行了,需要采用terms查询. golang中的用法 看了一下,olivere/elastic 包提供了一个 terms查询,于是高兴的直接使用了. query := elastic.NewBoolQuery() query = query.Filter(elastic.NewTermsQuery("status", []int{1,2})) 没想到没有效果,于是又看源码,才发现 func NewTermsQuery(nam