clibing / common-mqtt

commont mqtt client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MQTT模块
  • 最简使用
common:
  mqtt:
    client-id: ${spring.application.name}
    host-urls:
      - "tcp://172.16.11.199:1883"
    username: ${spring.application.name}
    password:
    ...

使用证书

common:
  mqtt:
    hostUrls:
      - ssl://clibing.com:8883
    clientId: ${spring.application.name}${random.int}
    ssl: true
    root-ca: /root-cacert.pem
    client-ca: /client-cert.pem
    client-key: /client.key
  • 实现cn.linuxcrypt.common.service.IMessageHandler中的doHandler方法,用于处理接收到的消息

对消息的验证是否合法性。签名验证等操作

  • 注入发送消息服务,即可发送消息
public class MessageService {
    @Resource
    private IMqttSendService mqttSendService;

    /**
     * 发送
     * @param topicName
     * @param data
     * @return
     */
    public Result<Boolean> sendMessage(String topicName, Object data) {
        String payload = JsonUtil.toJson(data);
        log.debug("向话题: {}, 设置发送指令(payload): {}", topicName, payload);
        mqttSendService.sendToMqtt(topicName, payload);
        return Result.success("指令已经下发");
    }
}

About

commont mqtt client


Languages

Language:Java 100.0%