pwd
/usr/local
wget http://mosquitto.org/files/source/mosquitto-1.4.5.tar.gz
tar -xzvf mosquitto-1.4.5.tar.gz
cd mosquitto-1.4.5.tar.gz
config.mk包括了多个选项, 可按需关闭或开启,但一旦开启则需要先安装对应的模块
vi config.mk
# Build with SRV lookup support.
WITH_SRV:=no
# Build using libuuid for clientid generation (Linux only - please report if
# supported on your platform).
WITH_UUID:=no
# Build with websockets support on the broker.
WITH_WEBSOCKETS:=no
yum install openssl-devel
yum install gcc-c++
yum install cmake
yum install libuuid-devel (WITH_UUID:=yes 需要)
make
make install
mosquitto默认以mosquitto用户启动,可以通过配置文件修改
groupadd mosquitto
useradd -g mosquitto mosquitto
mosquitto_passwd -c pwfile.example tianming(此为用户名,然后输入两次密码)
mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
成功将启动1883端口监听
mosquitto -c /etc/mosquitto/mosquitto.conf -d
[root@localhost mosquitto]# pwd
/etc/mosquitto
[root@localhost mosquitto]# mosquitto -c /etc/mosquitto/mosquitto.conf -d
[root@localhost mosquitto]# 1541836831: mosquitto version 1.4.5 (build date 2018-11-09 23:43:35-0800) starting
1541836831: Config loaded from /etc/mosquitto/mosquitto.conf.
1541836831: Opening ipv4 listen socket on port 1883.
1541836831: Opening ipv6 listen socket on port 1883.
[root@localhost ~]# ps -ef|grep mosquitto
mosquit+ 23929 1 0 23:52 pts/2 00:00:00 mosquitto -c /etc/mosquitto/mosquitto.conf -d
root 24427 24084 0 23:59 pts/3 00:00:00 grep --color=auto mosquitto
[root@localhost ~]# kill -9 23929
[root@localhost ~]#
[root@localhost ~]#servcie iptables stop --临时关闭防火墙
[root@localhost ~]#chkconfig iptables off --永久关闭防火墙
mosquitto -c /etc/mosquitto/mosquitto.conf -d
#MQTT
#MQTT-用户名
spring.mqtt.username=tianming
#MQTT-密码
spring.mqtt.password=tianming
#MQTT-服务器连接地址,如果有多个,用逗号隔开
spring.mqtt.url=tcp://192.168.122.29:1883
#MQTT-连接服务器默认客户端ID
spring.mqtt.client.id=65515
#MQTT-默认的消息推送主题,实际可在调用接口时指定
spring.mqtt.default.topic=topic
#超时时间,单位为秒
spring.mqtt.connectionTimeout=30
#会话心跳时间,单位为秒
spring.mqtt.keepAliveInterval=30
#MQTT主题
spring.mqtt.theme=MS/KD/65515
#AES128秘钥
spring.aes.key=asdfgjqwerio
# destination file directory
spring.destination.orderpush=/app/rookie
SpringBoot2.0集成MQTT实现消息推送功能
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
@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.connectionTimeout}")
private int connectionTimeout;
@Value("${spring.mqtt.keepAliveInterval}")
private int keepAliveInterval;
@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
factory.setUserName(username);
factory.setPassword(password);
factory.setServerURIs(new String[] { hostUrl });
// 设置超时时间
factory.setConnectionTimeout(connectionTimeout);
// 设置会话心跳时间
factory.setKeepAliveInterval(keepAliveInterval);
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();
}
}
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
@Component
@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
public interface MqttGateway {
void sendToMqtt(String data);
void sendToMqtt(String data, @Header(MqttHeaders.TOPIC) String topic);
// data:发送内容 topic:主题名称 qos:消息处理机制(值为:0,1,2,默认为0)
void sendToMqtt(String data, @Header(MqttHeaders.TOPIC) String topic, @Header(MqttHeaders.QOS) int qos);
}
int num = 0;
for (int i = 0; i < files.length; i++) {
InputStreamReader inReader = new InputStreamReader(new FileInputStream(files[i]), "GBK");
BufferedReader bReader = new BufferedReader(inReader);
contentLine = bReader.readLine();
while (contentLine != null) {
// 日志打印
LOGGER.info("读取的当前数据==》" + contentLine.trim());
// AES加密
String encryptContent = AESCoder.encrypt(aesKey, contentLine.trim());
// 发送到MQTT
mqttGateway.sendToMqtt(encryptContent, mqttTheme);
num++;
// 读取下一行
contentLine = bReader.readLine();
}
bReader.close();
}
SpringBoot2.0集成MQTT消息推送功能
SpringBoot2.0如何集成mqtt消息推送
SpringBoot2.0集成MQTT功能之消息订阅处理
参考博客:
[1]. https://www.cnblogs.com/littleatp/p/4835879.html