moquitto部署验证发布订阅模式

1.基础准备

安装基础软件

yum install gcc-c++
yum install cmake
yum install openssl-devel //mosquitto默认支持openssl

2.下载程序

wget http://mosquitto.org/files/source/mosquitto-1.4.4.tar.gz
tar -xzvf mosquitto-1.4.4.tar.gz
cd mosquitto-1.4.4
3.编译安装

vim config.mk

参考如下:

4.安装c-areas

wget http://c-ares.haxx.se/download/c-ares-1.10.0.tar.gz
tar xvf c-ares-1.10.0.tar.gz
cd c-ares-1.10.0
./configure
make
sudo make install

5.安装lib-uuid

yum install libuuid-devel

6.安装libwebsockets

wget https://github.com/warmcat/libwebsockets/archive/v1.3-chrome37-firefox30.tar.gz
tar zxvf v1.3-chrome37-firefox30.tar.gz
cd libwebsockets-1.3-chrome37-firefox30
mkdir build; cd build;
cmake .. -DLIB_SUFFIX=64
make install


7.开始安装mosquitto

注意需要在解压目录下进行安装

make
make install
8.程序配置

mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

配置项增加程序启动路径,其他部分未动,//关于详细配置可参考
http://mosquitto.org/man/mosquitto-conf-5.html

#pid_file /var/run/mosquitto.pid
 

9.启动程序

mosquitto -c /etc/mosquitto/mosquitto.conf -d

启动时出现如下错误:

解决方法:需要添加mosquitto用户,adduser -g mosquitto mosquitto

10.客户端测试

开两个shell窗口

cd /usr/local/bin

订阅者:mosquitto_sub -t location

发布者:mosquitto_pub -t location -h localhost -m "new location"

测试时出现错误:error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

解决方法:修正链接库路径

//添加路径

vim /etc/ld.so.conf.d/liblocal.conf
/usr/local/lib64
/usr/local/lib
  •  

//刷新
ldconfig

正常后可以在A窗口看到由B推送的消息,此外服务端窗口也可以看到客户端连接和端口的日志

参考1:https://blog.youkuaiyun.com/black_little_/article/details/88555822

参考2:https://www.cnblogs.com/Free-Thinker/p/5559816.html

你提到的 “mbqq” 很可能是 **MQTT** 的误写(拼音输入法中 “mqtt” 和 “mbqq” 键位相近)。 如果你是想用 Python 搭建一个 **MQTT 服务器(Broker)或客户端**,那么这是完全可行的。 但如果是其他含义(如自定义协议、内部系统代号),请补充说明。 --- ## ✅ 假设你想实现:用 Python 搭建 MQTT 服务(支持发布/订阅消息) ### 📌 什么是 MQTT? MQTT(Message Queuing Telemetry Transport)是一种轻量级的发布/订阅式消息传输协议,常用于 IoT 设备通信、实时通知等场景。 Python 中常用的库: - `paho-mqtt`:最流行的 MQTT 客户端库 - `hbmqtt` / `vernemq` / `moquitto`:可运行 MQTT Broker(服务器) --- ## ✅ 方案一:使用 `paho-mqtt` 搭建 MQTT 客户端(推荐) ### 1. 安装依赖 ```bash pip install paho-mqtt ``` --- ### 2. 启动本地 MQTT Broker(以 Mosquitto 为例) 因为 Python 不直接提供“一键启动 Broker”的功能,你需要先安装一个 MQTT 服务器。推荐使用开源的 [Mosquitto](https://mosquitto.org/): #### macOS: ```bash brew install mosquitto mosquitto -v # 启动 broker,默认端口 1883 ``` #### Ubuntu: ```bash sudo apt-get install mosquitto mosquitto-clients sudo systemctl start mosquitto ``` #### Windows: 下载地址:[https://mosquitto.org/download/](https://mosquitto.org/download/) 解压后运行: ```cmd mosquitto.exe -v ``` > 或者使用 Docker 快速启动: > > ```bash > docker run -it -p 1883:1883 eclipse-mosquitto > ``` --- ### 3. Python 编写 MQTT 发布者(Publisher) ```python import paho.mqtt.client as mqtt import time # 配置 BROKER = "localhost" PORT = 1883 TOPIC = "test/topic" CLIENT_ID = "publisher_client" # 创建客户端 client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, CLIENT_ID) def on_connect(client, userdata, flags, rc, properties=None): if rc == 0: print("✅ Publisher 连接到 MQTT Broker") else: print(f"❌ 连接失败,返回码 {rc}") client.on_connect = on_connect # 连接 Broker client.connect(BROKER, PORT) client.loop_start() # 启动网络循环 # 发送消息 try: for i in range(10): message = f"Hello from Python Publisher #{i+1}" result = client.publish(TOPIC, message, qos=1) print(f"📤 已发送: {message}") time.sleep(2) finally: client.loop_stop() client.disconnect() ``` --- ### 4. Python 编写 MQTT 订阅者(Subscriber) ```python import paho.mqtt.client as mqtt # 配置 BROKER = "localhost" PORT = 1883 TOPIC = "test/topic" CLIENT_ID = "subscriber_client" def on_connect(client, userdata, flags, rc, properties=None): if rc == 0: print("✅ Subscriber 成功连接到 Broker") client.subscribe(TOPIC) else: print(f"❌ 连接失败,返回码 {rc}") def on_message(client, userdata, msg): print(f"📥 收到消息 [{msg.topic}]: {msg.payload.decode('utf-8')}") # 创建客户端 client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, CLIENT_ID) client.on_connect = on_connect client.on_message = on_message # 连接并开始监听 client.connect(BROKER, PORT) client.loop_forever() # 持续监听 ``` --- ## ✅ 方案二:纯 Python 实现 MQTT Broker(使用 `hbmqtt`) 如果你想完全用 Python 写一个 MQTT 服务器,可以使用 `hbmqtt`: ### 1. 安装 hbmqtt ```bash pip install hbmqtt ``` ### 2. 启动 MQTT Broker(Python 实现) ```python from hbmqtt.broker import Broker config = { 'listeners': { 'default': { 'type': 'tcp', 'bind': 'localhost:1883' } }, 'topic-check': { 'enabled': True, 'plugins': ['topic_taboo'] } } broker = Broker(config) async def start_broker(): await broker.start() if __name__ == '__main__': import asyncio asyncio.run(start_broker()) ``` > ⚠️ 注意:`hbmqtt` 对 QoS 支持有限,生产环境建议使用 Mosquitto。 --- ## ✅ 测试是否成功? 打开终端测试: ```bash # 订阅主题 mosquitto_sub -h localhost -t "test/topic" # 发布消息 mosquitto_pub -h localhost -t "test/topic" -m "Hi from CLI" ``` 你的 Python 程序应该能收到这些消息! --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值