在RocketMQ中,使用NamesrvStartup作为启动类

主函数作为其启动的入口:

public static void main(String[] args) {
	main0(args);
}

main0方法:

public static NamesrvController main0(String[] args) {
    try {
        NamesrvController controller = createNamesrvController(args);
        start(controller);
        String tip = "The Name Server boot success. serializeType=" + RemotingCommand.getSerializeTypeConfigInThisServer();
        log.info(tip);
        System.out.printf("%s%n", tip);
        return controller;
    } catch (Throwable e) {
        e.printStackTrace();
        System.exit(-1);
    }

    return null;
}

首先通过createNamesrvController方法生成NameServer的控制器NamesrvController

createNamesrvController方法:

public static NamesrvController createNamesrvController(String[] args) throws IOException, JoranException {
    System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION));
    //PackageConflictDetect.detectFastjson();

    Options options = ServerUtil.buildCommandlineOptions(new Options());
    commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, buildCommandlineOptions(options), new PosixParser());
    if (null == commandLine) {
        System.exit(-1);
        return null;
    }

    final NamesrvConfig namesrvConfig = new NamesrvConfig();
    final NettyServerConfig nettyServerConfig = new NettyServerConfig();
    nettyServerConfig.setListenPort(9876);
    if (commandLine.hasOption('c')) {
        String file = commandLine.getOptionValue('c');
        if (file != null) {
            InputStream in = new BufferedInputStream(new FileInputStream(file));
            properties = new Properties();
            properties.load(in);
            MixAll.properties2Object(properties, namesrvConfig);
            MixAll.properties2Object(properties, nettyServerConfig);

            namesrvConfig.setConfigStorePath(file);

            System.out.printf("load config properties file OK, %s%n", file);
            in.close();
        }
    }

    if (commandLine.hasOption('p')) {
        InternalLogger console = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_CONSOLE_NAME);
        MixAll.printObjectProperties(console, namesrvConfig);
        MixAll.printObjectProperties(console, nettyServerConfig);
        System.exit(0);
    }

    MixAll.properties2Object(ServerUtil.commandLine2Properties(commandLine), namesrvConfig);

    if (null == namesrvConfig.getRocketmqHome()) {
        System.out.printf("Please set the %s variable in your environment to match the location of the RocketMQ installation%n", MixAll.ROCKETMQ_HOME_ENV);
        System.exit(-2);
    }

    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(namesrvConfig.getRocketmqHome() + "/conf/logback_namesrv.xml");

    log = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_LOGGER_NAME);

    MixAll.printObjectProperties(log, namesrvConfig);
    MixAll.printObjectProperties(log, nettyServerConfig);

    final NamesrvController controller = new NamesrvController(namesrvConfig, nettyServerConfig);

    // remember all configs to prevent discard
    controller.getConfiguration().registerConfig(properties);

    return controller;
}

这里创建了两个实体类NamesrvConfig和NettyServerConfig
这两个实体类对应了其配置文件中的配置

NamesrvConfig:

private String rocketmqHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));
private String kvConfigPath = System.getProperty("user.home") + File.separator + "namesrv" + File.separator + "kvConfig.json";
private String configStorePath = System.getProperty("user.home") + File.separator + "namesrv" + File.separator + "namesrv.properties";
private String productEnvName = "center";
private boolean clusterTest = false;
private boolean orderMessageEnable = false;

NettyServerConfig:

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;

private int serverSocketSndBufSize = NettySystemConfig.socketSndbufSize // 65535;
private int serverSocketRcvBufSize = NettySystemConfig.socketRcvbufSize // 65535;
private boolean serverPooledByteBufAllocatorEnable = true;

对应如下配置文件:

##
# 名称:NamesrvConfig.rocketmqHome <String>
# 默认值:(通过 sh mqnamesrv 设置 ROCKETMQ_HOME 环境变量,在源程序中获取环境变量得
#        到的目录)
# 描述:RocketMQ 主目录 
# 建议:不主动配置
##
rocketmqHome = /usr/rocketmq

##
# 名称:NamesrvConfig.kvConfigPath <String>
# 默认值:$user.home/namesrv/kvConfig.json <在源程序中获取用户环境变量后生成>
# 描述:kv 配置文件路径,包含顺序消息主题的配置信息 
# 建议:启用顺序消息时配置
##
kvConfigPath = /root/namesrv/kvConfig.json

