MQTT——EMQX学习笔记06——WebHook

一、开启WebHook插件

这个功能是由emqx_web_hook插件提供支持的。WebHook有什么用,它用于监控一些规则,比如发布、订阅消息。当监控到这些规则触发时,会向web服务器(spring boot项目)发送一些数据(消息)。

这个功能是默认关闭的,我们先去开启这个功能。
在这里插入图片描述

二、绑定web服务器

[root@iz2zedg4ylq9iqtwm11wecz plugins]# pwd
/my/emqx/conf/etc/plugins
[root@iz2zedg4ylq9iqtwm11wecz plugins]# ls
acl.conf.paho          emqx_lua_hook.conf
emqx_auth_http.conf    emqx_lwm2m.conf
emqx_auth_jwt.conf     emqx_management.conf
emqx_auth_ldap.conf    emqx_prometheus.conf
emqx_auth_mnesia.conf  emqx_psk_file.conf
emqx_auth_mongo.conf   emqx_recon.conf
emqx_auth_mysql.conf   emqx_retainer.conf
emqx_auth_pgsql.conf   emqx_rule_engine.conf
emqx_auth_redis.conf   emqx_sasl.conf
emqx_bridge_mqtt.conf  emqx_sn.conf
emqx_coap.conf         emqx_stomp.conf
emqx_dashboard.conf    emqx_telemetry.conf
emqx_exhook.conf       emqx_web_hook.conf
emqx_exproto.conf
[root@iz2zedg4ylq9iqtwm11wecz plugins]# vim emqx_web_hook.conf
# 将web.hook.url 修改为自己web服务器(spring boot项目的请求地址)的地址。
# web.hook.url = http://127.0.0.1:8088/mqtt/webhook

在这里插入图片描述

注意:1、重启emqx
2、这里的IP地址根据自己的进行填写,如果是docker安装的emqx,一定不是127.0.0.1

[root@iz2zedg4ylq9iqtwm11wecz plugins]# docker restart myemqx
myemqx

三、配置事件规则

# 文件所在位置:
 /etc/plugins/emqx_web_hook.conf

在这里插入图片描述
事件规则的详情可以查看 官网

## 格式示例
web.hook.rule.<Event>.<Number> = <Rule>

## 示例值
web.hook.rule.message.publish.1 = {"action": "on_message_publish", "topic": "a/b/c"}
web.hook.rule.message.publish.2 = {"action": "on_message_publish", "topic": "foo/#"}

四、创建web服务器

直接创建spring boot项目即可

(一)导入pom依赖

<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>
</dependencies>

(二)改yml

# 注意这个端口号必须跟配置文件里面的一致
# /etc/plugins/emqx_web_hook.conf
# web.hook.url = http://127.0.0.1:8088/mqtt/webhoo
server.port=8088

(三)代码

在这里插入图片描述

package com.lihua.webhook.controller;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hs
 * @date 2021/10/26 17:32
 */
@RestController
@RequestMapping("/mqtt")
public class WebHookController {

    private Map<String,Boolean> clientStatus = new HashMap<>();

    @PostMapping("/webhook")
    public void hook(@RequestBody Map<String,Object> params){
        System.out.println("emqx 触发 webhook,请求体数据={}"+params);

        String action = (String) params.get("action");
        String clientId = (String) params.get("clientid");
        if(action.equals("client_connected")){
            System.out.println("客户端{}接入本系统"+clientId);
            clientStatus.put(clientId,true);
        }

        if(action.equals("client_disconnected")){
            System.out.println("客户端{}下线"+clientId);
            clientStatus.put(clientId,false);
        }

    }

    @GetMapping("/allStatus")
    public Map getStatus(){
        return this.clientStatus;
    }
}

(四)测试

启动web hook服务器
连接一个客户端,
断开一个客户端,
查看控制台输出
在这里插入图片描述

注意:1、如果测试没有反应可能时服务器地址写错了。如果使用docker安装的话,ip一定不是127.0.0.1。 2、注意关闭win的防火墙
在这里插入图片描述

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值