快速搭建你的MQTT服務器

       MQTT服務器在Linux和Windows上的搭建稍微有些區別,不過使用第三方開源的項目一般會有比較詳細的說明文檔。不做過多贅述。筆者搭建環境是windows10。

ActiveMQ

1、windows MQTT服務器下載

https://activemq.apache.org/components/classic/download/

mosquitto

1、下載與安裝 http://mosquitto.org/download/

2、在配置文件mosquitto.conf中,任意位置加入下面文本,保存退出(我說的全新安裝的情況下,如果是維護已經使用很久的系統,可在配置文件中找到對應的屬性進行更改)

#設置不允許匿名登錄 

allow_anonymous false 

#設置賬戶密碼文件位置爲C:\MosquittoTest\pwfile.example 

password_file /MosquittoTest/pwfile.example

 

 

 

 

 

3、重啓mosquitto服務配置文件的修改才能生效。爲了方便調試和操作,我們後面的操作全部通過命令行模式進行。

4、插入新用戶名及密碼,輸入密碼時界面是不會顯示的,直接輸入後回車就可以,需要連續輸入兩次。保證pwfile.example的路徑和上面的配置一致。下面打開CMD並進入mosquitto根目錄輸入:

mosquitto_passwd -c /MosquittoTest/pwfile.example FirstUserName (使用-c 參數會導致清空密碼文件,重新插入用戶)

mosquitto_passwd /MosquittoTest/pwfile.example SecondUserName (不使用-c 表示追加用戶,不影響舊用戶)

5、創建成功後pwfile.example會出現剛剛添加的用戶信息。

 

 6、啓動mosquitto 進行測試。

(1)以管理員的方式運行第一個CMD。進入到mosquitto的目錄下“,啓動服務器

mosquitto.exe -v

(2)以管理員的方式運行第二個CMD,進入到mosquitto的目錄下,訂閱一個主題:

mosquitto_sub -u UserName -P password -t 'James/topic' -v

(3)以管理員的方式運行第三個(或更多個)CMD,進入到mosquitto的目錄下,發佈一個主題消息:

mosquitto_pub -u UserName -P password -t 'James/topic' -m '喜歡也沒用,沒用也喜歡'

 

mosquitto>mosquitto.exe --help

mosquitto version 1.6.9

mosquitto is an MQTT v3.1.1 broker.

Usage: mosquitto [-c config_file] [-d] [-h] [-p port]

-c : specify the broker config file.

-d : put the broker into the background after starting.

-h : display this help.

-p : start the broker listening on the specified port.

Not recommended in conjunction with the -c option.

-v : verbose mode - enable all logging types. This overrides

any logging options given in the config file.

 

See http://mosquitto.org/ for more information.

EMQ X

下載地址 https://www.emqx.io/downloads

文檔地址 https://docs.emqx.io/broker/v3/cn/getstarted.html

./bin/emqx start 

# 檢查運行狀態

./bin/emqx_ctl status 

# 停止

emqx ./bin/emqx stop

總結:

使用過程中,個人覺得還是mouquitto,尤其是EMQX比較好用一些,着重推薦。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章