1

我在本地机器上的 python 上运行一个简单的 GRPC 服务器。当我尝试使用 java 从我的 android 设备连接到它时,我不断收到Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE错误消息。请注意,我尝试通过 python 客户端连接到服务器,它按预期工作。该问题仅在使用 java 客户端时存在。

我曾尝试在python中使用客户端来检查proto文件是否有问题,并且它工作正常,所以我认为问题出在python服务器和java客户端组合之间的连接上。

    private ManagedChannel mChannel;
    private TestGrpc.TestBlockingStub blockingStub;
    private TestGrpc.TestStub asyncStub;

    mChannel = ManagedChannelBuilder.forAddress("10.0.0.17", 50051).build();
    blockingStub = TestGrpc.newBlockingStub(mChannel);
    helloMessage testMessage = helloMessage.newBuilder()
    .setMessageContent("NAME")
    .build();
    helloMessage msg= blockingStub.sayHello(testMessage);

原型文件:

syntax="proto3";
option java_package = "io.grpc.testing";
option java_multiple_files = true;
option java_outer_classname = "TestClass";
option objc_class_prefix = "TST ";

package TestCode;

service Test{
    rpc sayHello(helloMessage) returns (helloMessage) {}
    rpc streamTest(helloMessage) returns (stream helloMessage) {}
}

message helloMessage{
    string messageContent = 1;
}

蟒蛇服务器

import protofile_pb2
import protofile_pb2_grpc


# create a class to define the server functions, derived from
# calculator_pb2_grpc.CalculatorServicer
class TestService(protofile_pb2_grpc.TestServicer):

    # calculator.square_root is exposed here
    # the request and response are of the data type
    # calculator_pb2.Number
    def sayHello(self, request, context):
        response = protofile_pb2.helloMessage()
        response.messageContent = "hello mister "+request.messageContent
        return response
# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

# use the generated function `add_CalculatorServicer_to_server`
# to add the defined class to the server
protofile_pb2_grpc.add_TestServicer_to_server(
        TestService(), server)

# listen on port 50051
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()

# since server.start() will not block,
# a sleep-loop is added to keep alive
try:
    while True:
        time.sleep(86400)
except KeyboardInterrupt:
    server.stop(0)

这应该返回一个具有一个值的迭代器,即字符串:“hello miser NAME”。实际结果: Caused by:io.grpc.StatusRuntimeException: UNAVAILABLE

4

1 回答 1

0

我发现这个问题的解决方案是我使用了一个不安全的端口进行通信,并使用 SSL 来连接来自 android 的预期握手。您可以通过创建服务器证书并使用安全通道通信或将更改更改为使用 .Plaintext() 构建来解决此问题

https://grpc.io/docs/guides/auth/

于 2020-05-22T12:52:41.660 回答