EMQ X (Erlang/Enterprise/Elastic MQTT Broker) 是基于 Erlang/OTP 平台开发的开源物联网 MQTT 消息服务器。
Erlang/OTP是出色的软实时 (Soft-Realtime)、低延时 (Low-Latency)、分布式 (Distributed)的语言平台。
MQTT 是轻量的 (Lightweight)、发布订阅模式 (PubSub) 的物联网消息协议。
EMQ X 设计目标是实现高可靠,并支持承载海量物联网终端的MQTT连接,支持在海量物联网设备间低延时消息路由:
EMQ X 的配置文件通常以 .conf 作为后缀名,你可以在 etc 目录找到这些配置文件,主要配置文件包括:
配置文件 | 说明 |
---|---|
etc/emqx.conf | EMQ X 配置文件 |
etc/acl.conf | EMQ X 默认 ACL 规则配置文件 |
etc/plugins/*.conf | EMQ X 扩展插件配置文件 |
EMQ X集群默认占用的Tcp端口如下:
端口 | 说明 |
---|---|
1883 | MQTT/TCP 协议端口 |
11883 | MQTT/TCP 协议内部端口,仅用于本机客户端连接 |
8883 | MQTT/SSL 协议端口 |
8083 | MQTT/WS 协议端口 |
8084 | MQTT/WSS 协议端口 |
8081 | 管理 API 端口 |
18083 | Dashboard 端口 |
4369 | 集群节点发现端口 (EPMD 模式) |
4370 | 集群节点发现端口 |
5370 | 集群节点 PRC 通道 |
node-02 IP: 192.168.1.202
node-03 IP: 192.168.1.203
首先配置好node-02和node-03两台单节点上的emqx,之后,依次启动;node-03执行集群加入命令,加入到node-02上集群;由于阿里云不提供VIP,最后采用阿里云提供负载均衡提供一个集群统一入口。
安装包下载地址:emqx安装包下载地址
# 下载安装包并解压
[root@node-02 opt]# wget https://www.emqx.com/en/downloads/broker/v4.0.0/emqx-centos7-v4.0.0.zip
[root@node-02 opt]# unzip emqx-centos7-v4.0.0.zip
# 修改配置文件
[root@node-02 emqx]# vim etc/emqx.conf
....
cluster.name = emqxcl # 集群名称
node.name = [email protected] # 修改为本机主机名和IP
node.data_dir = data # 数据存储目录
...
# 启动
[root@node-02 bin]# ./emqx start
EMQ X Broker v4.0.0 is started successfully!
# 查看节点状态
[root@node-02 bin]# ./emqx_ctl status
Node '[email protected]' is started
emqx 4.0.0 is running
node-03按照node-02配置进行修改,注意修改相应主机名和IP。
[root@node-03 bin]# ./emqx start
EMQ X Broker v4.0.0 is started successfully!
# 查看节点状态
[root@node-03 bin]# ./emqx_ctl status
Node '[email protected]' is started
emqx 4.0.0 is running
node-03执行集群加入命令:
[root@node-03 bin]# ./emqx_ctl cluster join [email protected]
=CRITICAL REPORT==== 16-Sep-2021::11:03:51.502636 ===
[EMQ X] emqx shutdown for join
Join the cluster successfully.
Cluster status: #{running_nodes =>
['[email protected]','[email protected]'],
stopped_nodes => []}
# node-03上查看集群状态
[root@node-03 bin]# ./emqx_ctl cluster status
Cluster status: #{running_nodes =>
['[email protected]','[email protected]'],
stopped_nodes => []}
# node-02上查看集群状态
[root@node-02 bin]# ./emqx_ctl cluster status
Cluster status: #{running_nodes =>
['[email protected]','[email protected]'],
stopped_nodes => []}
至此,云环境本机emqx集群部署完成,利用阿里云提供的负载均衡配置一个集群统一入口即可。
首先利用docker运行一个单节点emqx,将其中的配置文件复制出来
[root@node-02 ~]# docker run -d --name emqx --rm emqx/emqx:v4.0.0
# 复制出配置文件
[root@node-02 ~]# docker cp emqx:/opt/emqx/etc .
# 删除当前容器
[root@node-02 ~]# docker stop emqx
# 将配置文件复制到指定目录,为下一步运行挂载作准备
[root@node-02 ~]# mkdir -p /data/emqx
[root@node-02 ~]# mv etc /data/emqx
修改配置文件
[root@node-02 etc]# vim emqx.conf
...
cluster.name = emqxcl
cluster.discovery = static
cluster.static.seeds = [email protected],[email protected]
node.name = [email protected]
...
# 修改下当前目前权限
[root@node-02 ~]# chown -R 1000:1000 /data/emqx/
运行docker容器
[root@node-02 ~]# docker run -d --name emqx --network host -v /data/emqx/etc:/opt/emqx/etc -v /data/emqx/data:/data/emqx/data emqx/emqx:v4.0.0
node-03按照node-02配置进行修改,启动;查看集群状态
# node-02查看集群状态
[root@node-02 etc]# docker exec -it emqx /bin/sh /opt/emqx/bin/emqx_ctl cluster status
Cluster status: #{running_nodes =>
['[email protected]','[email protected]'],
stopped_nodes => []}
创建 docker-compose.yaml 文件
version: '3'
services:
emqx1:
image: emqx/emqx:v4.0.0
environment:
- "EMQX_NAME=emqx"
- "EMQX_HOST=node1.emqx.io"
- "EMQX_CLUSTER__DISCOVERY=static"
- "[email protected], [email protected]"
healthcheck:
test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
interval: 5s
timeout: 25s
retries: 5
networks:
emqx-bridge:
aliases:
- node1.emqx.io
emqx2:
image: emqx/emqx:v4.0.0
environment:
- "EMQX_NAME=emqx"
- "EMQX_HOST=node2.emqx.io"
- "EMQX_CLUSTER__DISCOVERY=static"
- "[email protected], [email protected]"
healthcheck:
test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
interval: 5s
timeout: 25s
retries: 5
networks:
emqx-bridge:
aliases:
- node2.emqx.io
networks:
emqx-bridge:
driver: bridge
2.启动 docker-compose 集群
$ docker-compose -p my_emqx up -d
3.查看集群
$ docker exec -it my_emqx_emqx1_1 sh -c "emqx_ctl cluster status"
Cluster status: #{running_nodes => ['[email protected]','[email protected]'],
stopped_nodes => []}
登录web查看emqx集群状态,登录地址为:http://node_IP:18083
初始账号密码为:admin/public