搭建MQTT服务器及客户端搭建(Windows) :

JAVA MQTT 订阅与发布_java

项目结构:

JAVA MQTT 订阅与发布_java_02

一、首先第一步,加入pom包:

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

二、第二步,yml文件增加如下:

server:
  port: 9999
spring:
  mqtt:
    username: admin # 用户名
    password: admin_public # 密码
    hostUrl: tcp://127.0.0.1:1883 # tcp://ip:端口
    clientId: ${random.value} # 客户端id
    defaultTopic: message01,message02 #多个主题逗号分隔
    timeout: 100 # 超时时间 (单位:秒)
    keepalive: 60 # 心跳 (单位:秒)

#其中的defaultTopic在代码中会手动订阅

三、第三步,我们需要加入4个类

分别是 配置类:

1、MqttConfig 发布连接类:

2、MqttPushClient 订阅类:

3、MqttSubClient 回调类:

4、PushCallback

配置类:

package com.taskmovedata.mqtt;

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;

/**
 * 配置类
 * @author wfeil211@foxmail.com
 * @date 2023-2-11 20:11:46
 */
@Component
@ConfigurationProperties("spring.mqtt")
public class MqttConfig {

    @Autowired
    private MqttPushClient mqttPushClient;

    @Autowired
    private MqttSubClient mqttSubClient;

    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 连接地址
     */
    private String hostUrl;
    /**
     * 客户Id
     */
    private String clientId;
    /**
     * 默认连接话题
     */
    private String defaultTopic;
    /**
     * 超时时间
     */
    private int timeout;
    /**
     * 保持连接数
     */
    private int keepalive;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHostUrl() {
        return hostUrl;
    }

