MQTT服务搭建与SpringBoot简单实现

一、MQTT服务器搭建

  1. 下载MQTT Broker

https://www.emqx.com/zh/try?product=broker

  1. 解压缩,启动emqx,查看状态,重置Dashboard密码

emqx.cmd start

emqx_ctl.cmd status

emqx_ctl.cmd admins passwd admin 123456

  1. 登录MQTT服务器控制台

http://127.0.0.1:18083

MQTT服务搭建与SpringBoot简单实现_第1张图片
4. 使用指南

https://www.emqx.io/docs/zh/latest/

二、MQTT客户端使用

  1. 下载MQTT Client

https://www.emqx.com/zh/try?product=MQTTX

三、Java连接MQTT

  1. 创建两个微服务MqttProviderApp和MqttConsumerApp作为Mqtt消息提供方和消费方;
<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.1.6.RELEASEversion>
parent>
<dependencies>
    
    <dependency>
        <groupId>org.springframework.integrationgroupId>
        <artifactId>spring-integration-mqttartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
dependencies>
  1. 启动类
@SpringBootApplication
public class MqttProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(MqttProviderApp.class, args);
    }
}
  1. 配置

(1) application.yml

# MqttProviderApp
spring:
  application:
    name: mqtt-provider
mqtt:
  url: tcp://localhost:1883
  username: admin
  password: 123456
  client:
    id: provider-id
  default:
    topic: topic
server:
  port: 60001
  
#MqttConsumerApp
spring:
  application:
    name: mqtt-consumer
mqtt:
  url: tcp://127.0.0.1:1883
  username: admin
  password: 123456
  client:
    id: consumer-id
  default:
    topic: topic
server:
  port: 60002

(2) 配置类

//MqttProviderApp
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.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;

@Slf4j
@Configuration
public class MqttProviderConfig {

    @Value("${mqtt.username}")
    private String username;

    @Value("${mqtt.password}")
    private String password;

    @Value("${mqtt.url}")
    private String url;

    @Value("${mqtt.client.id}")
    private String clientId;

    @Value("${mqtt.default.topic}")
    private String defaultTopic;

    private MqttClient client;

    /**
     * Bean初始化后连接到服务器
     */
    @PostConstruct
    public void init() {
        connect();
    }

    /**
     * 配置Mqtt客户端对象
     */
    public void connect() {
        try {
            client = new MqttClient(url, clientId, new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            //是否清空session,设置false表示服务器会保留客户端的连接记录(订阅主题,qos)
            //客户端重连之后能获取到服务器在客户端断开连接期间推送的消息,
            //设置为true表示每次连接服务器都是新身份
            options.setCleanSession(true);
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            //连接超时,单位为秒
            options.setConnectionTimeout(100);
            //设置心跳时间 单位为秒,表示服务器每隔 1.5*20秒的时间向客户端发送心跳判断客户端是否在线
            options.setKeepAliveInterval(20);
            //设置遗嘱消息的话题,若客户端和服务器之间的连接意外断开,服务器将发布客户端的遗嘱信息
            //QoS服务质量,QoS0至多一次,QoS1至少一次,QoS2只有一次
            options.setWill("willTopic", (clientId + "与服务器断开连接").getBytes(), 0, false);
            client.setCallback(new MqttProviderCallback());
            client.connect(options);
        } catch (MqttException e) {
            log.error("Mqtt Server has broken.");
        }
    }

    public void publish(String topic, String message, int qos, boolean retained) {
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setQos(qos);
        mqttMessage.setRetained(retained);
        mqttMessage.setPayload(message.getBytes());
        MqttTopic mqttTopic = client.getTopic(topic);
        //提供一种机制跟踪消息的传递进度,以非阻塞方式执行发布时跟踪消息的传递进度
        MqttDeliveryToken token;
        try {
            //将指定消息发布到主题,但不等待消息传递完成,返回的token可用于跟踪消息的传递状态

            token = mqttTopic.publish(mqttMessage);
            token.waitForCompletion();
        } catch (MqttException e) {
            log.info("Mqtt publish failed.");
        }
    }
}


import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
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.Value;

@Slf4j
public class MqttProviderCallback implements MqttCallback {

    @Value("${mqtt.client.id}")
    private String clientId;

    @Override
    public void connectionLost(Throwable throwable) {
        //与服务器断开的回调
        log.info(clientId + "与服务器断开连接");
    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        //消息到达的回调
        log.info("Message Arrived.");
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        //消息发布成功的回调
        IMqttAsyncClient client = iMqttDeliveryToken.getClient();
        log.info(client.getClientId() + "发布消息成功!");
    }
}

//MqttConsumerApp 
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;

@Slf4j
@Configuration
public class MqttConsumerConfig {

    @Value("${mqtt.username}")
    private String username;

    @Value("${mqtt.password}")
    private String password;

    @Value("${mqtt.url}")
    private String url;

    @Value("${mqtt.client.id}")
    private String clientId;

    @Value("${mqtt.default.topic}")
    private String defaultTopic;

    private MqttClient client;

    @PostConstruct
    public void init() {
        connect();
    }

    /**
     * 配置Mqtt客户端对象
     */
    public void connect() {
        try {
            client = new MqttClient(url, clientId, new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            //是否清空session,设置false表示服务器会保留客户端的连接记录(订阅主题,qos)
            //客户端重连之后能获取到服务器在客户端断开连接期间推送的消息,
            //设置为true表示每次连接服务器都是新身份
            options.setCleanSession(true);
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            //连接超时,单位为秒
            options.setConnectionTimeout(100);
            //设置心跳时间 单位为秒,表示服务器每隔 1.5*20秒的时间向客户端发送心跳判断客户端是否在线
            options.setKeepAliveInterval(20);
            //设置遗嘱消息的话题,若客户端和服务器之间的连接意外断开,服务器将发布客户端的遗嘱信息
            //QoS服务质量,QoS0至多一次,QoS1至少一次,QoS2只有一次
            options.setWill("willTopic", (clientId + "与服务器断开连接").getBytes(), 0, false);
            client.setCallback(new MqttConsumerCallback());
            client.connect(options);
            //订阅主题 消息等级和主题数组一一对应,服务端将按照指定等级给订阅了主题的客户端推送消息
            int[] qos = {1, 1};
            String[] topics = {"topic1", "topic2"};
            client.subscribe(topics, qos);
        } catch (MqttException e) {
            log.error("Mqtt Server has broken.");
        }
    }

    /**
     * 断开连接
     */
    public void disconnect() {
        try {
            client.disconnect();
        } catch (MqttException e) {
            log.error("Mqtt Server has broken.");
        }
    }
}

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;

@Slf4j
public class MqttConsumerCallback implements MqttCallback {

    @Override
    public void connectionLost(Throwable throwable) {
        //客户端断开连接后回调
        log.info("与服务器断开连接,可重连");
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        log.info("接收消息主题:{},QoS:{},消息内容:{},消息retained:{}", topic,
                mqttMessage.getQos(), new String(mqttMessage.getPayload()), mqttMessage.isRetained());
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        //消息发布成功回调
    }
}

(3) 测试接口

//MqttProviderApp
import org.lxx.stream.mqtt.provider.config.MqttProviderConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendController {

    @Autowired
    private MqttProviderConfig providerClient;

    @GetMapping("sendMessage")
    public String sendMessage(String topic, String message, int qos, boolean retained) {
        providerClient.publish(topic, message, qos, retained);
        return "发送成功";
    }

}

//MqttConsumerApp
@RestController
public class ReceiveController {

    @Autowired
    private MqttConsumerConfig client;

    @Value("${mqtt.client.id}")
    private String clientId;

    @GetMapping("connect")
    public String connect() {
        client.connect();
        return client + "连接到服务器";
    }

    @GetMapping("disconnect")
    public String disconnect() {
        client.disconnect();
        return clientId + "与服务器断开连接";
    }
}

(4) Postman测试

提供方生产消息:

curl --location --request GET 'http://localhost:60001/sendMessage?qos=1&retained=false&topic=topic1&message=Hello%20MQTT!'

消费方断开连接:

curl --location --request GET 'http://localhost:60002/disconnect'

消费方重新连接:

curl --location --request GET 'http://localhost:60002/connect'

MQTT服务搭建与SpringBoot简单实现_第2张图片

你可能感兴趣的:(#,spring,spring,boot,java,spring)