##
# 名称:NamesrvConfig.configStorePath <String>
# 默认值:$user.home/namesrv/namesrv.properties <在源程序中获取用户环境变量后生成>
# 描述:NameServer 配置文件路径
# 建议:启动时通过 -c 指定
##
configStorePath = /root/namesrv/namesrv.properties

##
# 名称:NamesrvConfig.clusterTest <boolean>
# 默认值:false <在源程序中初始化字段时指定>
# 描述:是否开启集群测试
# 建议:不主动配置
##
clusterTest = false

##
# 名称:NamesrvConfig.orderMessageEnable <boolean>
# 默认值:false <在源程序中初始化字段时指定>
# 描述:是否支持顺序消息
# 建议:启用顺序消息时配置
##
orderMessageEnable = false

##
# 名称:NettyServerConfig.listenPort <int>
# 默认值:9876 <在源程序中初始化后单独设置>
# 描述:服务端监听端口
# 建议:不主动配置
##
listenPort = 9876

##
# 名称:NettyServerConfig.serverWorkerThreads <int>
# 默认值:8 <在源程序中初始化字段时指定>
# 描述:Netty 业务线程池线程个数
# 建议:不主动配置
##
serverWorkerThreads = 8

##
# 名称:NettyServerConfig.serverCallbackExecutorThreads <int>
# 默认值:0 <在源程序中初始化字段时指定>
# 描述:Netty public 任务线程池线程个数,Netty 网络设计,根据业务类型会创建不同的线程池,比如处理发送消息、消息消费、心跳检测等。如果该业务类型(RequestCode)未注册线程池,则由 public 线程池执行
# 建议:
##
serverCallbackExecutorThreads = 0

##
# 名称:NettyServerConfig.serverSelectorThreads <int>
# 默认值:3 <在源程序中初始化字段时指定>
# 描述:IO 线程池线程个数,主要是 NameServer、Broker 端解析请求、返回响应的线程个数,这类线程池主要是处理网络请求的,解析请求包,然后转发到各个业务线程池完成具体的业务操作,然后将结果再返回调用方
# 建议:不主动配置
##
serverSelectorThreads = 3

##
# 名称:NettyServerConfig.serverOnewaySemaphoreValue <int>
# 默认值:256 <在源程序中初始化字段时指定>
# 描述:send oneway 消息请求并发度
# 建议:不主动配置
##
serverOnewaySemaphoreValue = 256

##
# 名称:NettyServerConfig.serverAsyncSemaphoreValue <int>
# 默认值:64 <在源程序中初始化字段时指定>
# 描述:异步消息发送最大并发度
# 建议:不主动配置
##
serverAsyncSemaphoreValue = 64

##
# 名称:NettyServerConfig.serverChannelMaxIdleTimeSeconds <int>
# 默认值:120 <在源程序中初始化字段时指定>
# 描述:网络连接最大空闲时间,单位秒,如果连接空闲时间超过该参数设置的值,连接将被关闭
# 建议:不主动配置
##
serverChannelMaxIdleTimeSeconds = 120

##
# 名称:NettyServerConfig.serverSocketSndBufSize <int>
# 默认值:65535 <在源程序中初始化字段时指定>
# 描述:网络 socket 发送缓存区大小,单位 B,即默认为 64KB
# 建议:不主动配置
##
serverSocketSndBufSize = 65535

##
# 名称:NettyServerConfig.serverSocketRcvBufSize <int>
# 默认值:65535 <在源程序中初始化字段时指定>
# 描述:网络 socket 接收缓存区大小,单位 B,即默认为 64KB
# 建议:不主动配置
##
serverSocketRcvBufSize = 65535

##
# 名称:NettyServerConfig.serverPooledByteBufAllocatorEnable <int>
# 默认值:true <在源程序中初始化字段时指定>
# 描述:ByteBuffer 是否开启缓存,建议开启
# 建议:不主动配置
##
serverPooledByteBufAllocatorEnable = true

##
# 名称:NettyServerConfig.useEpollNativeSelector <int>
# 默认值:false <在源程序中初始化字段时指定>
# 描述:是否启用 Epoll IO 模型
# 建议:Linux 环境开启
##
useEpollNativeSelector = true

接下来是对‘-c’命令下配置文件的加载,以及‘-p’命令下namesrvConfig和nettyServerConfig属性的打印
后续是对日志的一系列配置

