CentOS 7 下安裝mosquitto


簡介

MQTT(Message Queuing Telemetry Transport,消息隊列遙測傳輸)是IBM開發的一個即時通訊協議,有可能成為物聯網的重要組成部分。該協議支持所有平台,幾乎可以把所有聯網物品和外部連接起來,被用來當做傳感器和制動器(比如通過Twitter讓房屋聯網)的通信協議。

特點

MQTT協議是為大量計算能力有限,且工作在低帶寬、不可靠的網絡的遠程傳感器和控制設備通訊而設計的協議,它具有以下主要的幾項特性:
1、使用發布/訂閱消息模式,提供一對多的消息發布,解除應用程序耦合;
2、對負載內容屏蔽的消息傳輸;
3、使用 TCP/IP 提供網絡連接;
4、有三種消息發布服務質量:
“至多一次”,消息發布完全依賴底層 TCP/IP 網絡。會發生消息丟失或重復。這一級別可用於如下情況,環境傳感器數據,丟失一次讀記錄無所謂,因為不久后還會有第二次發送。
“至少一次”,確保消息到達,但消息重復可能會發生。
“只有一次”,確保消息到達一次。這一級別可用於如下情況,在計費系統中,消息重復或丟失會導致不正確的結果。
5、小型傳輸,開銷很小(固定長度的頭部是 2 字節),協議交換最小化,以降低網絡流量;
6、使用 Last Will 和 Testament 特性通知有關各方客戶端異常中斷的機制;

安裝與配置

准備

/etc/yum.repos.d/目錄中新建一個mosquitto.repo文件

[home_oojah_mqtt]

name=mqtt (CentOS_CentOS-7)

type=rpm-md

baseurl=http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/

gpgcheck=1

gpgkey=http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7//repodata/repomd.xml.key

enabled=1

執行 yum search all mosquitto

[root@localhost ~]# yum search all mosquitto
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirrors.ustc.edu.cn
============================================================================================================= 匹配:mosquitto ==============================================================================================================
mosquitto-clients.x86_64 : Mosquitto command line publish/subscribe clients
mosquitto-debuginfo.x86_64 : Debug information for package mosquitto
mosquitto-devel.x86_64 : Development files for mosquitto
mosquitto.x86_64 : An Open Source MQTT v3.1/v3.1.1 Broker
libmosquitto-devel.x86_64 : MQTT C client library development files
libmosquitto1.x86_64 : MQTT C client library
libmosquittopp-devel.x86_64 : MQTT C++ client library development files
libmosquittopp1.x86_64 : MQTT C++ client library

安裝mosquitto客戶端

執行 yum install -y mosquitto-clients.x86_64

[root@localhost ~]# yum install -y mosquitto-clients.x86_64 

安裝mosquitto服務

執行命令 yum install mosquitto.x86_64

[root@localhost ~]# yum -y install mosquitto.x86_64 

修改mosquitto.conf文件

文件在/etc/mosquitto/mosquitto.conf
下面是可以選擇的參數 在 /etc/mosquitto/mosquitto.conf.example

# =================================================================
  # General configuration
  # =================================================================
  # 客戶端心跳的間隔時間
  #retry_interval 20
  # 系統狀態的刷新時間
  #sys_interval 10
  # 系統資源的回收時間,0表示盡快處理
  #store_clean_interval 10
  # 服務進程的PID
 #pid_file /var/run/mosquitto.pid
 # 服務進程的系統用戶
 #user mosquitto
 # 客戶端心跳消息的最大並發數
 #max_inflight_messages 10
 # 客戶端心跳消息緩存隊列
 #max_queued_messages 100
 # 用於設置客戶端長連接的過期時間,默認永不過期
 #persistent_client_expiration
# =================================================================
# Default listener
# =================================================================
# 服務綁定的IP地址
#bind_address
# 服務綁定的端口號
#port 1883
# 允許的最大連接數,-1表示沒有限制
#max_connections -1
# cafile:CA證書文件
# capath:CA證書目錄
# certfile:PEM證書文件
# keyfile:PEM密鑰文件
#cafile
#capath
#certfile
#keyfile
# 必須提供證書以保證數據安全性
#require_certificate false
# 若require_certificate值為true,use_identity_as_username也必須為true
#use_identity_as_username false
# 啟用PSK(Pre-shared-key)支持
#psk_hint
# SSL/TSL加密算法,可以使用“openssl ciphers”命令獲取
# as the output of that command.
#ciphers
# =================================================================
# Persistence
# =================================================================
# 消息自動保存的間隔時間
#autosave_interval 1800
# 消息自動保存功能的開關
#autosave_on_changes false
# 持久化功能的開關
persistence true
# 持久化DB文件
#persistence_file mosquitto.db
# 持久化DB文件目錄
#persistence_location /var/lib/mosquitto/
# =================================================================
# Logging
# =================================================================
# 4種日志模式:stdout、stderr、syslog、topic
# none 則表示不記日志,此配置可以提升些許性能
log_dest none
# 選擇日志的級別(可設置多項)
#log_type error
#log_type warning
#log_type notice
#log_type information
# 是否記錄客戶端連接信息
#connection_messages true
# 是否記錄日志時間
#log_timestamp true
# =================================================================
# Security
# =================================================================
# 客戶端ID的前綴限制,可用於保證安全性
#clientid_prefixes
# 允許匿名用戶
#allow_anonymous true
# 用戶/密碼文件,默認格式:username:password
#password_file
# PSK格式密碼文件,默認格式:identity:key
#psk_file
# pattern write sensor/%u/data
# ACL權限配置,常用語法如下:
# 用戶限制:user <username>
# 話題限制:topic [read|write] <topic>
# 正則限制:pattern write sensor/%u/data
#acl_file
# =================================================================
# Bridges
# =================================================================
# 允許服務之間使用“橋接”模式(可用於分布式部署)
#connection <name>
#address <host>[:<port>]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
# 設置橋接的客戶端ID
#clientid
  # 橋接斷開時,是否清除遠程服務器中的消息
  #cleansession false
  # 是否發布橋接的狀態信息
  #notifications true
  # 設置橋接模式下,消息將會發布到的話題地址
  # $SYS/broker/connection/<clientid>/state
  #notification_topic
  # 設置橋接的keepalive數值
  #keepalive_interval 60
  # 橋接模式,目前有三種:automatic、lazy、once
  #start_type automatic
  # 橋接模式automatic的超時時間
  #restart_timeout 30
  # 橋接模式lazy的超時時間
  #idle_timeout 60
  # 橋接客戶端的用戶名
  #username
  # 橋接客戶端的密碼
  #password
  # bridge_cafile:橋接客戶端的CA證書文件
  # bridge_capath:橋接客戶端的CA證書目錄
  # bridge_certfile:橋接客戶端的PEM證書文件
  # bridge_keyfile:橋接客戶端的PEM密鑰文件
  #bridge_cafile
  #bridge_capath
  #bridge_certfile
  #bridge_keyfile
  # 自己的配置可以放到以下目錄中
  include_dir /etc/mosquitto/conf.d

啟動服務

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

主要還是根據需求來配置好用戶和加密,用的時候需要了解mqtt協議。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM