一.MQTT服务器的安装
1.安装依赖
yum install gcc-c++ cmake openssl-devel libuuid-devel c-ares-devel uuid-devel libwebsockets-devel.x86_64 libwebsockets.x86_64 -y
2.官网下载mosquitto
Eclipse Mosquitto
或者使用命令
wget --no-check-certificate https://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz
3.解压 编译 安装
tar -zxvf mosquitto-1.6.8.tar.gz
cd mosquitto-1.6.8
make
make install
4.创建配置文件
cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
vim /etc/mosquitto/mosquitto.conf
配置文件中默认使用user mosquitto。 如果不想创建此用户,可以修改成root
5.启动、查看、关闭程序
运行程序
mosquitto -c /etc/mosquitto/mosquitto.conf -d
ps查看
ps -aux | grep mosquitto
关闭程序
kill -9 $(pidof mosquitto)
6.设置用户名密码
mosquitto_passwd -c /etc/mosquitto/pwfile.conf user
7.再次编辑配置文件
vim /etc/mosquitto/mosquitto.conf
修改以下内容:
646 # Defaults to true if no other security options are set. If password_file
or
647 # psk_file
is set, or if an authentication plugin is loaded which implements
648 # username/password or TLS-PSK checks, then allow_anonymous
defaults to
649 # false.
650 #
651 #allow_anonymous true
652 allow_anonymous ture
......
666 # See the TLS client require_certificate and use_identity_as_username options
667 # for alternative authentication options. If an auth_plugin is used as well as
668 # password_file, the auth_plugin check will be made first.
669 #password_file
670 password_file /etc/mosquitto/pwfile.conf
8.使用命令发送和接受
1.打开订阅者
无密码
mosquitto_sub -t test1
有密码
mosquitto_sub -u ycgl -P ycglmosquitto123 -t test1
2.打开发布者
无密码
mosquitto_pub -t test1 -m "发布内容"
有密码
mosquitto_pub -u ycgl -P ycglmosquitto123 -t test1 -m "发布内容"
二.MQTT后端的使用
1.发布者测试用例
@Test
public void test() {
try {
String broker = "tcp://localhost:1883";
String clientId = MqttClient.generateClientId();
IMqttClient mqttClient = new MqttClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
mqttClient.connect(connOpts);
// 模拟要发送的消息内容
String messageContent = "消息测试";
MqttMessage message = new MqttMessage(messageContent.getBytes());
message.setQos(0); // 设置 QoS 等级
// 发布消息到指定主题
mqttClient.publish("test", message);
mqttClient.disconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}
2.订阅者测试用例
public static void main(String[] args) {
try {
String broker = "tcp://localhost:1883";
String clientId = MqttClient.generateClientId();
IMqttClient mqttClient = new MqttClient(broker, clientId);
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
System.out.println("Connection lost!");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("Received message: " + new String(message.getPayload())
+ " on topic " + topic);
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// Not used in this example
}
});
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
mqttClient.connect(connOpts);
mqttClient.subscribe("test", 0);
} catch (MqttException e) {
e.printStackTrace();
}
}