4

linux 下mqtt客户端工具mosquitto安装与使用

 2 years ago
source link: https://www.ishells.cn/archives/emqx-client-mosquitto
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
linux 下mqtt客户端工具mosquitto安装与使用

linux 下mqtt客户端工具mosquitto安装与使用

14 次访问 2021-11-25
枕头下臭袜子
Scroll Down

我司的emqx使用kubernetes部署,开启了NodePort供外部IOT设备连接使用,有时候emqx的pod可能会假死导致IOT设备无法连接到emqx server端。所以就有了以下这么几个前提条件:

a、emqx服务端使用kubernetes部署,多Pod,emqx为集群模式

b、外部设备通过 NodePort 连接,kubernetes的 NodePort 模式会监听所有集群节点的该端口

c、emqx的pod没有开启liveness存活检测,实测不是特别好使

d、调试的时候可以通过emqx的客户端程序进行订阅发布来检测emqx是否正常工作

但是出现IOT设备连接不上emqx服务端的时候,通过个人电脑本地的emqx客户端只能发现emqx是不是一直在回复你发布的订阅消息(因为不管使用kubernetes集群节点 + nodeport 还是集群 域名 + nodeport 都会连接到所有的emqx pod副本)

如果是在集群之中使用emqx pod 的ip 进行连接测试的话,就能很快定位到具体的emqx pod并进行相关处理

基于以上,linux主机的emqx客户端就很有必要了。

linux emqx客户端介绍:

Mosquitto 是一个开源(EPL/EDL 许可证)的消息代理,Mosquitto 安装之后默认提供了 mosquitto_pub 和 mosquitto_sub 两个命令行 MQTT 客户端工具。

Mosquitto CLI 有多个配置选项,支持 TLS 证书连接、通过代理服务器连接,支持 debug 模式,在 debug 模式下可以获取更详细的消息信息。

它的使用也非常简便,默认使用环境只需提供少许参数即可使用

客户端支持多操作系统:Windows、macOs、Linux

Mosquitto github 项目地址

Mosquitto 官网地址

linux下安装方式有多种,可以基于snap、yum、apt-get进行安装

a、snap方式(centos)

sudo yum install epel-releasesudo yum install snapdsudo systemctl enable --now snapd.socketsudo ln -s /var/lib/snapd/snap /snapsudo snap install mosquitto

b、yum方式

yum install mosquitto

c、apt-get方式

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppasudo apt-get update
订阅(收):
# 命令格式:mosquitto_sub -h emqx的pod_ip -p 端口号 -t "订阅名称" -u "用户名" -P "密码"# 例:mosquitto_sub -h 10.244.44.251 -p 1883 -t demo -u "server" -P "xxxxxxxxxx" # 参数解释:mosquitto_sub  -c 设定‘clean session’为无效状态,这样一直保持订阅状态,即便是已经失去连接,如果再次连接仍旧能够接收的断开期间发送的消息。-d 打印debug信息-h 指定要连接的域名 默认为localhost-p 端口-i 指定clientId-I 指定clientId前缀-k keepalive 每隔一段时间,发PING消息通知broker,仍处于连接状态。 默认为60秒。-q 指定希望接收到QoS为什么的消息 默认QoS为0-R 不显示陈旧的消息-t 订阅topic-v 打印消息-u 用户名-P 密码--will-payload 指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与--will-topic一起使用--will-qos Will的QoS值。该参数需要与--will-topic一起使用--will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与--will-topic一起使用--will-topic 用户发送Will消息的topic
发布(发):
# 命令格式:mosquitto_pub -h emqx的pod_ip  -p 端口号 -t "订阅名称" -m "订阅消息"# 例:mosquitto_pub -h 10.244.44.251 -p 1883 -t "demo" -m "111" # 参数解释:mosquitto_pubd 打印debug信息-f 将指定文件的内容作为发送消息的内容-h 指定要连接的域名 默认为localhost-i 指定要给哪个clientId的用户发送消息-I 指定给哪个clientId前缀的用户发送消息-m 消息内容-n 发送一个空(null)消息-p 连接端口号-q 指定QoS的值(0,1,2)-t 指定topic-u 指定broker访问用户-P 指定broker访问密码-V 指定MQTT协议版本--will-payload 指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与--will-topic一起使用--will-qos Will的QoS值。该参数需要与--will-topic一起使用--will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与--will-topic一起使用--will-topic 用户发送Will消息的topic

mosquitto_pub.png

mosquitto_sub.png

参考:
1、常见的emqx客户端介绍

MQTT X、Mosquitto CLI、MQTT.fx、MQTT Explorer、MQTT Box、mqtt-spy、MQTT Lens、MQTT WebSocket Toolkit

2、Linux mosquitto(mqtt)使用


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK