Netty在RocketMQ中的应用----服务端

RocketMQ中角色有Producer、Comsumer、Broker和NameServer,它们之间的通讯是通过Netty实现的。在之前的文章RocketMQ是如何通讯的?中,对RocketMQt通讯进行了一些介绍,但是底层Netty的细节涉及的比较少,这一篇将作为其中的一个补充。

服务端启动

 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,
                            new NettyEncoder(),
                            new NettyDecoder(),
                            new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()),
                            new NettyConnectManageHandler(),
                            new NettyServerHandler());
                    }
                });

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

服务端的启动和客户端不同的一点,在于支持了Epoll模式。Epoll是支持高效网络编程的的一把利器,通过它可以提高应用性能。关于Epoll可以参考下面这篇文章:linux下IO复用与Epoll详解
如果启用了Epoll(默认是启用的),那么selectotrGroup就会使用EpollEventLoopGroup。同时Channel也会使用EpollServerSocketChannel,而不是我们通常的NioServerSocketChannel。
如果启用了对象池,那么还会设置ALLOCATOR选项为PooledByteBufAllocator.DEFAULT。
Netty服务端在配置完成后就开始绑定端口并监听请求了。

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

接收消息

服务端处理消息也在NettyRemotingAbstract里面的processMessageReceived进行处理,客户端请求来的消息类型是REQUEST_COMMAND,所以我们关注processRequestCommand(ctx, cmd);方法即可。
首先,服务端根据请求来的REQUEST_CODE,找到之前注册好了的处理器(Propcessor)。
然后,服务端判断是否需要拒绝掉这次请求。拒绝的一般是在Producer发送消息时Broker的原因,例如系统页缓存繁忙。
如果没有拒绝这次请求,那么就会将这次请求包装成线程任务RequestTask供线程池调度执行。下面我们来看看这个包装的线程任务。

            Runnable run = new Runnable() {
                @Override
                public void run() {
                    try {
                        RPCHook rpcHook = NettyRemotingAbstract.this.getRPCHook();
                        if (rpcHook != null) {
                            rpcHook.doBeforeRequest(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd);
                        }

                        final RemotingCommand response = pair.getObject1().processRequest(ctx, cmd);
                        if (rpcHook != null) {
                            rpcHook.doAfterResponse(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd, response);
                        }

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

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

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

处理的流程非常清晰,先调用钩子函数,如果需要对消息做一些预处理,可以注册一个钩子函数。然后就是Processor处理请求,处理完成后,继续调用钩子函数。如果是单向请求,就结束了。需要返回则调用ctx.writeAndFlush(response);刷出站消息。

超时responseFuture处理

请求时,responseFuture是放在responseTable中的。在客户端请求时服务端(或者服务端请求客户端时),在异步调用中,如果超时了,那么responseTable中的responseFuture迟迟得不到处理,这显然是不行的。同步调用因为超时后会直接删掉该responseFuture,但在极端情况下也可能来不及删除被留在了responseTable中。因此我们需要新的定时任务,定期扫描该表。

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

定时任务的周期是1秒处理一次,需要取出超时的responseFuture删除。在异步调用中,如果有回调,还需要调用回调。回调同样是通过包装成线程,通过线程池调用。

public void scanResponseTable() {
        final List<ResponseFuture> rfList = new LinkedList<ResponseFuture>();
        Iterator<Entry<Integer, ResponseFuture>> it = this.responseTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Integer, ResponseFuture> next = it.next();
            ResponseFuture rep = next.getValue();

            if ((rep.getBeginTimestamp() + rep.getTimeoutMillis() + 1000) <= System.currentTimeMillis()) {
                rep.release();
                it.remove();
                rfList.add(rep);
                PLOG.warn("remove timeout request, " + rep);
            }
        }

        for (ResponseFuture rf : rfList) {
            try {
                executeInvokeCallback(rf);
            } catch (Throwable e) {
                PLOG.warn("scanResponseTable, operationComplete Exception", e);
            }
        }
    }
发布了379 篇原创文章 · 获赞 85 · 访问量 59万+

猜你喜欢

转载自blog.csdn.net/GAMEloft9/article/details/102932133
今日推荐