在完成这些后,会根据namesrvConfig和nettyServerConfig创建NamesrvController实例

NamesrvController:

public NamesrvController(NamesrvConfig namesrvConfig, NettyServerConfig nettyServerConfig) {
    this.namesrvConfig = namesrvConfig;
    this.nettyServerConfig = nettyServerConfig;
    this.kvConfigManager = new KVConfigManager(this);
    this.routeInfoManager = new RouteInfoManager();
    this.brokerHousekeepingService = new BrokerHousekeepingService(this);
    this.configuration = new Configuration(
        log,
        this.namesrvConfig, this.nettyServerConfig
    );
    this.configuration.setStorePathFromConfig(this.namesrvConfig, "configStorePath");
}

可以看到这里创建了一个KVConfigManager和一个RouteInfoManager

KVConfigManager:

public class KVConfigManager {
	private final NamesrvController namesrvController;
	private final HashMap<String/* Namespace */, HashMap<String/* Key */, String/* Value */>> configTable =
	        new HashMap<String, HashMap<String, String>>();
	        
	public KVConfigManager(NamesrvController namesrvController) {
	        this.namesrvController = namesrvController;
	}
	......
}

KVConfigManager通过建立configTable管理KV

RouteInfoManager:

public class RouteInfoManager {
	private final HashMap<String/* topic */, List<QueueData>> topicQueueTable;
	private final HashMap<String/* brokerName */, BrokerData> brokerAddrTable;
	private final HashMap<String/* clusterName */, Set<String/* brokerName */>> clusterAddrTable;
	private final HashMap<String/* brokerAddr */, BrokerLiveInfo> brokerLiveTable;
	private final HashMap<String/* brokerAddr */, List<String>/* Filter Server */> filterServerTable;
	private final static long BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2;

	public RouteInfoManager() {
	this.topicQueueTable = new HashMap<String, List<QueueData>>(1024);
	this.brokerAddrTable = new HashMap<String, BrokerData>(128);
	this.clusterAddrTable = new HashMap<String, Set<String>>(32);
	this.brokerLiveTable = new HashMap<String, BrokerLiveInfo>(256);
	this.filterServerTable = new HashMap<String, List<String>>(256);
	}
	......
}

RouteInfoManager则记录了这些路由信息,其中BROKER_CHANNEL_EXPIRED_TIME 表示允许的不活跃的Broker存活时间

在NamesrvController中还创建了一个BrokerHousekeepingService:

public class BrokerHousekeepingService implements ChannelEventListener {
    private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_LOGGER_NAME);
    private final NamesrvController namesrvController;

    public BrokerHousekeepingService(NamesrvController namesrvController) {
        this.namesrvController = namesrvController;
    }

    @Override
    public void onChannelConnect(String remoteAddr, Channel channel) {
    }

    @Override
    public void onChannelClose(String remoteAddr, Channel channel) {
        this.namesrvController.getRouteInfoManager().onChannelDestroy(remoteAddr, channel);
    }

    @Override
    public void onChannelException(String remoteAddr, Channel channel) {
        this.namesrvController.getRouteInfoManager().onChannelDestroy(remoteAddr, channel);
    }

    @Override
    public void onChannelIdle(String remoteAddr, Channel channel) {
        this.namesrvController.getRouteInfoManager().onChannelDestroy(remoteAddr, channel);
    }
}

可以看到这是一个ChannelEventListener,用来处理Netty的中的异步事件监听

在创建完NamesrvController后,回到main0,调用start方法,真正开启NameServer服务

start方法:

public static NamesrvController start(final NamesrvController controller) throws Exception {
    if (null == controller) {
        throw new IllegalArgumentException("NamesrvController is null");
    }

    boolean initResult = controller.initialize();
    if (!initResult) {
        controller.shutdown();
        System.exit(-3);
    }

    Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            controller.shutdown();
            return null;
        }
    }));

    controller.start();

    return controller;
}

首先调用NamesrvController的initialize方法:

