版本
- 基于
rocketmq-all-4.3.1
版本
Remoting模块结构
remoting
是RocketMQ
的基础通信模块,阅读此模块的源码前提是对netty有一定的了解,以下是remoting
模块的部分UML图
Server端关键类分析
-
NettyServerConfig
:Server端配置类public class NettyServerConfig implements Cloneable { private int listenPort = 8888; private int serverWorkerThreads = 8; private int serverCallbackExecutorThreads = 0; private int serverSelectorThreads = 3; private int serverOnewaySemaphoreValue = 256; private int serverAsyncSemaphoreValue = 64; private int serverChannelMaxIdleTimeSeconds = 120; //发送缓冲区默认65535 private int serverSocketSndBufSize = NettySystemConfig.socketSndbufSize; //接收缓冲区默认65535 private int serverSocketRcvBufSize = NettySystemConfig.socketRcvbufSize; //是否使用ByteBuf内存池,netty默认使用PooledByteBufAllocator,服务端默认true private boolean serverPooledByteBufAllocatorEnable = true; /** * make make install * ../glibc-2.10.1/configure \ --prefix=/usr \ --with-headers=/usr/include \ * --host=x86_64-linux-gnu \ --build=x86_64-pc-linux-gnu \ --without-gd */ private boolean useEpollNativeSelector = false; ...省略getter和setter方法... }
-
RemotingServer
:此接口抽象出三种调用方式,同步调用、异步调用、单程调用(不需要返回),NettyRemotingServer
是RemotingServer
的实现类。public interface RemotingServer extends RemotingService { void registerProcessor(final int requestCode, final NettyRequestProcessor processor, final ExecutorService executor); void registerDefaultProcessor(final NettyRequestProcessor processor, final ExecutorService executor); int localListenPort(); Pair<NettyRequestProcessor, ExecutorService> getProcessorPair(final int requestCode); RemotingCommand invokeSync(final Channel channel, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException; void invokeAsync(final Channel channel, final RemotingCommand request, final long timeoutMillis, final InvokeCallback invokeCallback) throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException; void invokeOneway(final Channel channel, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException; }
-
NettyRemotingServer.NettyConnectManageHandler
将Netty中的事件(Close、Connect、Exception、IDLE状态)封装为NettyEvent对象,并将NettyEvent对象添加到阻塞队列中,并通过异步线程(NettyEventExecutor
)进行处理(异步线程在Server启动的时候会随之启动) -
NettyRemotingServer.NettyServerHandler
是仅仅处理业务数据,请求的业务数据会被封装为RemotingCommand
对象,根据RemotingCommand
对象中的code(RequestCode)
来找到对应的处理器进行处理(处理器在对应的线程池中异步执行业务处理)
Client关键类分析
-
NettyClientConfig
:client配置类public class NettyClientConfig { /** * Worker thread number */ private int clientWorkerThreads = 4; private int clientCallbackExecutorThreads = Runtime.getRuntime().availableProcessors(); //用于控制同时单程调用的请求个数,默认65535,防止调用太快 private int clientOnewaySemaphoreValue = NettySystemConfig.CLIENT_ONEWAY_SEMAPHORE_VALUE; //用于控制同时异步调用请求个数,默认65535 private int clientAsyncSemaphoreValue = NettySystemConfig.CLIENT_ASYNC_SEMAPHORE_VALUE; //客户端连接超时时间 private int connectTimeoutMillis = 3000; //在当前版本没有用到这个参数?难道是忘记了? private long channelNotActiveInterval = 1000 * 60; /** * IdleStateEvent will be triggered when neither read nor write was performed for * the specified period of this time. Specify {@code 0} to disable */ private int clientChannelMaxIdleTimeSeconds =