|
|
|
@ -42,7 +42,7 @@ type UnaryServerInterceptor func(ctx context.Context, req interface{}, info *Una |
|
|
|
|
* 一个`UnaryHandler`方法用于传递`Handler`,就是基于`proto`文件`service`内声明而生成的方法 |
|
|
|
|
* 一个`UnaryServerInterceptor`用于拦截`Handler`方法,可在`Handler`执行前后插入拦截代码 |
|
|
|
|
|
|
|
|
|
为了更形象的说明拦截器的执行过程,请看基于`proto`生成的以下代码[代码位置](https://github.com/bilibili/kratos-demo/blob/master/api/api.pb.go): |
|
|
|
|
为了更形象的说明拦截器的执行过程,请看基于`proto`生成的以下代码[代码位置](https://github.com/go-kratos/kratos-demo/blob/master/api/api.pb.go): |
|
|
|
|
|
|
|
|
|
```go |
|
|
|
|
func _Demo_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { |
|
|
|
@ -89,7 +89,7 @@ func UnaryInterceptor(i UnaryServerInterceptor) ServerOption { |
|
|
|
|
|
|
|
|
|
> Only one unary interceptor can be installed. The construction of multiple interceptors (e.g., chaining) can be implemented at the caller. |
|
|
|
|
|
|
|
|
|
`gRPC`本身只支持一个`interceptor`,想要多`interceptors`需要自己实现~~所以`warden`基于`grpc.UnaryClientInterceptor`实现了`interceptor chain`,请看下面代码[代码位置](https://github.com/bilibili/kratos/blob/master/pkg/net/rpc/warden/server.go): |
|
|
|
|
`gRPC`本身只支持一个`interceptor`,想要多`interceptors`需要自己实现~~所以`warden`基于`grpc.UnaryClientInterceptor`实现了`interceptor chain`,请看下面代码[代码位置](https://github.com/go-kratos/kratos/blob/master/pkg/net/rpc/warden/server.go): |
|
|
|
|
|
|
|
|
|
```go |
|
|
|
|
// Use attachs a global inteceptor to the server. |
|
|
|
@ -167,7 +167,7 @@ type UnaryClientInterceptor func(ctx context.Context, method string, req, reply |
|
|
|
|
* 一个`UnaryInvoker`表示客户端具体要发出的执行方法 |
|
|
|
|
* 一个`UnaryClientInterceptor`用于拦截`Invoker`方法,可在`Invoker`执行前后插入拦截代码 |
|
|
|
|
|
|
|
|
|
具体执行过程,请看基于`proto`生成的下面代码[代码位置](https://github.com/bilibili/kratos-demo/blob/master/api/api.pb.go): |
|
|
|
|
具体执行过程,请看基于`proto`生成的下面代码[代码位置](https://github.com/go-kratos/kratos-demo/blob/master/api/api.pb.go): |
|
|
|
|
|
|
|
|
|
```go |
|
|
|
|
func (c *demoClient) SayHello(ctx context.Context, in *HelloReq, opts ...grpc.CallOption) (*google_protobuf1.Empty, error) { |
|
|
|
@ -211,7 +211,7 @@ func WithUnaryInterceptor(f UnaryClientInterceptor) DialOption { |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
需要注意的是客户端的拦截器在官方`gRPC`内也只能支持注册一个,与服务端拦截器`interceptor chain`逻辑类似`warden`在客户端拦截器也做了相同处理,并且在客户端连接时进行注册,请看下面代码[代码位置](https://github.com/bilibili/kratos/blob/master/pkg/net/rpc/warden/client.go): |
|
|
|
|
需要注意的是客户端的拦截器在官方`gRPC`内也只能支持注册一个,与服务端拦截器`interceptor chain`逻辑类似`warden`在客户端拦截器也做了相同处理,并且在客户端连接时进行注册,请看下面代码[代码位置](https://github.com/go-kratos/kratos/blob/master/pkg/net/rpc/warden/client.go): |
|
|
|
|
|
|
|
|
|
```go |
|
|
|
|
// Use attachs a global inteceptor to the Client. |
|
|
|
@ -332,9 +332,9 @@ package grpc |
|
|
|
|
import ( |
|
|
|
|
pb "kratos-demo/api" |
|
|
|
|
"kratos-demo/internal/service" |
|
|
|
|
"github.com/bilibili/kratos/pkg/conf/paladin" |
|
|
|
|
"github.com/bilibili/kratos/pkg/net/rpc/warden" |
|
|
|
|
"github.com/bilibili/kratos/pkg/net/rpc/warden/ratelimiter" |
|
|
|
|
"github.com/go-kratos/kratos/pkg/conf/paladin" |
|
|
|
|
"github.com/go-kratos/kratos/pkg/net/rpc/warden" |
|
|
|
|
"github.com/go-kratos/kratos/pkg/net/rpc/warden/ratelimiter" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// New new a grpc server. |
|
|
|
|