物联网实战|Spring Boot MQTT平台 |emqx broker实战

物联网实战|Spring Boot MQTT平台 |emqx broker实战

📑 目录

1. 物联网实战|Spring Boot MQTT平台 |emqx broker实战

1.1 emqx官网

1.2 docker部署emqx

1.2.1 前期准备和权限开通

1.2.2 docker构建emqx broker应用

1.3 spring-boot接入emqx broker

1.3.1 pom依赖

1.3.2 源码

1.3.3 客户端注册部分代码

1.3.4 客户端注册信息

1.3.5 订阅部分代码

1.3.6 emqx 模拟下发

1.3.7 操作

EMQX 是一款「无限连接,任意集成,随处运行」的大规模分布式物联网接入平台 基于版本6.2.2

emqx官网

https://docs.emqx.com/zh/emqx/latest/

docker部署emqx

其中18083 为网页访问端口 (http://127.0.0.1:28084/#/dashboard/overview)
  • 账号
默认为 admin/public

前期准备和权限开通

bash

mkdir -p /data/emqx/
chmod -R 777 /data/

docker构建emqx broker应用

bash

  docker run -d   \
  --name emqx   \
  --restart always   \
  -p 1883:1883   \
  -p 8083:8083   \
  -p 8084:8084   \
  -p 8883:8883   \
  -p 28084:18083  \
  -p 18085:18085   \
  -v /data/emqx/data:/opt/emqx/data   \
  -v /data/emqx/log:/opt/emqx/log   \
  -e EMQX_COPY_CONF=true   \
  emqx/emqx:latest

img

img

spring-boot接入emqx broker

pom依赖

xml

<class="tk-tag">dependencies>
    <!-- HiveMQ MQTT5 客户端(同时兼容 MQTT3.1.1) -->
    <class="tk-tag">dependency>
        <class="tk-tag">groupId>com.hivemq</class="tk-tag">groupId>
        <class="tk-tag">artifactId>hivemq-mqtt-client</class="tk-tag">artifactId>
        <class="tk-tag">version>1.3.15</class="tk-tag">version>
    </class="tk-tag">dependency>
</class="tk-tag">dependencies>

<class="tk-tag">dependencyManagement>
    <class="tk-tag">dependencies>
        <!-- Spring Boot Starter -->
        <class="tk-tag">dependency>
            <class="tk-tag">groupId>org.springframework.boot</class="tk-tag">groupId>
            <class="tk-tag">artifactId>spring-boot-dependencies</class="tk-tag">artifactId>
            <class="tk-tag">version>3.5.11</class="tk-tag">version>
            <class="tk-tag">type>pom</class="tk-tag">type>
            <class="tk-tag">scope>import</class="tk-tag">scope>
        </class="tk-tag">dependency>
    </class="tk-tag">dependencies>
</class="tk-tag">dependencyManagement>

源码

https://gitee.com/kcnf-iot/iot-sample/tree/master/emqx

客户端注册部分代码

java

package com.jysemel.iot.client;

import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient;
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.UUID;

@Slf4j
@Configuration
public class MqttConfig {

    @Bean
    public Mqtt5AsyncClient mqtt5AsyncClient() {
        // 使用 Builder 模式创建异步客户端
        Mqtt5AsyncClient client = Mqtt5Client.builder()
                .identifier("client" + UUID.randomUUID()) // 设置客户端ID,需唯一
                .serverHost("192.168.0.19") // Broker 地址
                .serverPort(1883) // 默认非加密端口
                .buildAsync(); // 构建异步客户端

        // 连接到 Broker
        client.connect()
                .whenComplete((connAck, throwable) -> {
                    if (throwable != null) {
                        System.err.println("MQTT 连接失败: " + throwable.getMessage());
                        // 可以在这里添加重试逻辑
                    } else {
                        System.out.println("MQTT 连接成功!");
                    }
                });
        return client;
    }
}

客户端注册信息

img

订阅部分代码

java

package com.jysemel.iot.client;

import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
public class MqttSubscribeService {

    @Autowired
    private Mqtt5AsyncClient mqttClient;

    // 使用 @PostConstruct 在 Bean 初始化后自动订阅
    @PostConstruct
    public void subscribe() {
        String topic = "server/v1/test";
        mqttClient.subscribeWith()
                .topicFilter(topic) // 订阅的主题
                .qos(MqttQos.AT_LEAST_ONCE)
                .callback(publish -> {
                    // 处理收到的消息
                    String payload = new String(publish.getPayloadAsBytes(), StandardCharsets.UTF_8);
                    System.out.println("收到消息,主题: " + publish.getTopic() + ", 内容: " + payload);
                })
                .send()
                .whenComplete((subAck, throwable) -> {
                    if (throwable != null) {
                        System.err.println("订阅失败: " + throwable.getMessage());
                    } else {
                        System.out.println("订阅主题 'test/topic' 成功!");
                    }
                });
    }
}

emqx 模拟下发

json

{ "msg": "hello" }

操作

img

img

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值