MQTT代理服務(wù)器Mosquitto實(shí)戰(zhàn)

市面上有相當(dāng)多的高質(zhì)量MQTT代理,mosquitto是一個(gè)開源的輕量級的C實(shí)現(xiàn),完全兼容MQTT 3.1和MQTT 3.1.1,配合mosquitto_pub和mosquitto_sub命令行工具,是一個(gè)比較適合MQTT入門的工具。
本文以mosquitto為例搭建MQTT服務(wù),測試環(huán)境是阿里云(mqtt.youyangiot.com:47.94.221.244)和本地(192.168.100.101)Ubuntu 16.04.4 LTS系統(tǒng)。

1. 安裝和使用

安裝mosquitto及mosquitto-clients

sudo apt-get install software-properties-common python-software-properties
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

測試訂閱

mosquitto_sub -h 47.94.221.244 -d -t 'room-A/temperature'

測試發(fā)送

mosquitto_pub -h 47.94.221.244 -d -t 'room-A/temperature' -m '{"time":"1509289143197","code":0,"id":"09df1610-3bfb-4eab-a461-26e83962b10a","temp":25}'
2. SSL/TLS加密

MQTT是基于TCP的,默認(rèn)情況通訊并不加密。如果需要傳輸敏感信息或者對設(shè)備進(jìn)行反控,使用SSL/TLS幾乎是必須的。
注意:在填寫FQDN(Fully Qualified Domain Name)字段的時(shí)候最好用服務(wù)器域名,本文中使用mqtt.youyangiot.com。

生成CA證書和密鑰

openssl req -new -x509 -days 365 -extensions v3_ca -keyout ca.key -out ca.crt

生成MQTT代理服務(wù)器的密鑰

openssl genrsa -out server.key 2048

生成MQTT代理服務(wù)器的CSR(Certificate Signing Request)

openssl req -out server.csr -key server.key -new

通過CA簽署CSR,生成MQTT代理證書

openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365

配置/etc/mosquitto/mosquitto.conf,啟用SSL/TLS

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

重啟mosquitto服務(wù)

sudo service  mosquitto stop
sudo service  mosquitto start

測試訂閱

mosquitto_sub -h mqtt.youyangiot.com -p 8883 -d -t 'room-A/temperature' --cafile ./ca.crt

測試發(fā)送

mosquitto_pub -h mqtt.youyangiot.com -p 8883 -d -t 'room-A/temperature' -m '{"time":"1509289143197","code":0,"id":"09df1610-3bfb-4eab-a461-26e83962b10a","temp":25}' --cafile ./ca.crt
3. 客戶端X509證書認(rèn)證

MQTT代理在TLS握手成功之后可以繼續(xù)發(fā)送客戶端的X509證書來認(rèn)證設(shè)備,如果設(shè)備不合法便可以中斷連接,代價(jià)是需要有較好的證書創(chuàng)建流程和證書的管理系統(tǒng)。

生成MQTT設(shè)備的密鑰

openssl genrsa -out client.key 2048

生成MQTT設(shè)備的CSR(Certificate Signing Request)

openssl req -out client.csr -key client.key -new

通過CA簽署CSR,生成MQTT設(shè)備證書

openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 365

配置/etc/mosquitto/mosquitto.conf,啟用SSL/TLS

listener 8883
protocol mqtt
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
require_certificate true

重啟mosquitto服務(wù)

sudo service  mosquitto stop
sudo service  mosquitto start

測試訂閱

mosquitto_sub -h mqtt.youyangiot.com -p 8883 -d -t 'room-A/temperature' --cafile ./ca.crt --cert client.crt --key client.key

測試發(fā)送

mosquitto_pub -h mqtt.youyangiot.com -p 8883 -d -t 'room-A/temperature' -m '{"time":"1509289143197","code":0,"id":"09df1610-3bfb-4eab-a461-26e83962b10a","temp":25}' --cafile ./ca.crt --cert client.crt --key client.key
4. 客戶端用戶名密碼認(rèn)證

Mosquitto支持用戶名/密碼認(rèn)證方式,使用起來非常方便,不過由于用戶名密碼是以明文形式傳輸,最好配合SSL/TSL加密使用。

生成用戶名和密碼文件

cd /etc/mosquitto
sudo mosquitto_passwd -c passwd test

配置/etc/mosquitto/mosquitto.conf,啟用SSL/TLS

listener 8883
protocol mqtt
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
password_file /etc/mosquitto/passwd
allow_anonymous false

重啟mosquitto服務(wù)

sudo service  mosquitto stop
sudo service  mosquitto start

測試訂閱

mosquitto_sub -h mqtt.youyangiot.com -p 8883 -d -t 'room-A/temperature' --cafile ./ca.crt -u test -P 123456

測試發(fā)送

mosquitto_pub -h mqtt.youyangiot.com -p 8883 -d -t 'room-A/temperature' -m '{"time":"1509289143197","code":0,"id":"09df1610-3bfb-4eab-a461-26e83962b10a","temp":25}' --cafile ./ca.crt -u test -P 123456
5. WebSocket支持

Mosquitto支持WebSocket的調(diào)用方式,為HTML類型的應(yīng)用提供非常便利的方式。

配置/etc/mosquitto/mosquitto.conf,啟用WebSocket

listener 8080
protocol websockets

如果要啟用SSL/TLS WebSocket

listener 8081
protocol websockets
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key

重啟mosquitto服務(wù)

sudo service  mosquitto stop
sudo service  mosquitto start

JS在線客戶端測試 paho JS client

websocket-js-online.png

python客戶端WebSocket適配代碼

client = mqtt.Client(transport='websockets')
client.connect(mqtt.youyangiot.com, 8080, 60)

參考文章


Eclipse Mosquitto - An open source MQTT broker
Eclipse Paho
MQTT入門篇

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一:前言 最近在了解MQTT協(xié)議相關(guān)的內(nèi)容,內(nèi)容有點(diǎn)多,特此把MQTT協(xié)議,以及其從服務(wù)端到客戶端的流程整理出來...
    子夏的不語閱讀 70,764評論 9 92
  • 服務(wù)器https配置 配置https操作說明文檔 1、查看服務(wù)器環(huán)境配置(tomcat和apache合并使用) 2...
    南京楊小兵閱讀 9,255評論 0 9
  • vsftpd.conf 部分:文件格式(5)索引 返回主要內(nèi)容 名稱 vsftpd.conf - vsftpd的配...
    張龍象閱讀 2,514評論 0 1
  • (1) 今天辦公室來了一位家長,孩子是班上最拖拉的一個(gè)男孩子,聽講不認(rèn)真,作業(yè)寫不完,成績很糟糕,上周布置的數(shù)學(xué),...
    徍音_閱讀 882評論 0 2

友情鏈接更多精彩內(nèi)容