public boolean initialize() {
    this.kvConfigManager.load();

    this.remotingServer = new NettyRemotingServer(this.nettyServerConfig, this.brokerHousekeepingService);

    this.remotingExecutor =
        Executors.newFixedThreadPool(nettyServerConfig.getServerWorkerThreads(), new ThreadFactoryImpl("RemotingExecutorThread_"));

    this.registerProcessor();

    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            NamesrvController.this.routeInfoManager.scanNotActiveBroker();
        }
    }, 5, 10, TimeUnit.SECONDS);

    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            NamesrvController.this.kvConfigManager.printAllPeriodically();
        }
    }, 1, 10, TimeUnit.MINUTES);

    if (TlsSystemConfig.tlsMode != TlsMode.DISABLED) {
        // Register a listener to reload SslContext
        try {
            fileWatchService = new FileWatchService(
                new String[] {
                    TlsSystemConfig.tlsServerCertPath,
                    TlsSystemConfig.tlsServerKeyPath,
                    TlsSystemConfig.tlsServerTrustCertPath
                },
                new FileWatchService.Listener() {
                    boolean certChanged, keyChanged = false;
                    @Override
                    public void onChanged(String path) {
                        if (path.equals(TlsSystemConfig.tlsServerTrustCertPath)) {
                            log.info("The trust certificate changed, reload the ssl context");
                            reloadServerSslContext();
                        }
                        if (path.equals(TlsSystemConfig.tlsServerCertPath)) {
                            certChanged = true;
                        }
                        if (path.equals(TlsSystemConfig.tlsServerKeyPath)) {
                            keyChanged = true;
                        }
                        if (certChanged && keyChanged) {
                            log.info("The certificate and private key changed, reload the ssl context");
                            certChanged = keyChanged = false;
                            reloadServerSslContext();
                        }
                    }
                    private void reloadServerSslContext() {
                        ((NettyRemotingServer) remotingServer).loadSslContext();
                    }
                });
        } catch (Exception e) {
            log.warn("FileWatchService created error, can't load the certificate dynamically");
        }
    }

    return true;
}

先通过kvConfigManager的load方法,向KVConfigManager中的map加载之前配置好的KV文件路径下的键值对

public void load() {
    String content = null;
    try {
        content = MixAll.file2String(this.namesrvController.getNamesrvConfig().getKvConfigPath());
    } catch (IOException e) {
        log.warn("Load KV config table exception", e);
    }
    if (content != null) {
        KVConfigSerializeWrapper kvConfigSerializeWrapper =
            KVConfigSerializeWrapper.fromJson(content, KVConfigSerializeWrapper.class);
        if (null != kvConfigSerializeWrapper) {
            this.configTable.putAll(kvConfigSerializeWrapper.getConfigTable());
            log.info("load KV config table OK");
        }
    }
}

方法比较简单,将JSON形式的KV文件包装成KVConfigSerializeWrapper,通过getConfigTable方法转换成map放在configTable中

完成KV加载后,建立了一个NettyRemotingServer,即Netty服务器

public NettyRemotingServer(final NettyServerConfig nettyServerConfig,
        final ChannelEventListener channelEventListener) {
    super(nettyServerConfig.getServerOnewaySemaphoreValue(), nettyServerConfig.getServerAsyncSemaphoreValue());
    this.serverBootstrap = new ServerBootstrap();
    this.nettyServerConfig = nettyServerConfig;
    this.channelEventListener = channelEventListener;

    int publicThreadNums = nettyServerConfig.getServerCallbackExecutorThreads();
    if (publicThreadNums <= 0) {
        publicThreadNums = 4;
    }

    this.publicExecutor = Executors.newFixedThreadPool(publicThreadNums, new ThreadFactory() {
        private AtomicInteger threadIndex = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "NettyServerPublicExecutor_" + this.threadIndex.incrementAndGet());
        }
    });

    if (useEpoll()) {
        this.eventLoopGroupBoss = new EpollEventLoopGroup(1, new ThreadFactory() {
            private AtomicInteger threadIndex = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("NettyEPOLLBoss_%d", this.threadIndex.incrementAndGet()));
            }
        });

        this.eventLoopGroupSelector = new EpollEventLoopGroup(nettyServerConfig.getServerSelectorThreads(), new ThreadFactory() {
            private AtomicInteger threadIndex = new AtomicInteger(0);
            private int threadTotal = nettyServerConfig.getServerSelectorThreads();

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("NettyServerEPOLLSelector_%d_%d", threadTotal, this.threadIndex.incrementAndGet()));
            }
        });
    } else {
        this.eventLoopGroupBoss = new NioEventLoopGroup(1, new ThreadFactory() {
            private AtomicInteger threadIndex = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("NettyNIOBoss_%d", this.threadIndex.incrementAndGet()));
            }
        });

        this.eventLoopGroupSelector = new NioEventLoopGroup(nettyServerConfig.getServerSelectorThreads(), new ThreadFactory() {
            private AtomicInteger threadIndex = new AtomicInteger(0);
            private int threadTotal = nettyServerConfig.getServerSelectorThreads();

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("NettyServerNIOSelector_%d_%d", threadTotal, this.threadIndex.incrementAndGet()));
            }
        });
    }

    loadSslContext();
}

