EMQX Broker 一开始的定位就是物联网消息中间件,目前开源版本功能已经非常强大,而企业版本与Cloud版本更是提供了高阶功能,全托管、更稳定、更可靠,技术支持更及时。开源版的 EMQX Broker 除了全面支持 MQTT5 新特性、多协议支持外,更强大的地方在于其围绕 MQTT 周边提供了一系列的 WebHook 、 HTTP API 接口以及最为核心的规则引擎。
这篇文章先完成 EMQX 的安装、运行以及简单体验,在接下来的实践项目中仅用到了其设备接入、 WebHook 功能。
系统环境为了方便管理,我们的服务器仅用到一台虚拟主机,关于虚拟机如何安装这里就不做演示了,后续的单实例 TDengine 、 TDengine-alert 也安装到当前主机,操作系统为 CentOS7 ,主机名为 iot1 。详细的系统及配置信息如下:
- 系统信息
[root@iot1 local]# uname -a Linux iot1 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux [root@iot1 local]# cat /proc/version Linux version 3.10.0-1127.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Mar 31 23:36:51 UTC 2020 [root@iot1 local]# cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core)
- 配置信息
内存:4G 处理器:2*2 硬盘:100G
Note: 如何设置、修改主机名?假设我之前的主机名为 hadoop9 ,现在我想修改主机名为 iot1 。
# 设置主机名(或者:vi /etc/hostname iot1) [root@hadoop9 ~]# hostnamectl set-hostname iot1 # 验证设置的主机名 [root@hadoop9 ~]# hostname # 编辑hosts文件,将IP与主机名关联 [root@hadoop9 ~]# vi /etc/hosts # 重启(其实,关闭当前连接,重新连接即可生效) [root@hadoop9 ~]# reboot下载安装
一开始的项目使用的是 EMQX4.2.4 ,现在按照当时的下载地址已经访问不到了;建议使用新版本 4.4.2 ,不过两个版本在当前项目中的使用并没有什么区别。
- 4.2.4
下载地址:https://www.emqx.io/downloads/broker/?osType=Linux 已失效404
# 解压 [root@iot1 local]# unzip emqx-centos7-4.2.4-x86_64.zip [root@iot1 local]# cd emqx # 运行EMQX服务 [root@iot1 emqx]# ./bin/emqx start EMQ X Broker 4.2.4 is started successfully! # 查看状态 [root@iot1 emqx]# ./bin/emqx_ctl status Node 'emqx@127.0.0.1' is started emqx 4.2.4 is running # 停止EMQX服务 [root@iot1 emqx]# ./bin/emqx stop ok # 重启EMQX服务 [root@iot1 emqx]# ./bin/emqx restart Node 'emqx@127.0.0.1' not responding to pings. EMQ X Broker 4.2.4 is stopped: EMQ X Broker 4.2.4 is started successfully!
- 4.4.2
新版地址:https://www.emqx.com/en/downloads/broker/4.4.2/emqx-4.4.2-otp24.1.5-3-el7-amd64.zip
4.4.2 换了 Logo 。。
# 下载、解压 [root@iot1 local]# wget https://www.emqx.com/en/downloads/broker/4.4.2/emqx-4.4.2-otp24.1.5-3-el7-amd64.zip [root@iot1 local]# unzip emqx-4.4.2-otp24.1.5-3-el7-amd64.zip [root@iot1 local]# cd emqx # 运行EMQX服务 [root@iot1 emqx]# ./bin/emqx start There seem to be missing dynamic libs from the OS. Using libs from /usr/local/emqx/dynlibs instead. NOTE: EMQX's rpm or deb package installation is recommended! EMQ X Broker 4.4.2 is started successfully! [root@iot1 emqx]# ./bin/emqx_ctl status Node 'emqx@127.0.0.1' 4.4.2 is started # 停止EMQX服务 [root@iot1 emqx]# ./bin/emqx stop There seem to be missing dynamic libs from the OS. Using libs from /usr/local/emqx/dynlibs instead. NOTE: EMQX's rpm or deb package installation is recommended! ok # 重启EMQX服务 [root@iot1 emqx]# ./bin/emqx restart There seem to be missing dynamic libs from the OS. Using libs from /usr/local/emqx/dynlibs instead. NOTE: EMQX's rpm or deb package installation is recommended! Node 'emqx@127.0.0.1' not responding to pings. EMQ X Broker 4.4.2 is stopped: EMQ X Broker 4.4.2 is started successfully!查看日志
如果在实际开发、运维过程中怀疑服务有问题,那么通过查看 EMQX 日志的方式进行排查。
[root@iot1 emqx]# tailf ./log/emqx.log.1 2022-04-10T22:09:59.256537-04:00 [warning] [Dashboard] The initial default password for dashboard 'admin' user in emqx_dashboard.conf, For safety, it should be changed as soon as possible., Please use the './bin/emqx_ctl admins' CLI to change it., Then remove `dashboard.default_user.login/password` from emqx_dashboard.conf开放端口
后续我们需要通过宿主机访问虚拟机中的服务,那么在远程访问时需要放行以下 TCP 端口:
- 18083:Dashboard端口
- 8083:WebSocket端口
- 1883:MQTT端口
[root@iot1 emqx]# iptables -I INPUT -p TCP --dport 18083 -j ACCEPT [root@iot1 emqx]# iptables -I INPUT -p TCP --dport 8083 -j ACCEPT [root@iot1 emqx]# iptables -I INPUT -p TCP --dport 1883 -j ACCEPT
或者直接关闭防火墙,在虚拟机内网测试环境下没关系,在生产环境就不合适了,安全意识一定要有。
# 关闭防火墙 [root@iot1 emqx]# systemctl stop firewalld.service
另外,如果想通过域名访问虚拟机中的服务,需在宿主机上添加以下内容(格式:IP 空格 主机名)至 Windows 操作系统的 hosts 文件,路径: C:WindowsSystem32driversetchosts 。之后就可以使用主机名/域名代替IP来访问虚拟机上的服务了,eg: http://iot1:18083
192.168.44.138 iot1Dashboard
远程访问 Dashboard 控制台,并进行客户端连接测试。 Dashboard 地址: http://iot1:18083 ,默认用户名:admin,默认密码:public。
- 登录
- 旧版首页
- 新版首页
- WebSocket客户端
- WebSocket客户端订阅
- MQTTBox客户端工具模拟一个MQTT客户端
- WebSocket客户端发布
- WebSocket客户端接收
- MQTT客户端发布
- Dashboard上的两个客户端
至此,我们已经在一台虚拟主机上安装好 EMQX 环境,下一篇就要安装体验我们这次项目的主角了: TDengine 。当前 EMQX 已发展成为专业、强大的物联网设备接入代理,其功能点之多用一本书也不一定写得完。后续专门开个专栏介绍下 EMQX 在我们实际生产项目中使用到的核心功能,包括但不限于以下方面:
- 设备离线、在线监测
- EMQX主动断开设备连接
- 设备接入认证
- ACL订阅、发布鉴权
- 系统主题
- 代理订阅
- 设备状态信息上报
- 远程控制命令下发
- 插件使用
- 规则引擎
- https://www.emqx.io/docs/zh/v4.4/
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!



