1、概述
MQTT(Message Queuing Telemetry Transport)是基于二进制消息的发布/订阅编程模式的消息协议,非常适合需要低功耗和网络带宽有限的IoT场景。这里整理关于mqtt在springboot中集成使用。
2、maven依赖
<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>
3、自定义Springboot的yml配置文件以及相关配置类
com:
mqtt:
inbound:
url: tcp://127.0.0.1:1883
clientId: familyServerIn
topics: topic1,topic2
outbound:
urls: tcp://127.0.0.1:1883
clientId: familyServerOut
topic: topic1
3.1 MqttProperties配置类(读取yml)
@ConfigurationProperties(prefix = "com.mqtt")
public class MqttProperties {
private MqttInboundProperties inbound;
private MqttOutboundProperties outbound;
public MqttInboundProperties getInbound() {
return inbound;
}
public void setInbound(MqttInboundProperties inbound) {
this.inbound = inbound;
}
public MqttOutboundProperties getOutbound() {
return outbound;
}
public void setOutbound(MqttOutboundProperties outbound) {
this.outbound = outbound;
}
}
3.2 关于mqtt数据消费端配置类
public class MqttInboundProperties {
private String url;
private String username;
private String password;
private String clientId;
private String topics;
//getter,setter方法
}
3.3 关于mqtt数据生产端配置类
public class MqttOutboundProperties {
private String urls;
private String username;
private String password;
private String clientId;
private String topic;
//getter,setter方法
}
3.4 配置类生效配置
@Configuration
@EnableConfigurationProperties(MqttProperties.class)
public class MqttPropertiesConfiguration {
}
Created By 2018-04-09
关于使用配置详见Springboot集成mqtt(二)