Я запускаю простой сервер GRPC на python на моем локальном компьютере. Когда я пытаюсь подключиться к нему, используя java, с моего устройства Android, я продолжаю получать ошибку Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE
. Обратите внимание, что я попытался подключиться к серверу через клиент Python, и он работал, как ожидалось. Проблема возникает только при использовании java-клиента.
Я попытался использовать клиент в python, чтобы проверить, есть ли проблема в файлах proto, и он работал правильно, поэтому я считаю, что проблема связана с соединением между сервером python и java < / em> клиентская комбинация.
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 файл:
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
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 mister NAME". фактический результат: Caused by:io.grpc.StatusRuntimeException: UNAVAILABLE