由于Protobuf消息序列化格式不是跨平台和跨软件版本确定的,因此仅仅使用toByteArray()方法序列化消息并不一定会产生原始字节。有没有其他方法可以用gRPC Java服务器访问它们?
发布于 2018-08-22 18:12:11
看起来正确的方法是使用服务器拦截器:
ServerBuilder.forPort(port)
.addService(
ServerInterceptors.intercept(
ServerInterceptors.useInputStreamMessages(
new MyMegaCoolService().bindService()
),
new ServerInterceptor() {
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT,RespT> call,
Metadata headers,
ServerCallHandler<ReqT,RespT> next
) {
ServerCall.Listener<ReqT> listener = next.startCall(call, headers);
return new ForwardingServerCallListener
.SimpleForwardingServerCallListener<ReqT>(listener) {
@Override public void onMessage(ReqT msg) {
InputStream in = (InputStream)msg;
assert in.markSupported();
in.mark(MAX_MESSAGE_SIZE);
readMessage(in);
try {
in.reset();
}
catch (IOException e) {
throw new IllegalStateException(e);
}
super.onMessage(msg);
}
};
}
}
)
)
.build();https://stackoverflow.com/questions/51905383
复制相似问题