Middleware: SpringBoot-JAVA integrates MQTT communication

1. Environment Win10;

2、JDK 1.8;

3、SpringBoot 2.7.5;

4、MQTTV3 1.2.5

5. Reference for local installation and use of EMQX server: MQTT learning record (1. Windows)_I don't know the blog-CSDN blog_mqtt default password

6. MQTTX official client visualization tool installation reference: MQTT learning record (1, Windows)_I don't know the blog-CSDN blog_mqtt default password

7. Source code download:  SpringBoot project JAVA MQTT communication in resource download

 

This example integrates MQTT communication at the back end, and it can also be used directly at the front end. For the method, refer to: Middleware: Front-end JS integrates MQTT communication_I don't know blog-CSDN blog 

1. Introduction to local installation and use of EMQX server

Reference: MQTT learning record (1, Windows)_I don't know the blog-CSDN blog_mqtt default password

 

 

2. Installation and use of MQTTX visual operation tool

Reference: MQTT learning record (1, Windows)_I don't know the blog-CSDN blog_mqtt default password

3. SpringBoot JAVA client

1. The project structure is as follows:

2. Refer to org.eclipse.paho.client.mqttv3 for MQTT communication. The POM.xml file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>mqtt_client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mqtt_client</name>
    <description>mqtt_client</description>
    <properties>
        <java.version>8</java.version>
        <mqttv3.version>1.2.5</mqttv3.version>
        <fastjson.version>1.2.78</fastjson.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mqtt-->
        <dependency>
            <groupId>org.eclipse.paho</groupId>
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
            <version>${mqttv3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、application.yml

server:
  port: 8088
spring:
  # mqtt 配置
  mqtt:
    use-flag: true                             # 是否启用MQTT
    username: admin                            # 账号
    password: public                           # 密码
    host-url: tcp://127.0.0.1:11883            # mqtt连接tcp地址
    client-id: ${random.int}                   # 客户端Id,不能相同,采用随机数 ${random.value}
    default-topic: mqttDemoTopic               # 默认主题
    default-qos: 1                             # 消息发送方式
    default-retained: false                    # 是否保留
    timeout: 30                                # 超时时间
    keepalive: 30                              # 保持连接
    clearSession: true                         # 清除会话(设置为false,断开连接,重连后使用原来的会话 保留订阅的主题,能接收离线期间的消息)

The meaning of MQTT parameters is as noted in the above code. The MQTT connection uses the tcp protocol, and the tcp protocol port in the EMQX server is the standard. For example, in this example, the tcp protocol MQTT port of EMQX is 1883 / 11883. The project can use these two ports as a client .

 4. implements ApplicationRunner loads the run method when the project starts, and calls emqxClient.connect(); to connect;

 5. Publish the topic (if the topic does not exist, it will create a new one), subscribe to the existing topic

 6. The simulation test manually calls the publishing topic and subscribes to the topic

 7. Message callback, that is, after subscribing to a topic, when another client or hardware sensor system sends a message to the topic of the middleware EMQX, the JAVA client will automatically receive the message under the premise of subscribing to this topic

4. Start the test

1. Create/subscribe to the default topic by default at project startup

The status can be seen on the server side

2. Simulate sending a message on the MQTTX client, and receive the message on the JAVA client

3. Simulate creating/publishing new topics on the JAVA client

 4. MQTTX also subscribes to new topics and publishes them again on the JAVA client

 

5. Unsubscribe from a topic

Guess you like

Origin blog.csdn.net/chenyang_wei/article/details/128470071