Mqtt服務器搭建

 

Mqtt服務器搭建html

測試環境:CentOS64位c++

 

1.安裝基礎軟件git

yum install gcc-c++

yum install cmake

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


2.下載源碼包github

 

3.解壓源碼包web

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.編譯準備chrome

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

 

vim compiling.txt  #這個文件裏有寫須要的依賴包,內容以下express

The following packages are required for mosquitto:vim

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

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

* 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及一些大神的文章才得出的成功。感謝各位大神的努力和汗水。

相關文章
相關標籤/搜索