操作系統(tǒng)中安裝
類似教程和文檔太多,針對(duì)windows和linux有不同的安裝方式,這里就不具體介紹。主要介紹下docker方式安裝。
docker安裝
提示
運(yùn)行容器前,沒有網(wǎng)橋,請(qǐ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認(rèn)證和Webhook(處理客戶端上下線),有兩種方式:
- 通過(guò)emqx.conf文件配置,已有配置好的文件位于
docker/data/emqx/ect
文件夾; - 通過(guò)18083端口,打開Emqx控制臺(tái),創(chuàng)建客戶端認(rèn)證,數(shù)據(jù)橋接和規(guī)則
- 通過(guò)emqx.conf文件配置,已有配置好的文件位于
Emqx賬號(hào)
- 控制臺(tái)默認(rèn)賬號(hào) (admin,public)
- docker-compose方式部署,emqx.conf中配置的賬號(hào)(admin,admin123)
注意
Emqx控制臺(tái)修改的賬號(hào),配置的Http認(rèn)證、Webhook和規(guī)則會(huì)被emqx.conf中對(duì)應(yīng)配置覆蓋掉。官網(wǎng)解釋如下:
- 通過(guò) Dashboard、HTTP API 或 CLI 進(jìn)行的更改將在運(yùn)行時(shí)寫入 data/configs/cluster.hocon 文件并立即生效。
- 如果相同的配置項(xiàng)在 etc/emqx.conf 文件中設(shè)置不同值,則在重新啟動(dòng)后,最終生效的是 etc/emqx.conf 中的配置。 為避免混淆,強(qiáng)烈建議不要在 cluster.hocon 和 emqx.conf 中具有相同的配置鍵。
emqx.conf配置文件中存在任何規(guī)則,設(shè)備消息轉(zhuǎn)發(fā)的規(guī)則,也要配置在emqx.conf中。
1.配置文件方式
下面內(nèi)容復(fù)制到emqx.conf文件中
# 設(shè)置控制臺(tái)端口和默認(rèn)賬號(hào)
dashboard {
listeners.http {
bind = 18083
}
default_username = "admin"
default_password = "admin123"
}
# http 認(rèn)證
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.控制臺(tái)創(chuàng)建方式
a.配置Http認(rèn)證

請(qǐng)求方式:POST
請(qǐng)求地址:http://177.7.0.13:8080/iot/tool/mqtt/authv5 (地址可以是內(nèi)網(wǎng)或者外網(wǎng),確保能訪問(wèn))
請(qǐng)求Body:
{
"clientid": "${clientid}",
"password": "${password}",
"username": "${username}",
"peerhost": "${peerhost}"
}
b.配置WebHook(目前emqx已經(jīng)從5.1升級(jí)到5.7.2版本,兩個(gè)版本webHook創(chuàng)建方式不一樣,具體如下)
Webhook配置,先創(chuàng)建數(shù)據(jù)橋接,然后創(chuàng)建規(guī)則,規(guī)則跟數(shù)據(jù)橋接關(guān)聯(lián)
5.1版本:

5.7.2版本:
數(shù)據(jù)橋接名稱:fastbee_hook (隨意填寫)
請(qǐng)求方式:POST
請(qǐng)求地址:http://177.7.0.13:8080/iot/tool/mqtt/webhookv5 (地址可以是內(nèi)網(wǎng)或者外網(wǎng),確保能訪問(wèn))
請(qǐ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"
動(dòng)作:選擇創(chuàng)建的數(shù)據(jù)橋接fastbee_hook
3.設(shè)備上下線確認(rèn)
為保證設(shè)備準(zhǔn)確上下線,項(xiàng)目運(yùn)行后,可在emqx控制臺(tái)創(chuàng)建API密鑰,并且將apiKey和apiSecret添加到application.yml中,重啟Java服務(wù)



Emqx4.0 安裝配置
提示
針對(duì)2.0及之前版本,新版本不再使用emqx4.0
FastBee v2.0以及之前版本使用Emqx4.0,新版本不再使用,這里介紹下emqx4.0的對(duì)應(yīng)安裝和配置,安裝使用docker,其他安裝方式可以查看emqx官網(wǎng)。
1. docker安裝emqx4.0
docker run \
--name emqx \
--publish 1883:1883 \
--publish 8081:8081 \
--publish 8083:8083 \
--publish 8883:8883 \
--publish 8084:8084 \
--publish 18083:18083 \
--restart unless-stopped \
--detach \
emqx/emqx:v4.0.0
2. HTTP認(rèn)證
提示
配置中可以注釋超級(jí)管理員認(rèn)證和ACL相關(guān)內(nèi)容,提高速度
# a. 進(jìn)入docker容器命令
# docker exec -it emqx /bin/sh
# b. 找到etc/plugins/emqx_auth_http.conf 文件,修改http認(rèn)證請(qǐng)求地址,內(nèi)容如下:
auth.http.auth_req = https://IP或域名/prod-api/iot/tool/mqtt/auth
3.WebHook設(shè)備上下線
提示
注釋不用的鉤子事件,提高性能
## 找到 etc/plugins/emqx_web_hook.conf 文件,修改請(qǐng)求地址,內(nèi)容如下:
web.hook.api.url = https://IP或域名/prod-api/iot/tool/mqtt/webhook
4.禁用匿名認(rèn)證
# 找到etc/emqx.conf 文件,修改值為false
allow_anonymous = true