系統(tǒng)安裝
類似教程和文檔太多,針對windows和linux有不同的安裝方式,這里就不具體介紹。主要介紹下docker方式安裝。
docker安裝
Tips
運行容器前,沒有網(wǎng)橋,請先創(chuàng)建網(wǎng)橋,用于容器間通訊: docker network create -d bridge fastbee-bridge
執(zhí)行一下命令安裝emqx5.1
docker run \
--name emqx \
--publish 1883:1883 \
--publish 8083:8083 \
--publish 8883:8883 \
--publish 8084:8084 \
--publish 18083:18083 \
--env TimeZone=Asia/Shanghai \
--volume /etc/localtime:/etc/localtime \
--restart unless-stopped \
--detach \
emqx:5.1
Emqx5配置
Emqx配置Http認證和Webhook(處理客戶端上下線),有兩種方式:
- 通過emqx.conf文件配置,已有配置好的文件位于
docker/data/emqx/ect
文件夾; - 通過18083端口,打開Emqx控制臺,創(chuàng)建客戶端認證,數(shù)據(jù)橋接和規(guī)則
- 通過emqx.conf文件配置,已有配置好的文件位于
Emqx賬號
- 控制臺默認賬號 (admin,public)
- docker-compose方式部署,emqx.conf中配置的賬號(admin,admin123)
注意
Emqx控制臺修改的賬號,配置的Http認證、Webhook和規(guī)則會被emqx.conf中對應(yīng)配置覆蓋掉。官網(wǎng)解釋如下:
- 通過 Dashboard、HTTP API 或 CLI 進行的更改將在運行時寫入 data/configs/cluster.hocon 文件并立即生效。
- 如果相同的配置項在 etc/emqx.conf 文件中設(shè)置不同值,則在重新啟動后,最終生效的是 etc/emqx.conf 中的配置。 為避免混淆,強烈建議不要在 cluster.hocon 和 emqx.conf 中具有相同的配置鍵。
emqx.conf配置文件中存在任何規(guī)則,設(shè)備消息轉(zhuǎn)發(fā)的規(guī)則,也要配置在emqx.conf中。
1.配置文件方式
下面內(nèi)容復制到emqx.conf文件中
# 設(shè)置控制臺端口和默認賬號
dashboard {
listeners.http {
bind = 18083
}
default_username = "admin"
default_password = "admin123"
}
# http 認證
authentication = [
{
mechanism = password_based
backend = http
enable = true
method = post
url = "http://177.7.0.13:8080/iot/tool/mqtt/authv5"
body {
clientid = "${clientid}"
username = "${username}"
password = "${password}"
peerhost = "${peerhost}"
}
headers {
"Content-Type" = "application/json"
"X-Request-Source" = "EMQX"
}
}
]
# WebHook(匹配上線和下線規(guī)則后觸發(fā))
bridges {
webhook.fastbee_hook =
{
enable = true
connect_timeout = 15s
retry_interval = 60s
pool_type = random
pool_size = 8
enable_pipelining = 100
max_retries = 2
request_timeout = 15s
method = post
url = "http://177.7.0.13:8080/iot/tool/mqtt/webhookv5"
body = "{\"clientid\" : \"${clientid}\",\"event\" : \"${event}\",\"peername\" : \"${peername}\"}"
headers = { accept = "application/json" "cache-control" = "no-cache" connection = "keep-alive" "content-type" = "application/json" "keep-alive" = "timeout=5"}
}
}
# 規(guī)則(處理上線和下線)
rule_engine {
ignore_sys_message = true
jq_function_default_timeout = 10s
rules.fastbee_rule =
{
sql = "SELECT * FROM \"t/#\",\"$events/client_connected\", \"$events/client_disconnected\", \"$events/session_subscribed\""
actions = ["webhook:fastbee_hook"]
enable = true
description = "處理設(shè)備上下線和訂閱完主題的規(guī)則"
}
}
2.控制臺創(chuàng)建方式
a.配置Http認證

請求方式:POST
請求地址:http://177.7.0.13:8080/iot/tool/mqtt/authv5 (地址可以是內(nèi)網(wǎng)或者外網(wǎng),確保能訪問)
請求Body:
{
"clientid": "${clientid}",
"password": "${password}",
"username": "${username}",
"peerhost": "${peerhost}"
}
b.配置WebHook
Webhook配置,先創(chuàng)建數(shù)據(jù)橋接,然后創(chuàng)建規(guī)則,規(guī)則跟數(shù)據(jù)橋接關(guān)聯(lián)

數(shù)據(jù)橋接名稱:fastbee_hook (隨意填寫)
請求方式:POST
請求地址:http://177.7.0.13:8080/iot/tool/mqtt/webhookv5 (地址可以是內(nèi)網(wǎng)或者外網(wǎng),確保能訪問)
請求Body:
{"clientid" : "${clientid}", "event" : "${event}", "peername" : "${peername}"}

規(guī)則名稱:fastbee_rule (隨意填寫)
SQL編輯器內(nèi)容(分別代表客戶端連接/斷開連接/主題訂閱完成):
SELECT * FROM "t/#","$events/client_connected", "$events/client_disconnected", "$events/session_subscribed"
動作:選擇創(chuàng)建的數(shù)據(jù)橋接fastbee_hook
3.設(shè)備上下線確認
為保證設(shè)備準確上下線,項目運行后,可在emqx控制臺創(chuàng)建API密鑰,并且將apiKey和apiSecret添加到application.yml中,重啟Java服務(wù)


