gpt4 book ai didi

Java gRPC 从 ServerInterceptor 获取服务名称

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:25 27 4
gpt4 key购买 nike

我正在使用 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 服务的名称,而不是 真实 名称(java) 服务。

它返回:

co.test.domain.transaction.TransactionService

但我需要这个:

co.test.domain.transaction.TransactionNeoBasicImpl

谢谢

最佳答案

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

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

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

关于Java gRPC 从 ServerInterceptor 获取服务名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829847/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com