小生醉舞魔 2023-09-26 10:02 采纳率: 0%
浏览 2

ClientInterceptor拦截器

实现一个GRPC的ClientInterceptor,当FullMethodName equal "path"的时候直接return response,而不是继续发送请求给server

  • 写回答

1条回答 默认 最新

  • 故事不长丨 2023-09-26 13:23
    关注
    import io.grpc.*;
    import io.grpc.ClientInterceptors.CheckedForwardingClientCall.SimpleForwardingClientCall;
    import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
    import io.grpc.Metadata.Key;
    import io.grpc.MethodDescriptor.MethodType;
    
    public class CustomInterceptor implements ClientInterceptor {
        private static final Key<String> FULL_METHOD_NAME = Key.of("full_method_name", Metadata.ASCII_STRING_MARSHALLER);
    
        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
                CallOptions callOptions, Channel next) {
            String fullMethodName = method.getFullMethodName();
            
            if (fullMethodName.equals("path")) {
                return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
                    @Override
                    public void start(Listener<RespT> delegate, Metadata headers) {
                        // 创建一个固定的 response
                        // 这里的 MyResponse 是你定义的自定义消息类型
                        delegate.onMessage((RespT) MyResponse.newBuilder().build());
                        delegate.onClose(Status.OK, new Metadata());
                    }
                };
            } else {
                return next.newCall(method, callOptions);
            }
        }
    }
    

    然后,在你的 gRPC 客户端代码中,将自定义的 Interceptor 添加到 Channel 上,如下所示:

    import io.grpc.*;
    
    public class MyGrpcClient {
        public static void main(String[] args) {
            ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 50051)
                    .usePlaintext()
                    .build();
    
            // 创建 Interceptor
            CustomInterceptor interceptor = new CustomInterceptor();
    
            // 创建 Channel,并将 Interceptor 添加到 Channel
            Channel interceptedChannel = ClientInterceptors.intercept(channel, interceptor);
    
            // 创建你的 gRPC 客户端
            YourGrpcClient client = YourGrpcClient.newBlockingStub(interceptedChannel);
    
            try {
                // 调用服务中的方法,会根据 FullMethodName 进行拦截判断
                client.myMethod(request);
                
                // ...
            } catch (Exception e) {
                // 异常处理
            }
        }
    }
    
    评论

报告相同问题?

问题事件

  • 修改了问题 9月26日
  • 创建了问题 9月26日

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端