文章目录
先从channel中获取到发送请求的客户端的地址:remoteAddr,然后根据这个地址去执行rpcHooks,还记得我们之前记过的,RPCHooks缓存的是执行请求前或获取响应后要执行的内容。
而在定义回调方法callback内,先执行响应后的动作,之后把响应回传给客户端,对于单向的rpc调用(无返回值),则不做操作。
继续往下,根据处理器是否异步同步做了不同的处理,但都调用了上面定义的callback方法
再往下就是从processorTable中获取执行器提交上面定义的runnable任务了。
回到NettyServerHandler,其实
processMessageReceived(ctx, msg)
这个方法就是执行器提交了收到的请求,并执行了前置和后置的钩子方法,并把响应通过channel返回回去。再看下
processResponseCommand(ChannelHandlerContext ctx, RemotingCommand cmd)
处理响应的方法
public void processResponseCommand(ChannelHandlerContext ctx, RemotingCommand cmd) {
final int opaque = cmd.getOpaque();
final ResponseFuture responseFuture = responseTable.get(opaque);
if (responseFuture != null) {
responseFuture.setResponseCommand(cmd);
responseTable.remove(opaque);
if (responseFuture.getInvokeCallback() != null) {
executeInvokeCallback(responseFuture);
} else {
responseFuture.putResponse(cmd);
responseFuture.release();
}
} else {
log.warn("receive response, but not matched any request, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
log.warn(cmd.toString());
}
}
可以看到方法内部从responseTable中取出responseFuture,把ResponseCommand塞入responseFuture,执行responseFuture的回调方法,之后把responseTable中的对应Future删除。
接下来第三个handler:NettyConnectManageHandler、再回顾下前两个handler,第一个是握手handler,也就是建立连接;第二个是NettyServerHandler,也就是服务端接受响应或请求的handler。










