Mqtt服務器搭建


 

Mqtt服務器搭建

測試環境:CentOS64位

 

1.安裝基礎軟件

yum install gcc-c++

yum install cmake

yum install openssl-devel  //mosquitto默認支持openssl


2.下載源碼包

 

3.解壓源碼包

tar xf c-ares-1.10.0.tar.gz && mv c-ares-1.10.0 /usr/local/src/

tar xf v1.3-chrome37-firefox30 -C /usr/local/src/

tar xf mosquitto-1.4.4.tar.gz -C /usr/local/src/

 

4.編譯准備

cd /usr/local/src/mosquitto-1.4.4/

 

vim compiling.txt  #這個文件里有寫需要的依賴包,內容如下

The following packages are required for mosquitto:

* tcp-wrappers (optional, package name libwrap0-dev)

* openssl (version 1.0.0 or greater if TLS-PSK support is needed, can be disabled)

* c-ares (for DNS-SRV support, can be disabled)

* libuuid (from e2fsprogs, can be disabled)

* On Windows, the Redhat pthreads library is required if threading support is

  to be included.

To compile, run "make", but also see the file config.mk for more details on the

various options that can be compiled in.

 

vim config.mk  #這個文件里包含多個選項,可以選擇自己需要的功能

常見功能說明

WITH_SRV
啟用c-areas庫,DNS查找的庫
missing ares.h
WITH_UUID     
啟用lib-uuid為每個連接客戶端生成uuid
missing uuid.h
WITH_WEBSOCKET
啟用WebSocket協議
missing libwebsockets.h

 

5.安裝tcp-wrappers

因為本機已經有了,所以就不安裝了

rpm –qa | grep tcp_wrap*

tcp_wrappers-7.6-57.el6.x86_64

tcp_wrappers-libs-7.6-57.el6.x86_64

 

6.編譯安裝c-ares

cd  /usr/local/src/c-ares-1.10.0

./configure

make

make install

 

 

7.安裝libuuid

yum -y install libuuid libuuid-devel

 

 

8.安裝websocket

cd /usr/local/src/libwebsockets-1.3-chrome37-firefox30/

vim README.build  #參照說明文檔內容編譯安裝即內容如下

 

  Generate the build files (default is Make files):

        cd /path/to/src

        mkdir build

        cd build

        cmake ..

        (NOTE: The build/ directory can have any name and be located anywhere

         on your filesystem, and that the argument ".." given to cmake is simply

         the source directory of libwebsockets containing the CMakeLists.txt

         project file. All examples in this file assumes you use "..")

        NOTE2

        A common option you may want to give is to set the install path, same

        as --prefix= with autotools.  It defaults to /usr/local.

        You can do this by, eg

        cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr  #指定安裝路徑

        NOTE3

        On machines that want libraries in lib64, you can also add the

        following to the cmake line

        -DLIB_SUFFIX=64                  #指定64位庫

        NOTE4

        If you are building against a non-distro OpenSSL (eg, in order to get

        access to ALPN support only in newer OpenSSL versions) the nice way to

        express that in one cmake command is eg,

        -DOPENSSL_ROOT_DIR=/usr/local/ssl        #指定ssl文件位置



mkdir build; cd build;

cmake .. -DLIB_SUFFIX=64

make install

 

9.開始安裝mosquitto服務

cd /usr/local/src/mosquitto-1.4.4/

make && make install && echo $?

 

默認文件路徑

/usr/local/sbin mosquitto server
/etc/mosquitto  configure
/usr/local/bin  utility command

 

10.修改鏈接庫

由於操作系統版本及架構原因,很容易出現安裝之后的鏈接庫無法被找到,如啟動mosquitto客戶端可能出現找不到libmosquitto.so.1文件,因此需要添加鏈接庫路徑

ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

ldconfig

 

否則會出現注意的錯誤

error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

 

還有一些編譯時可能會遇到的錯誤

mosquitto.c:871: error: ‘struct mosquitto’ has no member named ‘achan’

如果不使用可以在config.mk 把WITH_SRV功能關閉即可

 

其他問題也差不多,基本就是缺少相應的依賴包,解決辦法兩個

1在config.mk關閉相關選項

2 把依賴包裝上

嫌麻煩可以直接yum install mosquitto mosquitto-clients –y一句話搞定上面所有

 

開始測試

1.添加組和用戶

groupadd mosquitto

useradd –g mosquitto mosquitto

 

2.啟用,備份和編輯配置文件

cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf  #配置文件如下

# 服務進程的PID

#pid_file /var/run/mosquitto.pid

# 服務進程的系統用戶

#user mosquitto

# 服務綁定的IP地址

#bind_address

# 服務綁定的端口號

#port 1883

# 允許的最大連接數,-1表示沒有限制

#max_connections -1

# 允許匿名用戶

#allow_anonymous true


可以參考官網說明http://mosquitto.org/man/mosquitto-conf-5.html


 

配置代理:

實驗測試:在本地開啟三個終端來代表發布者服務代理訂閱者來模擬遠程測試

正常情況下:發布者 服務代理 訂閱者是分別是不同的host 也就是各自IP不同

所以若要測試遠程通信可以使用發布和訂閱命令時加上  -h  代理服務器的地址

 

先在服務代理終端上開啟mqtt服務

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

 

然后再打開一個終端訂閱主題,模擬訂閱者

mosquitto --help
mosquitto_sub -t goodafternoon   #訂閱goodafternoon主題

 

這里如果報錯:

mosquitto_sub: error while loading shared libraries: libcares.so.2: cannot open shared object file: No such file or directory

原因:找不到libcares.so.2庫文件

vim /etc/ld.so.conf.d/qt-x86_64.conf

添加下面兩行

/usr/local/lib64

/usr/local/lib

ldconfig  #刷新配置

 

 

然后再打開一個終端發布主題,模擬發布者

mosquitto_pub -t goodafternoon -m "hello world"  #向goodaftnoon發布消息

 

可以在代理B端上看到連接日志

1494420735: New connection from ::1 on port 1883.

1494420735: New client connected from ::1 as mosqsub/23455-demon-cli (c1, k60).

1494420850: New connection from ::1 on port 1883.

1494420850: New client connected from ::1 as mosqpub/23456-demon-cli (c1, k60).

1494420850: Client mosqpub/23456-demon-cli disconnected.

 

而在訂閱A端可以收到hello world這條消息!!

 

本文實驗參考http://www.cnblogs.com/littleatp/p/4835879.html及一些大神的文章才得出的成功。感謝各位大神的努力和汗水。

 


免責聲明!

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



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