码迷,mamicode.com
首页 > 其他好文 > 详细

Mqtt服务器搭建

时间:2017-05-10 14:49:37      阅读:1159      评论:0      收藏:0      [点我收藏+]

标签:nec   load   操作系统   需要   9.png   client   oca   测试环境   squit   

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 –C /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服务

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


技术分享

 

配置代理:

打开一个终端叫B,在B里开启代理服务

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

技术分享

技术分享

 

再打开一个终端叫A,A里充当订阅者,输入如下命令

mosquitto --help

这里报错了

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_sub -t goodafternoon   #订阅good_afternoon
 

再打开一个终端叫C,在C里执行如下命令

发布消息

mosquitto_pub -t goodafternoon -m "hello world"

 

可以在代理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

 

Mqtt服务器搭建

标签:nec   load   操作系统   需要   9.png   client   oca   测试环境   squit   

原文地址:http://www.cnblogs.com/demonxian3/p/6835405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!