vernemq webhook 集成使用
vernemq支持插件化开发,包含了lua的,以及基于webhook的,基于webhook 的好处是灵活,同时保留的钩子比较多,我们可以灵活扩展
同时webhook可以基于多语言开发,缺点当然也有,webhook 的可靠性以及稳定性(也比较简单,扩展多集群)
以下是关于vernemq webhook的一个试用,环境基于docker-compose 运行(webhook为了简化试用了benthos)
系统说明
参考部署图
环境准备
- docker-compose 文件
version: "3"
services:
haproxy:
image: haproxytech/haproxy-debian
networks:
app_net:
ipv4_address: 172.16.238.9
volumes:
- "./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg"
ports:
- "1883:1883"
- "10080:10080"
webhook:
image: jeffail/benthos
volumes:
- "./webhook.yaml:/benthos.yaml"
ports:
- "4195:4195"
networks:
app_net:
ipv4_address: 172.16.238.11
mq1:
image: vernemq/vernemq
ports:
- "8080:8080"
- "8888:8888"
environment:
- "DOCKER_VERNEMQ_ACCEPT_EULA=yes"
- "DOCKER_VERNEMQ_ALLOW_ANONYMOUS=on"
networks:
app_net:
ipv4_address: 172.16.238.10
mq2:
image: vernemq/vernemq
environment:
- "DOCKER_VERNEMQ_ACCEPT_EULA=yes"
- "DOCKER_VERNEMQ_ALLOW_ANONYMOUS=on"
- "DOCKER_VERNEMQ_DISCOVERY_NODE=172.16.238.10"
networks:
app_net:
ipv4_address: 172.16.238.12
mq3:
image: vernemq/vernemq
environment:
- "DOCKER_VERNEMQ_ACCEPT_EULA=yes"
- "DOCKER_VERNEMQ_ALLOW_ANONYMOUS=on"
- "DOCKER_VERNEMQ_DISCOVERY_NODE=172.16.238.10"
networks:
app_net:
ipv4_address: 172.16.238.13
networks:
app_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
haproxy 配置
global
log 127.0.0.1 local2
maxconn 4000
# turn on stats unix socket
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode tcp
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind 0.0.0.0:1883
default_backend app
backend app
balance roundrobin
server app1 172.16.238.10:1883 check
server app2 172.16.238.13:1883 check
server app3 172.16.238.13:1883 check
listen stats
bind 0.0.0.0:10080
mode http
log global
maxconn 10
timeout queue 100s
http-request use-service prometheus-exporter if { path /metrics }
stats enable
stats uri /stats
webhook 配置(基于benthos)
input:
type: broker
broker:
inputs:
- type: http_server
http_server:
path: /
processors:
- type: text
text:
operator: prepend
value: "get message: "
output:
type: stdout
- 简单nodejs mqtt 连接
pacakge.json
{
"name": "client",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"mqtt": "^4.2.6"
},
"scripts": {
"app":"node app.js"
}
}
app.js
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://127.0.0.1')
client.on('connect', function () {
client.subscribe('demoapp', function (err) {
if (!err) {
client.publish('presence', 'Hello mqtt')
}
})
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
})
- 集群状态
使用webhook
启用webhook插件(需要进入vernemq容器操作,而且每个都需要开启)
- 启用插件命令
vmq-admin plugin enable --name vmq_webhooks
- 注册hooks
vmq-admin webhooks register hook=on_publish endpoint="http://172.16.238.11:4195"
vmq-admin webhooks register hook=on_subscribe endpoint="http://172.16.238.11:4195"
- 查看hooks 信息
vmq-admin webhooks show
效果
- 发送消息
- 查看webhook 消息
进入benthos容器
说明
以上是一个简单的试用,webhook 部分试用了benthos(很强大,配置好了,可以直接进行数据写入操作),同时为了保证系统的可靠
使用了haproxy,如果生产使用我们可以基于haproxy 的prometheus 监控能力以及vernemq暴露的监控能力方便的进行系统监控
参考资料
https://docs.vernemq.com/plugindevelopment/webhookplugins
https://github.com/Jeffail/benthos
https://www.npmjs.com/package/mqtt
https://github.com/rongfengliang/vernemq-webhook-learnning