基于Netty与RabbitMQ的消息服务

简介:

Netty作为一个高性能的异步网络开发框架,可以作为各种服务的开发框架。

前段时间的一个项目涉及到硬件设备实时数据的采集,采用Netty作为采集服务的实现框架,同时使用RabbitMQ作为采集服务和各个其他模块的通信消息队列,整个服务框架图如下:

将业务代码和实际协议解析部分的代码抽离,得到以上一个简单的设计图,代码开源在GitHub上,简单介绍下NettyMQServer采集服务涉及到的几个关键技术点:

1、设备TCP消息解析:

NettyMQServer和采集设备Device之间采用TCP通信,TCP消息的解析可以使用LengthFieldBasedFrameDecoder(消息头和消息体),可以有效的解决TCP消息“粘包”问题。

消息包解析图如下:bytes length field at offset 0, do not strip header, the length field represents the length of the whole message

 lengthFieldOffset   =  0
 lengthFieldLength   =  2
 lengthAdjustment    = -2 (= the length of the Length field)
 initialBytesToStrip =  0

 BEFORE DECODE (14 bytes)         AFTER DECODE (14 bytes)
 +--------+----------------+      +--------+----------------+
 | Length | Actual Content |----->| Length | Actual Content |
 | 0x000E | "HELLO, WORLD" |      | 0x000E | "HELLO, WORLD" |
 +--------+----------------+      +--------+----------------+

代码中消息长度的存储采用了4个字节,采用LengthFieldBasedFrameDecoder(65535,0,4,-4,0)解码,Netty会从接收的数据中头4个字节中得到消息的长度,进而得到一个TCP消息包。

2、给设备发消息:

首先在连接创建时,要保留TCP的连接:

复制代码
static final ChannelGroup channels = new DefaultChannelGroup(
            GlobalEventExecutor.INSTANCE);

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // A closed channel will be removed from ChannelGroup automatically
        channels.add(ctx.channel());
    }
复制代码

在每次一个Channel Active(连接创建)的时候用ChannelGroup保存这个Channel连接,当需要给某个设备发消息的时候,可以遍历该ChannelGroup,找到对应的Channel,给该Channel发送消息:

for (io.netty.channel.Channel c : EchoServerHandler.channels) {
    ByteBuf msg = Unpooled.copiedBuffer(message.getBytes());
    c.writeAndFlush(msg);
}

这里是给所有的连接的设备都发。当连接断开的时候,ChannelGroup会自动remove掉这个连接,不需要我们手动管理。

3、心跳检测

当某个设备Device由于断电或是其他原因导致设备不正常无法采集数据,Netty服务端需要知道该设备是否在正常工作,可以使用Netty的IdleStateHandler,示例代码如下:

复制代码
// 3 minutes for read idle
ch.pipeline().addLast(new IdleStateHandler(3*60,0,0));
ch.pipeline().addLast(new HeartBeatHandler());

/**
 * Handler implementation for heart beating.
 */
public class HeartBeatHandler extends ChannelInboundHandlerAdapter{

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
            throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.READER_IDLE) {
                // Read timeout
                System.out.println("READER_IDLE: read timeout from "+ctx.channel().remoteAddress());
                //ctx.disconnect(); //Channel disconnect
            }
        }
    }
}
复制代码

上面设置3分钟没有读到数据,则触发一个READER_IDLE事件。

4、RabbitMQ消息接收与发送

NettyMQServer消息发送采用了Spring AMQP,只需要在配置文件中简单配置一下,就可以方便使用。

NettyMQServer消息接收同样可以采用Spring AMQP,但由于对Spring相关的配置不是很熟悉,为了更灵活的使用MQ,这里使用了RabbitMQ Client Java API来实现:

复制代码
                    Connection connection = connnectionFactory.newConnection();
                    Channel channel = connection.createChannel();
                    channel.exchangeDeclare(exchangeName, "direct", true, false, null);
                    channel.queueDeclare(queueName, true, false, false, null);
                    channel.queueBind(queueName, exchangeName, routeKey);

                    // process the message one by one
                    channel.basicQos(1);

                    QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
                    // auto-ack is false
                    channel.basicConsume(queueName, false, queueingConsumer);
                    while (true) {
                        QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
                        String message = new String(delivery.getBody());

                        log.debug("Mq Receiver get message");
                        // Send the message to all connected clients
                        // If you want to send to a specified client, just add
                        // your own logic and ack manually
                        // Be aware that ChannelGroup is thread safe
                        log.info(String.format("Conneted client number: %d",EchoServerHandler.channels.size()));
                        for (io.netty.channel.Channel c : EchoServerHandler.channels) {
                            ByteBuf msg = Unpooled.copiedBuffer(message.getBytes());
                            c.writeAndFlush(msg);
                        }
                        // manually ack to MQ server the message is consumed.
                        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                    }
复制代码

以上代码从一个Queue中读取数据,为了有效处理数据,防止异常数据丢失,使用了手动Ack。

RabbitMQ的使用方式:http://www.cnblogs.com/luxiaoxun/p/3918054.html

 

代码托管在GitHub上:https://github.com/luxiaoxun/Code4Java

 

 

参考:

http://netty.io/

http://netty.io/4.0/api/io/netty/handler/codec/LengthFieldBasedFrameDecoder.html

http://netty.io/4.0/api/io/netty/handler/timeout/IdleStateHandler.html



    本文转自阿凡卢博客园博客,原文链接:http://www.cnblogs.com/luxiaoxun/p/4257105.html,如需转载请自行联系原作者


相关文章
|
2月前
|
传感器 网络协议 物联网
在Linux中搭建Mosquitto MQTT协议消息服务端并结合内网穿透工具实现公网访问
Mosquitto是一个开源的消息代理,它实现了MQTT协议版本3.1和3.1.1。它可以在不同的平台上运行,包括Windows、Linux、macOS等。mosquitto可以用于物联网、传感器、移动应用程序等场景,提供了一种轻量级的、可靠的、基于发布/订阅模式的消息传递机制。
|
6月前
|
消息中间件 Java 数据安全/隐私保护
搭建RabbitMQ消息服务,整合SpringBoot实现收发消息(三)
搭建RabbitMQ消息服务,整合SpringBoot实现收发消息
|
6月前
|
消息中间件 Java 测试技术
搭建RabbitMQ消息服务,整合SpringBoot实现收发消息(四)
搭建RabbitMQ消息服务,整合SpringBoot实现收发消息
|
6月前
|
消息中间件 Java
搭建RabbitMQ消息服务,整合SpringBoot实现收发消息(二)
搭建RabbitMQ消息服务,整合SpringBoot实现收发消息
|
6月前
|
消息中间件 存储 Java
搭建RabbitMQ消息服务,整合SpringBoot实现收发消息(一)
搭建RabbitMQ消息服务,整合SpringBoot实现收发消息
EMQ
|
消息中间件 云安全 安全
EMQX Cloud BYOC 版本发布:在您的云上体验全托管的 MQTT 消息服务
EMQX Cloud BYOC为更高数据隐私控制和定制化云服务需求的企业提供理想解决方案。在自己的云环境中构建安全、可扩展的MQTT云部署,满足物联网业务需求。
EMQ
117 0
EMQX Cloud BYOC 版本发布:在您的云上体验全托管的 MQTT 消息服务
|
消息中间件 SpringCloudAlibaba 负载均衡
SA实战 ·《SpringCloud Alibaba实战》第18章-消息服务:MQ使用场景与选型对比
SA实战 ·《SpringCloud Alibaba实战》第18章-消息服务:MQ使用场景与选型对比
217 0
SA实战 ·《SpringCloud Alibaba实战》第18章-消息服务:MQ使用场景与选型对比
|
消息中间件 JSON 物联网
使用消息服务(MNS)订阅阿里云物联网平台设备消息PHP示例参考
物联网平台服务端订阅支持将设备消息发送至消息服务(MNS),云端应用通过监听MNS队列,获取设备消息。本文主要演示使用最新版MNS PHP SDK消费订阅到MNS Queue中的消息。
使用消息服务(MNS)订阅阿里云物联网平台设备消息PHP示例参考
|
1月前
|
消息中间件 存储 监控
RabbitMQ:分布式系统中的高效消息队列
RabbitMQ:分布式系统中的高效消息队列
|
1月前
|
消息中间件 Java
springboot整合消息队列——RabbitMQ
springboot整合消息队列——RabbitMQ
75 0

热门文章

最新文章