Netty是业界最流行的nio框架之一,结合springboot可以满足快速开发
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级"通讯协议,该协议构建于TCP/IP协议上的。MQTT协议的可以用在物联网、小型设备、还有移动应用上。
Netty也可以实现MQTT协议,他的内部封装了MQTT协议的相关对象。
Springboot+Netty搭建MQTT协议的服务端基础Demo代码案例
使用Netty+SpringBoot方式可以快速地开发一套基于MQTT协议(主要是MQTT3.1和MQTT3.1.1)的服务端程序
SpringBoot+Netty创建,pom.xml文件导入依赖包
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath />
</parent>
<groupId>boot.base.mqtt.server</groupId>
<artifactId>boot-example-base-mqtt-server-2.0.5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-example-base-mqtt-server-2.0.5</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包成一个可执行jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Springboot启动类,直接在main里面启动netty的MQTT服务(也包含web应用的)
package boot.example.mqtt.server;
import boot.example.mqtt.server.netty.BootNettyMqttServerThread;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import boot.example.mqtt.server.netty.BootNettyMqttServer;
/**
* 蚂蚁舞
*/
@SpringBootApplication
public class BootNettyMqttApplication implements CommandLineRunner {
public static void main( String[] args ) {
SpringApplication app = new SpringApplication(BootNettyMqttApplication.class);
app.run(args);
}
@Override
public void run(String... args) throws Exception {
// 启动 1883
int port = 1883;
BootNettyMqttServerThread bootNettyMqttServerThread = new BootNettyMqttServerThread(port);
bootNettyMqttServerThread.start();
}
}
Netty的MQTT启动类
package boot.example.mqtt.server.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.timeout.IdleStateHandler;
/**
* 蚂蚁舞
*/
public class BootNettyMqttServer {
private NioEventLoopGroup bossGroup;
private NioEventLoopGroup workGroup;
/**
* 启动服务
*/
public void startup(int port) {
try {
bossGroup = new NioEventLoopGroup(1);
workGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.option(ChannelOption.SO_REUSEADDR, true)
.option(ChannelOption.SO_BACKLOG, 1024)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.option(ChannelOption.SO_RCVBUF, 10485760);
bootstrap.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) {
ChannelPipeline channelPipeline = ch.pipeline();
// 设置读写空闲超时时间
channelPipeline.addLast(new IdleStateHandler(600, 600, 1200));
channelPipeline.addLast("encoder", MqttEncoder.INSTANCE);
channelPipeline.addLast("decoder", new MqttDecoder());
channelPipeline.addLast(new BootNettyMqttChannelInboundHan

本文介绍了如何使用SpringBoot和Netty框架构建一个基于MQTT协议的服务端。通过添加相关依赖,创建启动类,定义Netty的MQTT处理类以及消息回调方法,实现了MQTT连接、订阅、发布等基本功能。此外,还提到了使用Eclipse的Paho工具进行客户端测试,并展示了不同MQTT报文类型的示例。
最低0.47元/天 解锁文章
9934

被折叠的 条评论
为什么被折叠?



