NameServer has been covered a lot. This chapter does not expound too much. Learn directly around the source code.

Start the class – NamesrvStartup

Location: org. Apache. Rocketmq. Namesrv. NamesrvStartup

Key part code:

// Create a core processor NamesrvController, similar to the Web, to receive various pull network requests
NamesrvController controller = createNamesrvController(args);
/ / start NameServer
start(controller);
Copy the code

CreateNamesrvController Sets NameServer core parameters

Location: org. Apache. Rocketmq. Namesrv. NamesrvStartup# createNamesrvController

The beginning part is the detection of runtime parameters, part of the code as follows:

// ToDo: k2: detection of run-time parameter commands
Options options = ServerUtil.buildCommandlineOptions(new Options());
commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, buildCommandlineOptions(options), new PosixParser());
if (null == commandLine) {
    System.exit(-1);
    return null;
}
Copy the code

Next is the filling of the core parameters:

NamesrvConfig: contains parameters for NameServer to run;

NettyServerConfig: contains the parameters of the Netty server.

Part of the code is as follows:

// ToDo: k1: Three core configurations of NameServer
// NamesrvConfig: contains NameServer's own run parameters;
// NettyServerConfig: contains Netty server parameters.
final NamesrvConfig namesrvConfig = new NamesrvConfig();
final NettyServerConfig nettyServerConfig = new NettyServerConfig();
nettyServerConfig.setListenPort(9876);

// ToDo: k2-> Parse three configuration objects
if (commandLine.hasOption('c')) {... }if (commandLine.hasOption('p')) {... }Copy the code

Start starts

The start method is primarily the initialization of the Controller, with an interesting hook to shut down the MQ service.

Part of the code is as follows:

public static NamesrvController start(final NamesrvController controller) throws Exception {...// Controller initializes several timers
    booleaninitResult = controller.initialize(); .// Close the MQ service hook
    Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, new Callable<Void>() {
        @Override
        public Void call(a) throws Exception {
            controller.shutdown();
            return null; }})); controller.start();return controller;
}
Copy the code

Initialize the initialization

org.apache.rocketmq.namesrv.NamesrvController#initialize

Part of the code is as follows:

// NameController initialization
public boolean initialize(a) {
    // Load the KV configuration
    this.kvConfigManager.load();
    // Create a NettyServer network handling object
    this.remotingServer = new NettyRemotingServer(this.nettyServerConfig, this.brokerHousekeepingService);
    // Netty service worker thread pool
    this.remotingExecutor =
        Executors.newFixedThreadPool(nettyServerConfig.getServerWorkerThreads(), new ThreadFactoryImpl("RemotingExecutorThread_"));
    // Register Proccessor and inject remotingExecutor into remotingServer
    this.registerProcessor();

    // start scheduled task 1: NameServer scans brokers every 1Os to remove inactive brokers
    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

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

    // Start scheduled task 2: Names SI Rver prints the KV configuration every 10 minutes
    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run(a) {
            NamesrvController.this.kvConfigManager.printAllPeriodically(); }},1.10, TimeUnit.MINUTES); . }Copy the code

At this point, NameServer is ready to start. Its overall structure is as follows: