安装EMQX
约 911 字大约 3 分钟
安装单机
下载解压
官网: https://www.emqx.io/downloads#broker
创建mqtt文件夹存放zip包:
wget https://www.emqx.com/en/downloads/broker/5.0.23/emqx-5.0.23-el7-amd64.tar.gz
mkdir -p /usr/local/emqx-5.0.23
tar -zxvf emqx-5.0.23-el7-amd64.tar.gz -C /usr/local/emqx-5.0.23/
配置环境变量
[root@cdh1 ~]# vim /etc/profile
export EMQX_HOME=/usr/local/emqx-5.0.23
export PATH=$PATH:$EMQX_HOME/bin
[root@cdh1 ~]# source /etc/profile
[root@cdh1 ~]#
修改配置
vim /usr/local/emqx-5.0.23/etc/emqx.conf
占用端口
| 端口 | 用途 |
|---|---|
| 1883 | MQTT 协议端口 |
| 8883 | MQTT/SSL 端口 |
| 8083 | MQTT/WebSocket 端口 |
| 8080 | HTTP API 端口 |
| 18083 | Dashboard 管理控制台端口 |
启动服务
# 启动emqx
emqx start
# 停止emqx
emqx stop
# 重启emqx
emqx restart
# 检查运行状态
emqx_ctl status
# 服务卸载
emqx uninstall
设置账号密码
emqx_ctl users add 账号 密码
比如创建用户名为 ‘admin’,密码为‘password’,默认密码为【public】:
emqx_ctl users add admin password
4、访问Dashboard页面
页面地址:http://127.0.0.1:18083/#/ 输入账号名密码后如下图所示:
安装集群
EMQX 集群配置 的搭建有几种方式,现在我们是所说是有两种方式,一种是静态方式,一种是动态方式。
| 序号 | 域名 | 服务器IP | 端口 | emqx版本 |
|---|---|---|---|---|
| 1 | cdh1 | 172.18.11.111 | 1883 | 5.0.23 |
| 2 | cdh2 | 172.18.11.112 | 1883 | 5.0.23 |
| 3 | cdh3 | 172.18.11.113 | 1883 | 5.0.23 |
static方式
修改 /usr/local/emqx-5.0.23/etc/emqx.conf 文件
在 cdh1 节点上配置
# 设置节点名称,请注意,节点标识必须和上面节点发现列表中的一致,否则将无法实现集群节点发现
node {
name = "emqx@172.18.11.111"
cookie = "emqxsecretcookie"
data_dir = "data"
}
cluster {
name = emqxcl
# 修改集群模式为 static
discovery_strategy = static
static {
# 设置节点发现列表
seeds="emqx@172.18.11.111,emqx@172.18.11.112,emqx@172.18.11.113"
}
}
更新各个节点配置
# 在cdh2节点更新配置文件绑定地址
[root@cdh2 ~]# scp root@cdh1:/usr/local/emqx-5.0.23/etc/emqx.conf /usr/local/emqx-5.0.23/etc/emqx.conf
[root@cdh2 ~]# grep -rl 'name' /usr/local/emqx-5.0.23/etc/emqx.conf | xargs sed -i 's#name = "emqx@172.18.11.111"#name = "emqx@172.18.11.112"#g'
# 在cdh3节点更新配置文件绑定地址
[root@cdh3 ~]# scp root@cdh1:/usr/local/emqx-5.0.23/etc/emqx.conf /usr/local/emqx-5.0.23/etc/emqx.conf
[root@cdh3 ~]# grep -rl 'name' /usr/local/emqx-5.0.23/etc/emqx.conf | xargs sed -i 's#name = "emqx@172.18.11.111"#name = "emqx@172.18.11.113"#g'
# 验证更新成功
grep -rn 'name = "emqx' /usr/local/emqx-5.0.23/etc
启动集群中的每个节点
[root@cdh1 ~]# emqx start
[root@cdh2 ~]# emqx start
[root@cdh3 ~]# emqx start
manual方式
在 cdh1 节点上配置
# 设置节点名称,请注意,节点标识必须和上面节点发现列表中的一致,否则将无法实现集群节点发现
node {
name = "emqx@172.18.11.111"
cookie = "emqxsecretcookie"
data_dir = "data"
}
cluster {
name = emqxcl
# 修改集群模式为 manual
discovery_strategy = manual
}
更新各个节点配置
# 在cdh2节点更新配置文件绑定地址
[root@cdh2 ~]# scp root@cdh1:/usr/local/emqx-5.0.23/etc/emqx.conf /usr/local/emqx-5.0.23/etc/emqx.conf
[root@cdh2 ~]# grep -rl 'name' /usr/local/emqx-5.0.23/etc/emqx.conf | xargs sed -i 's#name = "emqx@172.18.11.111"#name = "emqx@172.18.11.112"#g'
# 在cdh3节点更新配置文件绑定地址
[root@cdh3 ~]# scp root@cdh1:/usr/local/emqx-5.0.23/etc/emqx.conf /usr/local/emqx-5.0.23/etc/emqx.conf
[root@cdh3 ~]# grep -rl 'name' /usr/local/emqx-5.0.23/etc/emqx.conf | xargs sed -i 's#name = "emqx@172.18.11.111"#name = "emqx@172.18.11.113"#g'
# 验证更新成功
grep -rn 'name = "emqx' /usr/local/emqx-5.0.23/etc
启动集群中的每个节点
[root@cdh1 ~]# emqx start
[root@cdh2 ~]# emqx start
[root@cdh3 ~]# emqx start
将 cdh2、cdh3 加入cdh1集群
emqx_ctl cluster join emqx@192.168.31.159
emqx_ctl cluster status
# 执行加入命令
[root@cdh2 ~]# emqx_ctl cluster join emqx@172.18.11.111
Join the cluster successfully.
Cluster status: #{running_nodes => ['emqx@172.18.11.111','emqx@172.18.11.112'],
stopped_nodes => []}
# 查看状态
[root@cdh2 ~]# emqx_ctl cluster status
[root@cdh3 ~]# emqx_ctl cluster join emqx@172.18.11.111
Join the cluster successfully.
Cluster status: #{running_nodes =>
['emqx@172.18.11.111','emqx@172.18.11.112',
'emqx@172.18.11.113'],
stopped_nodes => []}
[root@cdh3 ~]# emqx_ctl cluster status
通过管理页面查看集群
http://172.18.11.111:18083/
Nginx代理
stream {
# 轮询负载均衡配置
upstream emqx_cluster {
# nginx 的三个实例
server 172.18.11.111:1883 max_fails=2 fail_timeout=30s;
server 172.18.11.112:1883 max_fails=2 fail_timeout=30s;
server 172.18.11.113:1883 max_fails=2 fail_timeout=30s;
}
server {
# 监听 8884 端口 SSL
listen 8884 ssl;
# 反向代理到 emqx_cluster
proxy_pass emqx_cluster;
proxy_buffer_size 4k;
ssl_handshake_timeout 15s;
# 证书配置
ssl_certificate /etc/nginx/cert/nginx.pem;
ssl_certificate_key /etc/nginx/cert/nginx.key;
}
}
参考: