Netty-protobuf-rpcとprotobuf-socket-rpcは互いに通じる
2421 ワード
重要なポイント:
1,protobuf-socket-rpcは短い接続を使用しており、送信が完了するとsocketを上りオフにする必要があります
2,rpc.protoファイルを統一する
3, protobuf-socket-rpcはスライス送信をサポートせず、データの大きいパケットに対して3 K以上、netty-probuf-rpcが提供するrpcを使用して実現することを提案する
4、ネットワークがない場合、nettyがブロックされます.変更方法は以下の通りです.
1,protobuf-socket-rpcは短い接続を使用しており、送信が完了するとsocketを上りオフにする必要があります
2,rpc.protoファイルを統一する
class NettyRpcPipelineFactory implements ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = Channels.pipeline();
// p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MAX_FRAME_BYTES_LENGTH, 0, 4, 0, 4));
p.addLast("protobufDecoder", new ProtobufDecoder(defaultInstance));
// p.addLast("frameEncoder", new LengthFieldPrepender(4));
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("handler", handlerFactory.getChannelUpstreamHandler());
return p;
}
}
public class NettyRpcClientChannelUpstreamHandler extends SimpleChannelUpstreamHandler {
@Override
public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) {
try {
NioSocketChannel ch = (NioSocketChannel) e.getChannel();
DefaultSocketChannelConfig cfg = (DefaultSocketChannelConfig) ch
.getConfig();
Field f = DefaultSocketChannelConfig.class
.getDeclaredField("socket");
f.setAccessible(true);
Socket socket = (Socket) f.get(cfg);
socket.shutdownOutput();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
3, protobuf-socket-rpcはスライス送信をサポートせず、データの大きいパケットに対して3 K以上、netty-probuf-rpcが提供するrpcを使用して実現することを提案する
4、ネットワークがない場合、nettyがブロックされます.変更方法は以下の通りです.
public Message callBlockingMethod(MethodDescriptor method,RpcController controller, Message request, Message responsePrototype)
throws ServiceException {
BlockingRpcCallback callback = new BlockingRpcCallback();
ResponsePrototypeRpcCallback rpcCallback = new ResponsePrototypeRpcCallback(controller, responsePrototype, callback);
int nextSeqId = handler.getNextSeqId();
Message Request = buildRequest(method, request);
handler.registerCallback(nextSeqId, rpcCallback);
ChannelFuture future = this.channel.write(Request);
try {
future.sync();
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
synchronized(callback) {
while(!callback.isDone()) {
try {
callback.wait();
} catch (InterruptedException e) {
logger.warn("Interrupted while blocking", e);
/* Ignore */
}
}
}
...
}