The Server of Network Communication in RocketMq

  • 1. Broker Server Entry

Firstly, RocketMq network communication adopts Netty communication. The server is mainly in Broker. Let's first look at Broker Startup class

Obviously, the concrete logic is in the start method, and the following is to achieve:

 public void start() throws Exception {
        if (this.messageStore != null) {
            this.messageStore.start();
        }

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

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

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

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

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

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

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

        if (!messageStoreConfig.isEnableDLegerCommitLog()) {
            startProcessorByHa(messageStoreConfig.getBrokerRole());
            handleSlaveSynchronize(messageStoreConfig.getBrokerRole());
        }



        this.registerBrokerAll(true, false, true);

        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                try {
                    BrokerController.this.registerBrokerAll(true, false, brokerConfig.isForceRegister());
                } catch (Throwable e) {
                    log.error("registerBrokerAll Exception", e);
                }
            }
        }, 1000 * 10, Math.max(10000, Math.min(brokerConfig.getRegisterNameServerPeriod(), 60000)), TimeUnit.MILLISECONDS);

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

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


    }

You can roughly guess from the name that the remote message received is remotingServer.start(), click in to observe its implementation:

Here we see the familiar face ServerBootStrap, so it is clear that the specific communication protocol implementation we need to know must be written in a handler:

.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()
                            );
                    }

From these handlers, you can guess by name that the parsing of communication messages takes place in Netty Server handler and enters Netty Server handler:

As can be seen from the figure above, it is a message-reading Handler itself, and you can see that the body of the received message is Remoting Command. This class is necessarily the entire RocketMq communication protocol.

Go in and have a look:

Generally speaking, it consists of code, Header, body and some metedata. In fact, all Rpc invocation frameworks are basically this design idea. All requests must be inherited from a parent class.

However, the current micro-service system does not seem to do so, probably because of the diversity of different service requirements, but there is no uniform request is really weird, follow-up time to look at the design considerations in this regard.

At this point, the Netty call chain of Rpc basically ends.

Keywords: PHP Netty network

Added by faswad on Sun, 14 Jul 2019 21:17:16 +0300