链式自定义GRPC客户端拦截器/DialOptions

问题描述 投票:0回答:0

我想链接一些

DialOptions
/客户端拦截器。但由于某种原因,只会调用我最新的自定义拦截器:

myOpt1 := grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    print("testInterceptor invoked")
    return invoker(ctx, method, req, reply, cc, opts...)
})
myOpt2 := grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    print("testInterceptor2 invoked")
    return invoker(ctx, method, req, reply, cc, opts...)
})
opts := []grpc.DialOption{
        grpc.WithTransportCredentials(insecure.NewCredentials()), // will get invoked
        myOpt1, // will be skipped
        myOpt2, // will get invoked
}

conn, err := grpc.DialContext(context.Background(), "my-adress:443", opts...) // err is nil
myService := pbAugment.NewMyServiceClient(conn)
myService.MyFunction(context.Background()) // prints: testInterceptor2 invoked

我添加了

TransportCredentials
,这样我在启动时就不会收到错误(关于缺少传输安全性)。

我在这里缺少什么?

go grpc middleware grpc-go
© www.soinside.com 2019 - 2024. All rights reserved.