Mqtt服务器搭建
__
Mqtt服务器搭建
测试环境:CentOS64位
1.安装基础软件
yum install gcc-c++ yum install cmake yum install openssl-devel //mosquitto默认支持openssl
2.下载源码包
wget http://mosquitto.org/files/source/mosquitto-1.4.4.tar.gz wget http://c-ares.haxx.se/download/c-ares-1.10.0.tar.gz --no-check-certificate wget https://github.com/warmcat/libwebsockets/archive/v1.3-chrome37-firefox30.tar.gz
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./ 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及一些大神的文章才得出的成功。感谢各位大神的努力和汗水。
Mqtt服务器搭建的更多相关文章
- 常见MQTT服务器搭建与试用
常见MQTT服务器搭建与试用 简介 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,它比较适合于在低带宽.不可靠 ...
- 个人智能家居系统 - MQTT服务器搭建(centOS7.3)
个人智能家居系统 - MQTT服务器搭建(centOS7.3) 0x00 参考 在CentOS7 上安装mosquitto1.4.1服务器,实现MQTT信息推送功能并增加websocket功能 mos ...
- 常见MQTT服务器搭建[转载]
简介 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,它比较适合于在低带宽.不可靠的网络的进行远程传感器和控制设备通 ...
- Apollo mqtt 服务器搭建
html { overflow-x: initial !important } :root { --bg-color: #ffffff; --text-color: #333333; --select ...
- MQTT——服务器搭建(一)
MQTT介绍 MQTT,是IBM推出的一种针对移动终端设备的基于TCP/IP的发布/预订协议,可以连接大量的远程传感器和控制设备: 轻量级的消息订阅和发布(publish/subscribe)协议 建 ...
- MQTT服务器搭建-mosquitto1.4.4安装指南
Mosquitto mosquitto是一款实现了 MQTT v3.1 协议的开源的消息代理服务软件. 其提供了非常轻量级的消息数据传输协议,采用发布/订阅模式进行工作,可用于物联设备.中间件.APP ...
- MQTT服务器搭建--Mosquitto用户名密码配置
Mosquitto用户认证配置 前言:基于Mosquitto服务器已经搭建成功,大部分都是采用默认的是允许匿名用户登录模式,正式上线的系统需要进行用户认证. 1.用户参数说明 Mosquitto服务器 ...
- MQTT服务器搭建--Apollo
尊重原创,我是伸手党:https://blog.csdn.net/u012377333/article/details/68943416 1.Apollo下载 下载地址:http://activemq ...
- Windows 下 MQTT 服务器搭建之Apollo
https://blog.csdn.net/wangh0802/article/details/84861226#%EF%BC%881%EF%BC%89%E4%B8%8B%E8%BD%BD%20Apo ...
随机推荐
- Dapper源码学习和源码修改
之前ORM比较火热,自己也搞了个WangSql,但是感觉比较low,大家都说Dapper性能好,所以现在学习学习Dapper,下面简单从宏观层面讲讲我学习的Dapper. 再了解一个东西前,先得学会使 ...
- 【一】Swift 3.0 新浪微博项目实战 -整体框架搭建
最近要接手swift,所以找了个视频跟着做一下实战项目,在此记录一下过程和心得 框架搭建和目录拆分 关键词:MVVM 架构,桥接文件 桥接文件用于引入OC的头文件,Swift就可以正常使用(宏除外). ...
- NMF和SVD在推荐系统中的应用(实战)
本文以NMF和经典SVD为例,讲一讲矩阵分解在推荐系统中的应用. 数据 item\user Ben Tom John Fred item 1 5 5 0 5 item 2 5 0 3 4 item 3 ...
- python遍历一个目录,输出所有文件名
python遍历一个目录,输出所有文件名 python os模块 os import os def GetFileList(dir, fileList): newDir = dir if os. ...
- Jmeter新建用例图示
添加线程组 添加HTTP请求 编辑HTTP请求 添加HTTP信息头 编辑HTTP信息头 添加断言 添加查看结果树 添加聚合报告 添加响应时间 添加TPS 批量运行命令: ...
- z-index失效的原因
在做的过程中,发现了一个很简单却又很多人应该碰到的问题,设置Z-INDEX属性无效.在CSS中,只能通过代码改变层级,这个属性就是z-index,要让z-index起作用有个小小前提,就是元素的pos ...
- unity3d 中文乱码解决方法——cs代码文件格式批量转化UTF8
在Unity3d中经常会碰到中文乱码的问题,比如代码中的[AddComponentMenu("GameDef/AI/战机AI")],注释,中文文本等等 其原因在于,unity本身是 ...
- oracle索引(转)
引,索引的建立.修改.删除 2007-10-05 13:29 来源: 作者: 网友评论 0 条 浏览次数 2986 索引索引是关系数据库中用于存放每一条记录的一种对象,主要目的是加快数据的读取速度和完 ...
- opencv配置(win10+VS2015+opencv3.1)
Step 1:准备工作 a.win10 b.vs2015 c.opencv3.1[从http://opencv.org/downloads.html下载] Step 2.开始安装 a. 双击openc ...
- hive 动态分区数设置
当对hive分区未做设置时,报错如下: Caused by: org.apache.hadoop.hive.ql.metadata.HiveFatalException: [Error 20004]: ...
