简体   繁体   English

Java gRPC从ServerInterceptor获取服务名称

[英]Java gRPC get the service name from ServerInterceptor

I am using gRPC and I need to get the name of the service of the request from the ServerInterceptor but it seems it is not possible. 我正在使用gRPC,我需要从ServerInterceptor获取请求服务的名称,但似乎不可能。

Basically from an implementation of ServerInterceptor I need to know the name of the ServiceGrpc (as a string) that will be invoked. 基本上,从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);
        }
}

Please note that I have tried with serverCall.getMethodDescriptor() but it returns the name of the proto service rather than the real name of the (java) service. 请注意,我已尝试使用serverCall.getMethodDescriptor()但它返回原型服务的名称而不是(java)服务的真实名称。

It returns: 它返回:

co.test.domain.transaction.TransactionService

But I need this: 但我需要这个:

co.test.domain.transaction.TransactionNeoBasicImpl

Thanks 谢谢

There is a way to do this. 有一种方法可以做到这一点。

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

Basically fullMethodName has the fully qualified service name as prefix (followed by a ' / '). 基本上,fullMethodName具有完全限定的服务名称作为前缀(后跟“ / ”)。 Something like this fullyqualified.ServiceName/MethodName 像这样的fullyqualified.ServiceName/MethodName

That is not possible. 这是不可能的。

If your interceptor is the last one before the application, you may be able to inspect handler 's class and see its parent class' name. 如果您的拦截器是应用程序之前的最后一个,您可以检查handler的类并查看其父类的名称。 But that's very limited and not necessarily supported. 但这非常有限,不一定得到支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM