【问题标题】:Java gRPC get the service name from ServerInterceptorJava gRPC 从 ServerInterceptor 获取服务名称
【发布时间】:2018-07-27 13:03:48
【问题描述】:

我正在使用 gRPC,我需要从 ServerInterceptor 获取请求的服务名称,但似乎不可能。

基本上来自 ServerInterceptor 的实现,我需要知道将被调用的 ServiceGrpc 的名称(作为字符串)。

public class PermissionInterceptor implements ServerInterceptor {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
                ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> handler
        ) {
            // here I need the name of RPC service that has been requested

            return handler.startCall(serverCall, metadata);
        }
}

请注意,我已尝试使用 serverCall.getMethodDescriptor(),但它返回的是 proto 服务的名称,而不是 real 名称(java) 服务。

返回:

co.test.domain.transaction.TransactionService

但我需要这个:

co.test.domain.transaction.TransactionNeoBasicImpl

谢谢

【问题讨论】:

    标签: java grpc proto


    【解决方案1】:

    这是不可能的。

    如果您的拦截器是应用程序之前的最后一个拦截器,您可以检查handler 的类并查看其父类的名称。但这非常有限,不一定支持。

    【讨论】:

    • 感谢您的回答。因此,无法从 ServerInterceptor 读取 TransactionNeoBasicImpl 类中存在的注释。我说的对吗?
    • 正确。没有手动管道,你不能从拦截器中获取。
    【解决方案2】:

    有办法做到这一点。

    String fullMethodName = serverCall.getMethodDescriptor().getFullMethodName();
    String serviceName = MethodDescriptor.extractFullServiceName(fullMethodName);
    

    基本上 fullMethodName 将完全限定的服务名称作为前缀(后跟“/”)。像这样fullyqualified.ServiceName/MethodName

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-31
      • 2021-11-30
      • 2014-08-10
      • 2019-11-24
      • 2013-12-17
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      相关资源
      最近更新 更多