Springboot 集成Emqtt

一、引入jar包

pom.xml中增加一下以来

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</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>

二、增加配置

applicaion.yml配置如下

1
2
3
4
5
6
7
8
9
spring:
mqtt:
username: admin # 账号
password: public # 密码
host-url: tcp://ip:1883 # mqtt连接tcp地址
client-id: test # 客户端Id,每个启动的id要不同
default-topic: test # 默认主题
timeout: 100 # 超时时间
keepalive: 100 # 保持连接数

三、mqtt配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* @Classname MtqqEntity
* @Description mqtt相关配置信息
*/
@Component
@ConfigurationProperties("spring.mqtt")
@Data
public class MqttConfig {
@Autowired
private MqttPushClient mqttPushClient;

/**
* 用户名
*/
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() {
mqttPushClient.connect(hostUrl, clientId, username, password, timeout, keepalive);
// 以/#结尾表示订阅所有以test开头的主题
mqttPushClient.subscribe("test/#", 0);
return mqttPushClient;
}
}

四、mqtt推送客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* @Classname MqttPushClient
* @Description mqtt推送客户端
*/
@Component
@Slf4j
public class MqttPushClient {

@Autowired
private PushCallback pushCallback;

public static MqttClient client;

public 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 topic 主题
* @param qos 连接方式
*/
public void subscribe(String topic, int qos) {
log.info("开始订阅主题" + topic);
try {
MqttPushClient.getClient().subscribe(topic, qos);
} catch (MqttException e) {
e.printStackTrace();
}
}
}

五、消费监听类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* @Classname PushCallback
* @Description 消费监听类
*/
@Component
@Slf4j
public class PushCallback implements MqttCallback {

@Autowired
private MqttConfig mqttConfig;


@Override
public void connectionLost(Throwable throwable) {
// 连接丢失后,一般在这里面进行重连
log.info("连接断开,可以做重连");
if (MqttPushClient.client == null || !MqttPushClient.client.isConnected()) {
mqttConfig.getMqttPushClient();
}
}

@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
// subscribe后得到的消息会执行到这里面
log.info("接收消息主题 : " + topic);
log.info("接收消息Qos : " + mqttMessage.getQos());
log.info("接收消息内容 : " + new String(mqttMessage.getPayload()));
}

@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
log.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
}
}

六、测试文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import com.wang.emqtt.config.MqttPushClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @Description: 测试文件
*/
@RestController
@RequestMapping("/")
public class TestController {

@Autowired
private MqttPushClient mqttPushClient;

@GetMapping(value = "/publishTopic")
public String publishTopic() {
mqttPushClient.publish(0,false,"test/test","测试一下发布消息");
return "RUtils.ok()";
}
}

一辈子很短,努力的做好两件事就好;
第一件事是热爱生活,好好的去爱身边的人;
第二件事是努力学习,在工作中取得不一样的成绩,实现自己的价值,而不是仅仅为了赚钱;

继开 wechat
欢迎加我的微信,共同交流技术