我正在本地计算机上的python上运行一个简单的GRPC服务器。
当我尝试使用Java从Android设备连接到它时,我不断收到Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE
错误。
请注意,我尝试通过python客户端连接到服务器,并且按预期工作。
该问题仅在使用Java客户端时出现。
我尝试在python中使用客户端检查问题的原始文件是否存在,并且它工作正常,所以我认为问题出在 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;
}
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)
这应该返回一个值为一个值的迭代器,该值是字符串:
“您好,NAME”。
实际结果:
Caused by:io.grpc.StatusRuntimeException: UNAVAILABLE
答案 0 :(得分:0)
我发现此问题的解决方案是我使用了不安全的端口进行通讯,并使用SSL进行了android的连接(希望握手)。您可以通过创建服务器证书并使用安全通道通信或更改您的更改以使用.Plaintext()来解决此问题。