天天看點

CentOS7.9安裝mqtt消息中間件mosquitto

MQTT(MQ Telemetry Transport),消息隊列遙測傳輸協定,輕量級的釋出/訂閱協定, 适用于一些條件比較苛刻的環境,進行低帶寬、不可靠或間歇性的通信。目前已經是物聯網消息通信事實上的标準協定了。值得一提的是mqtt提供三種不同品質的消息服務。

1).“至多一次”:消息釋出完全依賴底層 TCP/IP 網絡。會發生消息丢失或重複。這一級别可用于如下情況,環境傳感器資料,丢失一次讀記錄無所謂,因為不久後還會有第二次發送。
2).“至少一次”:確定消息到達,但消息重複可能會發生。
3).“隻有一次”:確定消息到達一次。這一級别可用于如下情況,在計費系統中,消息重複或丢失會導緻不正确的結果。      

基于MQTT(但不僅限于)開源消息代理中間件(Brokers/servers),又有測試用戶端,看了幾個代理中間件,也百度了一下,應用比較多的有ActiveMQ、Apollo、Mosquitto等。先選擇一個沒那麼複雜的Mosquitto來嘗嘗鮮。Mosquitto是一款實作了消息推送協定 MQTT v3.1 的開源消息代理軟體,提供輕量級的,支援可釋出/可訂閱的的消息推送模式,使裝置對裝置之間的短消息通信變得簡單,比如現在應用廣泛的低功耗傳感器,手機、嵌入式計算機、微型控制器等移動裝置。

安裝:(參考官網 ​​http://mosquitto.org/download/​​)

1.安裝

(1).添加EPEL軟體庫
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm      

檢視安裝結果

[root@localhost ~]# yum repolist all | grep enabled
base/7/x86_64                 CentOS-7 - Base                    enabled: 10,070
epel/x86_64                   Extra Packages for Enterprise Linu enabled: 13,426
extras/7/x86_64               CentOS-7 - Extras                  enabled:    413
updates/7/x86_64              CentOS-7 - Updates                 enabled:  1,112
[root@localhost ~]#       
(2).安裝 mosquitto

查找 mosquitto 軟體包

yum list all | grep mosquitto      

檢視 mosquitto 資訊

[root@localhost ~]# yum info mosquitto
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: hkg.mirror.rackspace.com
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.aliyun.com
Installed Packages
Name        : mosquitto
Arch        : x86_64
Version     : 1.6.10
Release     : 1.el7
Size        : 748 k
Repo        : installed
From repo   : epel
Summary     : An Open Source MQTT v3.1/v3.1.1 Broker
URL         : http://mosquitto.org/
License     : BSD
Description : Mosquitto is an open source message broker that implements the MQ Telemetry
            : Transport protocol version 3.1 and 3.1.1 MQTT provides a lightweight method
            : of carrying out messaging using a publish/subscribe model. This makes it
            : suitable for "machine to machine" messaging such as with low power sensors
            : or mobile devices such as phones, embedded computers or micro-controllers
            : like the Arduino.

[root@localhost ~]#       
yum install mosquitto      
[root@localhost ~]# yum list installed | grep mosquitto
mosquitto.x86_64                     1.6.10-1.el7                   @epel       
[root@localhost ~]#       

2.測試 mosquitto

(1).啟動 mosquitto 主程式 會話1
[root@localhost ~]# mosquitto
1597721219: mosquitto version 1.6.10 starting
1597721219: Using default config.
1597721219: Opening ipv4 listen socket on port 1883.
1597721219: Opening ipv6 listen socket on port 1883.      
(2).啟動訂閱 會話2
mosquitto_sub -t rimelink      
(3).啟動釋出 會話3
mosquitto_pub -t rimelink -h localhost -m "hello,zhangsan"
mosquitto_pub -t rimelink -h localhost -m "hi,lisi"      
(4).檢視結果 會話2
[root@localhost ~]# mosquitto_sub -t rimelink
hello,zhangsan
hi,lisi      

繼續閱讀