1. 搭建 MQTT Server

MQTT(Message Queuing Telemetry Transport)是一种轻量级的消息传输协议,常用于物联网设备之间的通信。以下为你介绍几种常见的搭建 MQTT Server 的方式:

1.1. 使用 EMQ X 搭建 MQTT Server

EMQ X 是一个开源的、大规模分布式 MQTT 消息服务器,支持海量物联网设备连接。

1.1.1. 步骤 1:安装 Docker

如果你选择使用 Docker 安装 EMQ X,需要先安装 Docker。以 Ubuntu 系统为例:

# 更新系统包列表
sudo apt update
# 安装必要的依赖
sudo apt install apt-transport-https ca-certificates curl software-properties-common
# 添加 Docker 的官方 GPG 密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 添加 Docker 软件源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 更新系统包列表
sudo apt update
# 安装 Docker
sudo apt install docker-ce docker-ce-cli containerd.io
# 启动 Docker 服务
sudo systemctl start docker
# 设置 Docker 开机自启
sudo systemctl enable docker

另一种安装方式 基于官方连接直接安装,Centos8操作系统

# 下载 openssl11
sudo dnf install -y https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/o/openssl11-1.1.1k-3.el8.x86_64.rpm
sudo dnf install -y https://www.emqx.com/en/downloads/broker/v4.4.19/emqx-4.4.19-otp24.3.4.2-1-el8-amd64.rpm
# start emqx
systemctl start emqx
# start emqx when reboot/power-on
systemctl enable emqx

1.1.2. 步骤 2:拉取并运行 EMQ X 容器

# 拉取 EMQ X 镜像
docker pull emqx/emqx:5.0
# 运行 EMQ X 容器
docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:5.0

-d:表示在后台运行容器。
-p:用于将容器内部的端口映射到宿主机的端口。各端口作用如下:
1883:MQTT 协议端口。
8083:MQTT over WebSocket 端口。
8084:MQTT over WebSocket/SSL 端口。
8883:MQTT/SSL 端口。
18083:EMQ X 控制台端口。

1.1.3. 步骤 3:访问 EMQ X 控制台

打开浏览器,访问 http://<服务器 IP 地址>:18083,使用默认用户名 admin 和密码 public 登录控制台。

1.2. 使用 Mosquitto 搭建 MQTT Server

Mosquitto 是一个开源的 MQTT 消息代理,非常适合小型项目和测试环境。

1.2.1. 步骤 1:安装 Mosquitto

以 Ubuntu 系统为例:

# 添加 Mosquitto 软件源
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
# 更新系统包列表
sudo apt update
# 安装 Mosquitto
sudo apt install mosquitto mosquitto-clients

以 Centos8 系统为例: 源安装参考CentOS8系统配置国内yum源

sudo yum install epel-release
sudo yum install mosquitto mosquitto-clients

1.2.2. 步骤 2:配置 Mosquitto

Mosquitto 的配置文件位于 /etc/mosquitto/mosquitto.conf。你可以根据需要进行配置,例如开启认证功能。以下是一个简单的配置示例:

1.2.2.1. 监听地址和端口

默认情况下,Mosquitto 监听 1883 端口。你可以修改配置文件来监听其他地址和端口:

# 监听的地址和端口
listener 1883
# 允许匿名连接(如果需要认证,可将其改为 false)
allow_anonymous true
bind_address 0.0.0.0
listener:指定监听的端口。
bind_address:指定监听的 IP 地址,0.0.0.0 表示监听所有可用的网络接口。

1.2.2.2. 启用认证

如果你需要启用用户认证,可以在配置文件中添加以下内容:

allow_anonymous false
password_file /etc/mosquitto/passwd
allow_anonymous:设置为 false 表示禁止匿名连接。
password_file:指定存储用户密码的文件路径。

创建密码文件并添加用户:

sudo mosquitto_passwd -c /etc/mosquitto/passwd username
-c:创建新的密码文件。如果文件已存在,会覆盖原有内容。
username:要添加的用户名,执行命令后会提示你输入密码。

1.2.2.3. 使用 SSL/TLS 加密

为了保证通信安全,你可以使用 SSL/TLS 加密。以下是基本的配置步骤:
生成证书和密钥

sudo mkdir /etc/mosquitto/certs
cd /etc/mosquitto/certs
sudo openssl req -new -x509 -days 3650 -nodes -out ca.crt -keyout ca.key
sudo openssl req -new -out server.csr -keyout server.key
sudo openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650

配置 Mosquitto

在配置文件中添加以下内容:

listener 8883
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key

listener:指定 SSL/TLS 监听的端口,通常使用 8883。
cafile:指定 CA 证书的路径。
certfile:指定服务器证书的路径。
keyfile:指定服务器私钥的路径。

1.2.2.4. 使用 SSL/TLS 同Broker进行通信

在发布和订阅消息时,需要指定 SSL/TLS 选项:

mosquitto_sub -h localhost -p 8883 --cafile /etc/mosquitto/certs/ca.crt -t test/topic
mosquitto_pub -h localhost -p 8883 --cafile /etc/mosquitto/certs/ca.crt -t test/topic -m "Hello, MQTT!"

-p:指定 SSL/TLS 监听的端口。
--cafile:指定 CA 证书的路径。

通过以上步骤,你可以全面了解和使用 Mosquitto 作为 MQTT 服务器。

1.2.3. 步骤 3:启动 Mosquitto 服务

# 启动 Mosquitto 服务
sudo systemctl start mosquitto
# 设置 Mosquitto 开机自启
sudo systemctl enable mosquitto

1.3. 测试 MQTT Server 连接

使用 mosquitto_pub 和 mosquitto_sub 工具来测试 MQTT 连接。确保你已经安装了 mosquitto-clients:

sudo dnf install -y mosquitto-clients

MQTT的windows 客户端下载MQTTX

1.3.1. 发布消息

使用 mosquitto_pub 命令发布一条消息:

mosquitto_pub -h <服务器 IP 地址> -t test/topic -m "Hello, MQTT!"

1.3.2. 订阅消息

使用 mosquitto_sub 命令订阅主题:

mosquitto_sub -h <服务器 IP 地址> -t test/topic

如果一切正常,当你发布消息时,订阅者将收到该消息。

发表评论

邮箱地址不会被公开。 必填项已用*标注