这里创建了ServerBootstrap
channelEventListener就是刚才创建的BrokerHousekeepingService

然后根据是否使用epoll,选择创建两个合适的EventLoopGroup

创建完成后,通过loadSslContext完成对SSL和TLS的设置

回到initialize方法,在创建完Netty的服务端后,调用registerProcessor方法:

private void registerProcessor() {
    if (namesrvConfig.isClusterTest()) {

        this.remotingServer.registerDefaultProcessor(new ClusterTestRequestProcessor(this, namesrvConfig.getProductEnvName()),
            this.remotingExecutor);
    } else {

        this.remotingServer.registerDefaultProcessor(new DefaultRequestProcessor(this), this.remotingExecutor);
    }
}

这里和是否设置了clusterTest集群测试有关,默认关闭

在默认情况下创建了DefaultRequestProcessor,这个类很重要,后面会详细说明,然后通过remotingServer的registerDefaultProcessor方法,将DefaultRequestProcessor注册给Netty服务器:

public void registerDefaultProcessor(NettyRequestProcessor processor, ExecutorService executor) {
    this.defaultRequestProcessor = new Pair<NettyRequestProcessor, ExecutorService>(processor, executor);
}

在做完这些后,提交了两个定时任务
①定时清除不活跃的Broker
RouteInfoManager的scanNotActiveBroker方法:

public void scanNotActiveBroker() {
    Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, BrokerLiveInfo> next = it.next();
        long last = next.getValue().getLastUpdateTimestamp();
        if ((last + BROKER_CHANNEL_EXPIRED_TIME) < System.currentTimeMillis()) {
            RemotingUtil.closeChannel(next.getValue().getChannel());
            it.remove();
            log.warn("The broker channel expired, {} {}ms", next.getKey(), BROKER_CHANNEL_EXPIRED_TIME);
            this.onChannelDestroy(next.getKey(), next.getValue().getChannel());
        }
    }
}

这里比较简单,在之前RouteInfoManager中创建的brokerLiveTable表中遍历所有BrokerLiveInfo,找到超出规定时间BROKER_CHANNEL_EXPIRED_TIME的BrokerLiveInfo信息进行删除,同时关闭Channel
而onChannelDestroy方法,会对其他几张表进行相关联的删除工作,代码重复量大就不细说了

BrokerLiveInfo记录了Broker的活跃度信息:

private long lastUpdateTimestamp;
private DataVersion dataVersion;
private Channel channel;
private String haServerAddr;

lastUpdateTimestamp记录上一次更新时间戳,是其活跃性的关键

②定时完成configTable的日志记录
KVConfigManager的printAllPeriodically方法:

public void printAllPeriodically() {
    try {
        this.lock.readLock().lockInterruptibly();
        try {
            log.info("--------------------------------------------------------");

            {
                log.info("configTable SIZE: {}", this.configTable.size());
                Iterator<Entry<String, HashMap<String, String>>> it =
                    this.configTable.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, HashMap<String, String>> next = it.next();
                    Iterator<Entry<String, String>> itSub = next.getValue().entrySet().iterator();
                    while (itSub.hasNext()) {
                        Entry<String, String> nextSub = itSub.next();
                        log.info("configTable NS: {} Key: {} Value: {}", next.getKey(), nextSub.getKey(),
                            nextSub.getValue());
                    }
                }
            }
        } finally {
            this.lock.readLock().unlock();
        }
    } catch (InterruptedException e) {
        log.error("printAllPeriodically InterruptedException", e);
    }
}

很简单,根据configTable表的内容,完成KV的日志记录

在创建完这两个定时任务后会注册一个侦听器,以便完成SslContext的重新加载

initialize随之结束,之后是对关闭事件的处理

最后调用NamesrvController的start,此时才是真正的开启物理上的服务
NamesrvController的start方法:

public void start() throws Exception {
    this.remotingServer.start();

    if (this.fileWatchService != null) {
        this.fileWatchService.start();
    }
}

这里实际上就是开启的Netty服务端

NettyRemotingServer的start方法:

public void start() {
    this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(
        nettyServerConfig.getServerWorkerThreads(),
        new ThreadFactory() {

            private AtomicInteger threadIndex = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "NettyServerCodecThread_" + this.threadIndex.incrementAndGet());
            }
        });

    ServerBootstrap childHandler =
        this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupSelector)
            .channel(useEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, 1024)
            .option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_KEEPALIVE, false)
            .childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize())
            .childOption(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize())
            .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort()))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline()
                        .addLast(defaultEventExecutorGroup, HANDSHAKE_HANDLER_NAME,
                            new HandshakeHandler(TlsSystemConfig.tlsMode))
                        .addLast(defaultEventExecutorGroup,
                            new NettyEncoder(),
                            new NettyDecoder(),
                            new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()),
                            new NettyConnectManageHandler(),
                            new NettyServerHandler()
                        );
                }
            });

    if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) {
        childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    }

    try {
        ChannelFuture sync = this.serverBootstrap.bind().sync();
        InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress();
        this.port = addr.getPort();
    } catch (InterruptedException e1) {
        throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1);
    }

    if (this.channelEventListener != null) {
        this.nettyEventExecutor.start();
    }

    this.timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                NettyRemotingServer.this.scanResponseTable();
            } catch (Throwable e) {
                log.error("scanResponseTable exception", e);
            }
        }
    }, 1000 * 3, 1000);
}

可以看到也就是正常的Netty服务端启动流程

在childHandler的绑定中,可以看到向pipeline绑定了一个NettyServerHandler:

class NettyServerHandler extends SimpleChannelInboundHandler<RemotingCommand> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RemotingCommand msg) throws Exception {
        processMessageReceived(ctx, msg);
    }
}

那么当客户端和NameServre端建立连接后,之间传输的消息会通过processMessageReceived方法进行处理

processMessageReceived方法:

public void processMessageReceived(ChannelHandlerContext ctx, RemotingCommand msg) throws Exception {
       final RemotingCommand cmd = msg;
   if (cmd != null) {
        switch (cmd.getType()) {
            case REQUEST_COMMAND:
                processRequestCommand(ctx, cmd);
                break;
            case RESPONSE_COMMAND:
                processResponseCommand(ctx, cmd);
                break;
            default:
                break;
        }
    }
}

根据消息类型(请求消息、响应消息),使用不同的处理

processRequestCommand方法:

public void processRequestCommand(final ChannelHandlerContext ctx, final RemotingCommand cmd) {
    final Pair<NettyRequestProcessor, ExecutorService> matched = this.processorTable.get(cmd.getCode());
    final Pair<NettyRequestProcessor, ExecutorService> pair = null == matched ? this.defaultRequestProcessor : matched;
    final int opaque = cmd.getOpaque();

    if (pair != null) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    doBeforeRpcHooks(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd);
                    final RemotingCommand response = pair.getObject1().processRequest(ctx, cmd);
                    doAfterRpcHooks(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd, response);

                    if (!cmd.isOnewayRPC()) {
                        if (response != null) {
                            response.setOpaque(opaque);
                            response.markResponseType();
                            try {
                                ctx.writeAndFlush(response);
                            } catch (Throwable e) {
                                log.error("process request over, but response failed", e);
                                log.error(cmd.toString());
                                log.error(response.toString());
                            }
                        } else {

                        }
                    }
                } catch (Throwable e) {
                    log.error("process request exception", e);
                    log.error(cmd.toString());

                    if (!cmd.isOnewayRPC()) {
                        final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_ERROR,
                            RemotingHelper.exceptionSimpleDesc(e));
                        response.setOpaque(opaque);
                        ctx.writeAndFlush(response);
                    }
                }
            }
        };

        if (pair.getObject1().rejectRequest()) {
            final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_BUSY,
                "[REJECTREQUEST]system busy, start flow control for a while");
            response.setOpaque(opaque);
            ctx.writeAndFlush(response);
            return;
        }

        try {
            final RequestTask requestTask = new RequestTask(run, ctx.channel(), cmd);
            pair.getObject2().submit(requestTask);
        } catch (RejectedExecutionException e) {
            if ((System.currentTimeMillis() % 10000) == 0) {
                log.warn(RemotingHelper.parseChannelRemoteAddr(ctx.channel())
                    + ", too many requests and system thread pool busy, RejectedExecutionException "
                    + pair.getObject2().toString()
                    + " request code: " + cmd.getCode());
            }

            if (!cmd.isOnewayRPC()) {
                final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_BUSY,
                    "[OVERLOAD]system busy, start flow control for a while");
                response.setOpaque(opaque);
                ctx.writeAndFlush(response);
            }
        }
    } else {
        String error = " request type " + cmd.getCode() + " not supported";
        final RemotingCommand response =
            RemotingCommand.createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error);
        response.setOpaque(opaque);
        ctx.writeAndFlush(response);
        log.error(RemotingHelper.parseChannelRemoteAddr(ctx.channel()) + error);
    }
}