    public void setHostUrl(String hostUrl) {
        this.hostUrl = hostUrl;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getDefaultTopic() {
        return defaultTopic;
    }

    public void setDefaultTopic(String defaultTopic) {
        this.defaultTopic = defaultTopic;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public int getKeepalive() {
        return keepalive;
    }

    public void setKeepalive(int keepalive) {
        this.keepalive = keepalive;
    }

    /**
     * 连接至mqtt服务器,获取mqtt连接
     *
     * @return
     */
    @Bean
    public MqttPushClient getMqttPushClient() {
        //连接至mqtt服务器,获取mqtt连接
        mqttPushClient.connect(hostUrl, clientId, username, password, timeout, keepalive);
        //一连接mqtt,就订阅默认需要订阅的主题(如test_queue)
        mqttSubClient.subScribeDataPublishTopic(defaultTopic);
        return mqttPushClient;
    }

}

#此类在getMqttPushClient方法中会发起连接和订阅我们配置的类

发布连接类:

package com.taskmovedata.mqtt;
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;

/**
 * 发布连接类
 * @author wfeil211@foxmail.com
 * @date 2023-2-11 20:11:46
 */
@Slf4j
@Component
public class MqttPushClient {

    @Autowired
    private PushCallback pushCallback;

    private static MqttClient client;

    public static void setClient(MqttClient client) {
        MqttPushClient.client = client;
    }

    public static MqttClient getClient() {
        return client;
    }

    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);
            // automaticReconnect 为 true 表示断线自动重连,但仅仅只是重新连接,并不订阅主题;在 connectComplete 回调函数重新订阅
            options.setAutomaticReconnect(true);
            MqttPushClient.setClient(client);
            try {
                //设置回调类
                client.setCallback(pushCallback);
                //client.connect(options);
                IMqttToken iMqttToken = client.connectWithResult(options);
                boolean complete = iMqttToken.isComplete();
                log.info("MQTT连接" + (complete ? "成功" : "失败"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 发布,默认qos为0,非持久化
     *
     * @param topic       主题名
     * @param pushMessage 消息
     */
    public void publish(String topic, String pushMessage) {
        publish(0, false, topic, pushMessage);
    }

    /**
     * 发布
     *
     * @param qos
     * @param retained
     * @param topic
     * @param pushMessage
     */
    public void publish(int qos, boolean retained, String topic, String pushMessage) {
        MqttMessage message = new MqttMessage();
        message.setQos(qos);
        message.setRetained(retained);
        message.setPayload(pushMessage.getBytes());
        MqttTopic mTopic = MqttPushClient.getClient().getTopic(topic);
        if (null == mTopic) {
            log.error("主题不存在:{}", mTopic);
        }
        try {
            mTopic.publish(message);
        } catch (Exception e) {
            log.error("mqtt发送消息异常:", e);
        }
    }
}

#此类中包含连接方法以及发布方法,因mqtt服务有可能终端,所以在连接时需要设置自动重连,并且在重连时要重新订阅,此过程在回调中实现

订阅类:

package com.taskmovedata.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Component;

/**
 * 订阅类
 * @author wfeil211@foxmail.com
 * @date 2023-2-11 20:11:46
 */
@Slf4j
@Component
public class MqttSubClient {

    public void subScribeDataPublishTopic(String defaultTopic) {
        //订阅test_queue主题
        String mqtt_topic[] = defaultTopic.split(",");
        for (int i = 0; i < mqtt_topic.length; i++) {
            subscribe(mqtt_topic[i], 0);//订阅主题
        }
    }

    /**
     * 订阅某个主题,qos默认为0
     *
     * @param topic
     */
    public void subscribe(String topic) {
        subscribe(topic, 0);
    }

    /**
     * 订阅某个主题
     *
     * @param topic 主题名
     * @param qos
     */
    public void subscribe(String topic, int qos) {
        try {
            MqttClient client = MqttPushClient.getClient();
            if (client == null) return;
            client.subscribe(topic, qos);
            log.info("订阅主题:{}", topic);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

}

#该类包含我们订阅配置主题方法以及普通订阅方法

回调类:

package com.taskmovedata.mqtt;

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;

/**
 * 回调类
 * @author wfeil211@foxmail.com
 * @date 2023-2-11 20:11:46
 */
@Slf4j
@Component
public class PushCallback implements MqttCallback {
    @Autowired
    private MqttConfig mqttConfig;

    @Override
    public void connectionLost(Throwable cause) {
        // 连接丢失后,一般在这里面进行重连
        log.info("连接断开,正在重连");
        mqttConfig.getMqttPushClient();
    }

    /**
     * 发送消息,消息到达后处理方法
     *
     * @param token
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        log.info("deliveryComplete---------" + token.isComplete());
    }

    /**
     * 订阅主题接收到消息处理方法
     *
     * @param topic
     * @param message
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) {
        // subscribe后得到的消息会执行到这里面,这里在控制台有输出
        log.info("接收消息主题 : " + topic);
        log.info("接收消息Qos : " + message.getQos());
        log.info("接收消息内容 : " + new String(message.getPayload()));

    }

}

#此类包含断开重连,我们所有被订阅的主题消息都将在这里被接收

四、第四步,开始测试

到这里我们代码实现部分就已经完成了,现在我们来使用工具测试一下吧

1)、启动后端服务:

JAVA MQTT 订阅与发布_spring_03

2)、客户端配置与服务端yml 文件中相同的 username、password、hostUrl、clientId

(随机或配置都可以)、主题(topic 或 defaultTopic)、消息质量(qos:0-至多一次;

1-至少一次;2-最对一次)进行测试

JAVA MQTT 订阅与发布_spring_04


JAVA MQTT 订阅与发布_spring_05


JAVA MQTT 订阅与发布_后端_06


JAVA MQTT 订阅与发布_后端_07

五、登录EMQ X控制台

浏览器地址栏中直接输入:http://127.0.0.1:18083/

或者 http://localhost:18083/

正常情况下会看到以下登陆界面:

JAVA MQTT 订阅与发布_spring_08

可用 用户名:admin 密码:public 进行登陆 (登录修改后为:admin_public)

正常登陆后界面如下(未更改语言显示设置):

JAVA MQTT 订阅与发布_后端_09

2.3.更改EMQ X控制台语言显示为中文

JAVA MQTT 订阅与发布_spring_10


JAVA MQTT 订阅与发布_spring_11


JAVA MQTT 订阅与发布_JAVA MQTT 订阅与发布_12


JAVA MQTT 订阅与发布_JAVA MQTT 订阅与发布_13


JAVA MQTT 订阅与发布_后端_14


JAVA MQTT 订阅与发布_spring_15


JAVA MQTT 订阅与发布_java_16


JAVA MQTT 订阅与发布_spring boot_17


JAVA MQTT 订阅与发布_后端_18


JAVA MQTT 订阅与发布_java_19


JAVA MQTT 订阅与发布_spring_20


JAVA MQTT 订阅与发布_spring_21

MQTT介绍,服务器(EMQ X)搭建,客户端(mqtt-spy,安卓)使用,java编程示例