RocketMQ源码分析(二)之Remoting模块(一)

本文分析RocketMQ的Remoting模块,重点讲解Server端的配置类、调用方式以及Netty事件处理,以及Client端的配置、Future实现和请求Id管理。Remoting模块基于Netty,其Server和Client处理业务数据的方式类似,通过NettyEvent对象在异步线程中处理。NettyRemotingAbstract作为共用抽象类,包含超时管理、事件执行器等功能。
摘要由CSDN通过智能技术生成

版本

  1. 基于rocketmq-all-4.3.1版本

Remoting模块结构

  1. remotingRocketMQ的基础通信模块,阅读此模块的源码前提是对netty有一定的了解,以下是remoting模块的部分UML图
    在这里插入图片描述

Server端关键类分析

  1. 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方法...
    
    }
    
  2. RemotingServer:此接口抽象出三种调用方式,同步调用、异步调用、单程调用(不需要返回),NettyRemotingServerRemotingServer的实现类。

    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;
    
    }
    
  3. NettyRemotingServer.NettyConnectManageHandler将Netty中的事件(Close、Connect、Exception、IDLE状态)封装为NettyEvent对象,并将NettyEvent对象添加到阻塞队列中,并通过异步线程(NettyEventExecutor)进行处理(异步线程在Server启动的时候会随之启动)

  4. NettyRemotingServer.NettyServerHandler是仅仅处理业务数据,请求的业务数据会被封装为RemotingCommand对象,根据RemotingCommand对象中的code(RequestCode)来找到对应的处理器进行处理(处理器在对应的线程池中异步执行业务处理)

Client关键类分析

  1. 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 = 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值