springboot+mqtt(emqx)

1.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>

2.application.yaml配置

hostUrl :mqtt服务所在ip和对应端口号(我的是emqx)
username和password测试demo可以随便填,线上环境肯定关闭了匿名访问,需要特定的用户名和密码
clientId:可以随便设置,但是对于一个mqtt服务需要避免重复的id,不然会出问题
topic:此处可以配置多个,然后订阅时候取出,循环订阅

spring:
  mqtt:
    username: username # 用户名
    password: password # 密码
    hostUrl: tcp://192.168.1.111:1883 # tcp://ip:端口
    clientId: cloud # 客户端id
    defaultTopic: d2c/message/#,device/response/#
    timeout: 100 # 超时时间 (单位:秒)
    keepalive: 60 # 心跳 (单位:秒)
    enabled: true # 是否使能mqtt功能

3.代码块

在这里插入图片描述

1.MqttConfig

此部分是为了项目启动时候连接mqtt服务的(emqx)我使用的mqtt服务是emqx

package com.xinhuan.llpr.cloud.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 mlcheng
 * @date 2022/8/5 10:01
 */
@Component
@ConfigurationProperties("spring.mqtt")
public class MqttConfig {
   
    @Autowired
    private MqttPushClient mqttPushClient;

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

    public String getUsername() {
   
        return username;
    }

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

    public String getPassword() {
   
        return password;
    }

    public void setPassword(String password) {
   
        
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值