在这里创建了一个Runnable提交给线程池,这个Runnable的核心是

final RemotingCommand response = pair.getObject1().processRequest(ctx, cmd);

实际上调用的就是前面说过的DefaultRequestProcessor的processRequest方法:

public RemotingCommand processRequest(ChannelHandlerContext ctx,
   RemotingCommand request) throws RemotingCommandException {

    if (ctx != null) {
        log.debug("receive request, {} {} {}",
            request.getCode(),
            RemotingHelper.parseChannelRemoteAddr(ctx.channel()),
            request);
    }


    switch (request.getCode()) {
        case RequestCode.PUT_KV_CONFIG:
            return this.putKVConfig(ctx, request);
        case RequestCode.GET_KV_CONFIG:
            return this.getKVConfig(ctx, request);
        case RequestCode.DELETE_KV_CONFIG:
            return this.deleteKVConfig(ctx, request);
        case RequestCode.QUERY_DATA_VERSION:
            return queryBrokerTopicConfig(ctx, request);
        case RequestCode.REGISTER_BROKER:
            Version brokerVersion = MQVersion.value2Version(request.getVersion());
            if (brokerVersion.ordinal() >= MQVersion.Version.V3_0_11.ordinal()) {
                return this.registerBrokerWithFilterServer(ctx, request);
            } else {
                return this.registerBroker(ctx, request);
            }
        case RequestCode.UNREGISTER_BROKER:
            return this.unregisterBroker(ctx, request);
        case RequestCode.GET_ROUTEINTO_BY_TOPIC:
            return this.getRouteInfoByTopic(ctx, request);
        case RequestCode.GET_BROKER_CLUSTER_INFO:
            return this.getBrokerClusterInfo(ctx, request);
        case RequestCode.WIPE_WRITE_PERM_OF_BROKER:
            return this.wipeWritePermOfBroker(ctx, request);
        case RequestCode.GET_ALL_TOPIC_LIST_FROM_NAMESERVER:
            return getAllTopicListFromNameserver(ctx, request);
        case RequestCode.DELETE_TOPIC_IN_NAMESRV:
            return deleteTopicInNamesrv(ctx, request);
        case RequestCode.GET_KVLIST_BY_NAMESPACE:
            return this.getKVListByNamespace(ctx, request);
        case RequestCode.GET_TOPICS_BY_CLUSTER:
            return this.getTopicsByCluster(ctx, request);
        case RequestCode.GET_SYSTEM_TOPIC_LIST_FROM_NS:
            return this.getSystemTopicListFromNs(ctx, request);
        case RequestCode.GET_UNIT_TOPIC_LIST:
            return this.getUnitTopicList(ctx, request);
        case RequestCode.GET_HAS_UNIT_SUB_TOPIC_LIST:
            return this.getHasUnitSubTopicList(ctx, request);
        case RequestCode.GET_HAS_UNIT_SUB_UNUNIT_TOPIC_LIST:
            return this.getHasUnitSubUnUnitTopicList(ctx, request);
        case RequestCode.UPDATE_NAMESRV_CONFIG:
            return this.updateConfig(ctx, request);
        case RequestCode.GET_NAMESRV_CONFIG:
            return this.getConfig(ctx, request);
        default:
            break;
    }
    return null;
}

这个方法很直观,根据不同的RequestCode,执行不同的方法,其中有熟悉的
REGISTER_BROKER 注册Broker
GET_ROUTEINTO_BY_TOPIC 获取Topic路由信息
而其相对性的方法执行就是通过查阅或者修改之前创建的表来完成
最后将相应的数据包装,在Runnable中通过Netty的writeAndFlush完成发送

至此NameServer的启动结束