SpringBoot + Vue 智慧停车之消息队列 MQTT 使用

Java研发军团

共 3359字,需浏览 7分钟

 ·

2021-07-27 09:10

简介

MQTT:它构建在TCP/IP之上,用于受限制的设备和不可靠的网络,应用于许多(开放源码)代理实现和许多客户机库。它包含用于不良网络/连接的IoT特定特性,并被广泛使用(主要用于物联网,但也通过MQTT通过WebSockets用于web和移动应用程序)。

MQTT 安装

这里使用 Docker 一键安装:

docker run -d --name emqx -p 1883:1883 -p 8081:8081 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:4.3.5

默认账号 admin 密码 public

整合

配置 pom.xml 引入:

<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>

配置 application.properties 引入:

spring.mqtt.username = admin
spring.mqtt.password = public
spring.mqtt.url = tcp://127.0.0.1:1883
spring.mqtt.client.id = HDC-DNC
spring.mqtt.default.topic = topic
spring.mqtt.default.completionTimeout = 3000

发送配置:

@Slf4j
@Configuration
@IntegrationComponentScan
public class MqttSenderConfig {

@Value("${spring.mqtt.username}")
private String username;
@Value("${spring.mqtt.password}")
private String password;
@Value("${spring.mqtt.url}")
private String hostUrl;
@Value("${spring.mqtt.client.id}")
private String clientId;
@Value("${spring.mqtt.default.topic}")
private String defaultTopic;
@Value("${spring.mqtt.default.completionTimeout}")
private int completionTimeout;

@Bean
public MqttConnectOptions getMqttConnectOptions() {
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setCleanSession(true);
mqttConnectOptions.setConnectionTimeout(10);
mqttConnectOptions.setKeepAliveInterval(90);
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setUserName(username);
mqttConnectOptions.setPassword(password.toCharArray());
mqttConnectOptions.setServerURIs(new String[]{hostUrl});
mqttConnectOptions.setKeepAliveInterval(2);
return mqttConnectOptions;
}

@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
factory.setConnectionOptions(getMqttConnectOptions());
return factory;
}

@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
public MessageHandler mqttOutbound() {
MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory());
messageHandler.setAsync(true);
messageHandler.setDefaultTopic(defaultTopic);
return messageHandler;
}

@Bean
public MessageChannel mqttOutboundChannel() {
return new DirectChannel();
}

}

接口服务:

@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
public interface MqttGateway {

void sendToMqtt(String data, @Header(MqttHeaders.TOPIC) String topic);

}

源码:gitee.com/52itstyle/smart-parking



END


顺便给大家推荐一个GitHub项目,这个 GitHub 整理了上千本常用技术PDF,绝大部分核心的技术书籍都可以在这里找到,

GitHub地址:https://github.com/javadevbooks/books

Gitee地址:https://gitee.com/javadevbooks/books

电子书已经更新好了,你们需要的可以自行下载了,记得点一个star,持续更新中..


浏览 77
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报