gRPC客户端在从gRPC服务内部调用时不起作用。

huangapple go评论117阅读模式
英文:

gRPC client not working when called from within gRPC service

问题

免责声明,我对gRPC和RPC一般都很陌生,请耐心等待。

我有两个在同一个Java应用程序上运行的gRPC服务器,Service AService B
Service A创建了多个Service B客户端,然后同步地对各个Service B实例进行调用。

服务器

Service A具有由.proto文件定义的一个RPC调用,如下所示:

rpc notifyPeers(NotifyPeersRequest) returns (NotifyPeersResponse);

服务器端实现如下:

@Override
public void notifyPeers(NotifyPeersRequest request, StreamObserver<NotifyPeersResponse> responseObserver) {
    logger.debug("gRPC 'notifyPeers' 请求已接收");
    String host = request.getHost();
    
   
    for (PeerClient c : clients.values()) {   
        c.addPeer(host);    // <----  此调用
    }

    NotifyPeersResponse response = NotifyPeersResponse.newBuilder()
            .setResult(result)
            .build();

    responseObserver.onNext(response);
    responseObserver.onCompleted();
}

对等方列表 clients 在先前的RPC调用中构建。

ManagedChannel channel = ManagedChannelBuilder.forTarget(peer).usePlaintext().build();
ClientB client = new ClientB(channel);
clients.put(peer, client);

客户端

rpc addPeer(AddPeerRequest) returns (AddPeerResponse);rpc addPeer(AddPeerRequest) returns (AddPeerResponse);

服务器端实现如下:

@Override
public void addPeer(AddPeerRequest addPeerRequest, StreamObserver<AddPeerResponse> responseObserver)  {
    logger.info("gRPC 'addPeer' 请求已接收");
    boolean result = peer.addPeer(host);
    AddPeerResponse response = AddPeerResponse.newBuilder()
                .setResponse(result)
                .build();
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}

客户端端实现如下:

public boolean addPeer(String host) {
    AddPeerRequest request = AddPeerRequest.newBuilder().setHost(host).build();
    logger.info("发送 'addPeer' 请求");
    AddPeerResponse response = blockingStub.addPeer(request);
    return response.getResponse();
}

当我运行此应用程序并且对Service A进行RPC调用并创建客户端连接以调用addPeer时,会抛出模棱两可的异常io.grpc.StatusRuntimeException: UNKNOWN,然后导致JVM关闭。我不知道如何修复这个问题,或者是否在gRPC服务器内部创建gRPC客户端连接是否可行。

对于我所有的gRPC服务器实现,我都使用阻塞的 stubs

<grpc.version>1.16.1</grpc.version>
<java.version>1.8</java.version>

我几乎陷入了困境,所以任何信息都将不胜感激。

英文:

Quick disclaimer, I am very new to gRPC and RPC in general, so please have patience

I have two gRPC servers running on the same java application, Service A and Service B.
Service A creates multiple clients of Service B which then synchronously makes calls to the various instances of Service B

The server

Service A has a rpc call defined by the .proto file as

rpc notifyPeers(NotifyPeersRequest) returns (NotifyPeersResponse);

the server side implementation,

@Override
public void notifyPeers(NotifyPeersRequest request, StreamObserver&lt;NotifyPeersResponse&gt; responseObserver) {
    logger.debug(&quot;gRPC &#39;notifyPeers&#39; request received&quot;);
    String host = request.getHost();
    
   
    for (PeerClient c : clients.values()) {   
        c.addPeer(host);    // &lt;----  this call
    }

    NotifyPeersResponse response = NotifyPeersResponse.newBuilder()
            .setResult(result)
            .build();

    responseObserver.onNext(response);
    responseObserver.onCompleted();
}

The list of peers, clients are built up in previous rpc calls.

ManagedChannel channel = ManagedChannelBuilder.forTarget(peer).usePlaintext().build();
ClientB client = new ClientB(channel);
clients.put(peer, client);

The client

rpc addPeer(AddPeerRequest) returns (AddPeerResponse);rpc addPeer(AddPeerRequest) returns (AddPeerResponse);

the server side implementation,

@Override
public void addPeer(AddPeerRequest addPeerRequest, StreamObserver&lt;AddPeerResponse&gt; responseObserver)  {
    logger.info(&quot;gRPC &#39;addPeer&#39; request received&quot;);
    boolean result = peer.addPeer(host);
    AddPeerResponse response = AddPeerResponse.newBuilder()
                .setResponse(result)
                .build();
    responseObserver.onNext(response);
    responseObserver.onCompleted();

the client side implementation,

public boolean addPeer(String host) {
    AddPeerRequest request = AddPeerRequest.newBuilder().setHost(host).build();
    logger.info(&quot;Sending &#39;addPeer&#39; request&quot;);
    AddPeerResponse response = blockingStub.addPeer(request);
    return response.getResponse();
}

When I run this application, and an RPC is made to Service A and the client connection is created that calls addPeer, an ambiguous exception is thrown, io.grpc.StatusRuntimeException: UNKNOWN which then causes the JVM to shut down. I have no idea how to fix this, or whether it is even possible to create an gRPC client connection within a gRPC server

for all of my gRPC server implementations I'm using blocking stubs.

&lt;grpc.version&gt;1.16.1&lt;/grpc.version&gt;
&lt;java.version&gt;1.8&lt;/java.version&gt;

I've pretty much hit a brick wall, so any information will be appreciated

答案1

得分: 1

UNKNOWN消息是服务器端的异常,未传递给客户端。
您可能需要增加服务器上的日志级别以尝试查找根本原因。

在此帖子这里中,
像下面这样创建通道,可以使您看到更有意义的错误消息:

ManagedChannel channel = NettyChannelBuilder.forAddress( host, port )
				.protocolNegotiator(ProtocolNegotiators.serverPlaintext() )
英文:

The UNKNOWN message is an exception on the server side that was not passed to the client.
You probably need to increase the log level on the server to try to find the root cause.

In this post here ,
creating the channel like below, enable it to see a more meaningful error message:

ManagedChannel channel = NettyChannelBuilder.forAddress( host, port )
				.protocolNegotiator(ProtocolNegotiators.serverPlaintext() )

答案2

得分: 0

如果A和B在同一个应用程序中,您是否考虑过进行直接函数调用,或者至少使用InProcessChannelBuilderInProcessServerBuilder

如其他地方所提到的,在当前设置中,您可以尝试增加服务器端(B)的日志级别,以查看异常的源头。

英文:

If A and B are in the same application have you considered making direct function calls or at least using the InProcessChannelBuilder and InProcessServerBuilder?

As mentioned elsewhere, in the current setup you can try increasing the log level on the server side (in B) to see the source of the exception.

huangapple
  • 本文由 发表于 2020年7月31日 02:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63179318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定