简体   繁体   English

在 python 服务器和 android(java) 客户端之间使用 grpc 创建连接

[英]Creating a connection using grpc between a python server and an android(java) client

I am running a simple GRPC server on python on my local machine.我在本地机器上的 python 上运行一个简单的 GRPC 服务器。 When I'm trying to connect to it, using java, from my android device, i keep getting the Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE error.当我尝试使用 java 从我的 android 设备连接到它时,我不断收到Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE错误: Note that i tried to connect to the server, via a python client and it worked as expected.请注意,我尝试通过 python 客户端连接到服务器,并且它按预期工作。 The problem only stands when using a java client.该问题仅在使用 java 客户端时存在。

I have tried to use a client in python to check whether the proto files where the problem, and it worked correctly, so i believe the problem is with the connection between the python server and java client combination.我尝试使用 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);

proto file:原型文件:

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;
}

python server python 服务器

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)

This should return an iterator with one value, being the string: "hello mister NAME".这应该返回一个具有一个值的迭代器,即字符串:“hello miser NAME”。 actual result: Caused by:io.grpc.StatusRuntimeException: UNAVAILABLE实际结果: Caused by:io.grpc.StatusRuntimeException: UNAVAILABLE

I found the solution to this problem was that i was using an insecure port for the communication and using SSL for the connection from android which expected handshaking.我发现这个问题的解决方案是我使用了一个不安全的端口进行通信,并使用 SSL 进行来自 android 的连接,该连接预期握手。 You can solve this by creating server certificates and using secure channel communications or changing your change to build with.Plaintext()您可以通过创建服务器证书并使用安全通道通信或将更改更改为 build with.Plaintext() 来解决此问题

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

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

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