Java gRPC 从 ServerInterceptor 获取服务名称

Java gRPC get the service name from ServerInterceptor

我正在使用 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() 但它 returns proto[=30= 的名称] 服务,而不是 (java) 服务的 真实 名称。

它returns:

co.test.domain.transaction.TransactionService

但我需要这个:

co.test.domain.transaction.TransactionNeoBasicImpl

谢谢

那是不可能的。

如果您的拦截器是应用程序之前的最后一个拦截器,您可以检查 handler's class and see its parent class' 名称。但这非常有限,不一定支持。

有一种方法可以做到这一点。

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

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