最近开发一款物联网相关设备,接触到了mqtt协议,网上找了很多资料,但基本都是“犹抱琵琶半遮面”,总有些内容缺失。可能是作者们认为那些太基础的内容不需要说,但对于我这种初学者来说,就很不善了。
所幸,历经坎坷,总算调通了一个,记录在这里,给像我一样的小白提供一些参考。
首先,基于mqqt协议的通信,你得有一个服务器,这个我都不知道。。。。。。(坑1)
mqtt服务的基本原理是,大家同服务器连接后,可以向服务器订阅n个“主题”,比如“hello”“test”等。然后有张三需要向服务器发送消息时,会告知服务器自己所发送的消息是什么主题的,比如“hello"。服务器收到张三的主题为“hello”的消息后,会查看都有谁向自己订阅了“hello”主题的消息,然后就把消息转发到那些订阅者那里去,完成通信。
我用的是emqx作为mqtt服务器软件,当然还有其他的开源的,这个好找,不多说了。
emqx我放在了阿里云上新购买的实例上。新买实例后,需要修改密码并重启后才能连接到实例上,这一点他们没有提示(坑2)。
然后,安装好,运行起来emqx,发现远程端口没打开,又学习了安全方面的设置,还有监听0.0.0.0地址的端口才是公网端口(坑3)。
把服务器软件装好后,用通讯猫或者paho测试了一下,能够远程正常连接并发送主题(一定要先测试好)。
最后是springboot整合mqtt做客户端部分。
先上原码:放心下载,不需要C币
1. pox.xml 引入需要的包,主要是最后面3个依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.bam</groupId>
<artifactId>mqtt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>MQTT-1</name>
<description>Demo project for Shiro</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 然后是application.yml
mqtt:
username: admin
password: public
host-url: tcp://123.123.123.123:1883 # 你自己服务器的地址和端口,这个需要改
clientID: test # 这个改不改随意,但不同的客户端肯定不能一样
default-topic: test # 默认主题
timeout: 100
keepalive: 100
3. 读取配置文件
package com.bam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import lombok.Getter;
import lombok.Setter;
/**
* @Classname MqttConfig
* @Description mqtt相关配置信息
* @Date 2020/3/5 11:00
* @Created by bam
*/
@Component
@ConfigurationProperties("mqtt")
@Setter
@Getter
public class MqttConfig {
@Autowired
private MqttPushClient mqttPushClient;
/**
* 用户名
*/
// @Value("username")
private String username;
/**
* 密码
*/
private String password;
/**
* 连接地址
*/
private String hostUrl;
/**
* 客户Id
*/
private String clientID;
/**
* 默认连接话题
*/
private String defaultTopic;
/**
* 超时时间
*/
private int timeout;
/**
* 保持连接数
*/
private int keepalive;
@Bean
public MqttPushClient getMqttPushClient() {
System.out.println("hostUrl: "+ hostUrl);
System.out.println("clientID: "+ clientID);
System.out.println("username: "+ username);
System.out.println("password: "+ password);
System.out.println("timeout: "+timeout);
System.out.println("keepalive: "+ keepalive);
mqttPushClient.connect(hostUrl, clientID, username, password, timeout, keepalive);
// 以/#结尾表示订阅所有以test开头的主题
mqttPushClient.subscribe(defaultTopic, 0);
return mqttPushClient;
}
}
4. MqttPushClient 发布消息类
package com.bam;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
*
* @author bam
* 2020年3月5日
* MqttPushClient.java
*
*/
@Component
public class MqttPushClient {
private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);
@Autowired
private PushCallback pushCallback;
private static MqttClient client;
private static MqttClient getClient() {
return client;
}
private static void setClient(MqttClient client) {
MqttPushClient.client = client;
}
/**
* 客户端连接
*
* @param host ip+端口
* @param clientID 客户端Id
* @param username 用户名
* @param password 密码
* @param timeout 超时时间
* @param keepalive 保留数
*/
public void connect(String host, String clientID, String username, String password, int timeout, int keepalive) {
MqttClient client;
try {
client = new MqttClient(host, clientID, new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName(username);
options.setPassword(password.toCharArray());
options.setConnectionTimeout(timeout);
options.setKeepAliveInterval(keepalive);
MqttPushClient.setClient(client);
try {
client.setCallback(pushCallback);
client.connect(options);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发布
*
* @param qos 连接方式
* @param retained 是否保留
* @param topic 主题
* @param pushMessage 消息体
*/
public void publish(int qos, boolean retained, String topic, String pushMessage) {
MqttMessage message = new MqttMessage();
message.setQos(qos);
message.setRetained(retained);
message.setPayload(pushMessage.getBytes());
MqttTopic mTopic = MqttPushClient.getClient().getTopic(topic);
if (null == mTopic) {
logger.error("topic not exist");
}
MqttDeliveryToken token;
try {
token = mTopic.publish(message);
token.waitForCompletion();
} catch (MqttPersistenceException e) {
e.printStackTrace();
} catch (MqttException e) {
e.printStackTrace();
}
}
/**
* 订阅某个主题
*
* @param topic 主题
* @param qos 连接方式
*/
public void subscribe(String topic, int qos) {
logger.info("开始订阅主题" + topic);
try {
MqttPushClient.getClient().subscribe(topic, qos);
} catch (MqttException e) {
e.printStackTrace();
}
}
}
5. 收到消息的处理
package com.bam;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Classname PushCallback
* @Description 消费监听类
* @Date 2019/4/11 23:31
* @Created by Jack
*/
@Component
public class PushCallback implements MqttCallback {
private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);
@Autowired
private MqttConfig mqttConfig;
private static MqttClient client;
@Override
public void connectionLost(Throwable throwable) {
// 连接丢失后,一般在这里面进行重连
logger.info("连接断开,可以做重连");
if (null != client) {
mqttConfig.getMqttPushClient();
}
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
// subscribe后得到的消息会执行到这里面
logger.info("接收消息主题 : " + topic);
logger.info("接收消息Qos : " + mqttMessage.getQos());
logger.info("接收消息内容 : " + new String(mqttMessage.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
logger.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
}
}
6. 发送方法测试
package com.bam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
*
* @author bam
* 2020年3月5日
* TestController.java
*
*/
@RestController
@RequestMapping("/")
public class TestController {
@Autowired
private MqttPushClient mqttPushClient;
@GetMapping(value = "/publishTopic")
public String publishTopic() {
String topicString = "test";
mqttPushClient.publish(0, false, topicString, "测试一下发布消息");
return "ok";
}
// 发送自定义消息内容(使用默认主题)
@RequestMapping("/publishTopic/{data}")
public void test1(@PathVariable("data") String data) {
String topicString = "test";
mqttPushClient.publish(0,false,topicString, data);
return "ok";
}
// 发送自定义消息内容,且指定主题
@RequestMapping("/publishTopic/{topic}/{data}")
public void test2(@PathVariable("topic") String topic, @PathVariable("data") String data) {
mqttPushClient.publish(0,false,topic, data);
return "ok";
}
}