Mosquitto-1.4.15源码编译与使用

https://mosquitto.org/

https://mosquitto.org/download/

http://mosquitto.org/files/source/  --- download

https://github.com/eclipse/mosquitto

https://mosquitto.org/man/mosquitto-conf-5.html --- document

 

CentOS 7,mosquitto-1.4.15

1、环境依赖库的安装

yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel

yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
yum install libstdc++-devel -y
yum install openssl-devel -y
yum install c-ares-devel -y
yum install e2fsprogs-devel -y
yum install uuid-devel -y
yum install libuuid-devel -y

 

2、CentOS7默认的防火墙是firewall,关掉它

关闭firewall防火墙:sudo systemctl stop firewalld.service 

关闭firewall开机启动:sudo systemctl disable firewalld.service

firewall-cmd --state #查看状态

 

3、编译mosquitto源码

(1)切换到/home/

/mosquitto-1.4.15目录下

(2)make && make install -- 本人只执行make,没有执行make install

 

4、运行broker

(1)tar xvzf mosquitto-1.4.15.tar.gz

把配置文件mosquitto-1.4.15/mosquitto.conf拷贝一份到mosquitto-1.4.15/src/

(2)修改mosquitto.conf,第43行,因为当前是root账号,所以新增root用户名

#user mosquitto

user root

(3)启动broker

记得先设置每个进程允许打开的最大文件数
[root@localhost src]# ulimit -n 5120000

方法1,它是个窗口进程,可以看到调试信息。

[root@localhost src]# ./mosquitto -c mosquitto.conf -v

方法2,它是个守护者进程,即使终端窗口关闭,仍然在后台运行。

[root@localhost src]# ./mosquitto -c mosquitto.conf -d

firecat注:

(1)-c表示指明参数文件-v是可选参数,表示verbose详细打印输出的意思-d是daemon的意思

(2)如果使用Linux QtCreator编译和查看源码,运行时需要把mosquitto.conf拷贝到结果文件夹

/build-mosquitto-1.4.15-Desktop_Qt_5_9_1_GCC_64bit-Debug/src/

另外,需要填写命令行参数

Command line arguments:-c mosquitto.conf

 

5、查看和退出broker

[root@izwz91gznj4owe94k2j06uz src]# netstat -tunlp|egrep "1883"
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      28335/./mosquitto   
tcp6       0      0 :::1883                 :::*                    LISTEN      28335/./mosquitto   
[root@izwz91gznj4owe94k2j06uz src]# netstat -tunlp|egrep "mosquitto"
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      28335/./mosquitto   
tcp6       0      0 :::1883                 :::*                    LISTEN      28335/./mosquitto   
[root@izwz91gznj4owe94k2j06uz src]# kill -9 28335

 

6、客户端发布订阅测试

(1)把文件/mosquitto-1.4.15/lib/libmosquitto.so.1拷贝到/usr/lib/,然后终端执行命令ldconfig

[root@localhost]# ldconfig

(2)进入路径/mosquitto-1.4.15/client

订阅

[root@localhost client]# ./mosquitto_sub -v -t topic01

[root@localhost client]#./mosquitto_sub -h 19.128.13.196 -p 1883 -t topic01 -d

[root@localhost client]#./mosquitto_sub -h 19.128.13.196 -p 1883 -t topic01 -u firecatTerm -P in2011111 -d 

[root@localhost client]#./mosquitto_sub -L mqtt://firecatTerm:[email protected]:1883/topic01

其中-d表示调试模式,打印信息更丰富,选配。

-u表示username,-P表示password。

发布

[root@localhost client]# ./mosquitto_pub -t topic01 -m hello

(3)详细说明

订阅服务:
mosquitto_sub -t HelloWord
mosquitto_sub 订阅命令
-t 订阅主题
Helloworld 主题名称,可以随便取名
发布服务:
mosquitto_pub -t HelloWord -h localhost -m "Hello Word"
mosquitto_pub 发布命令
-t 发布主题
Helloworld 主题名称
-h 主机地址
localhost 发布到本地,如果远程主机直接写ip地址(不用写端口号)
-m 发布的信息
“Hello world” 发布的内容

 

7、mosquitto.conf参数详解

# =================================================================
# General configuration
# =================================================================

# 客户端心跳的间隔时间
#retry_interval 20

# 系统状态的刷新时间
#sys_interval 10

# 系统资源的回收时间,0表示尽快处理
#store_clean_interval 10

# 服务进程的PID
#pid_file /var/run/mosquitto.pid

# 服务进程的系统用户
#user mosquitto

# 客户端心跳消息的最大并发数
#max_inflight_messages 10

# 客户端心跳消息缓存队列
#max_queued_messages 100

# 用于设置客户端长连接的过期时间,默认永不过期
#persistent_client_expiration

# =================================================================
# Default listener
# =================================================================

# 服务绑定的IP地址
#bind_address

# 服务绑定的端口号
#port 1883

# 允许的最大连接数,-1表示没有限制
#max_connections -1

# cafile:CA证书文件
# capath:CA证书目录
# certfile:PEM证书文件
# keyfile:PEM密钥文件
#cafile
#capath
#certfile
#keyfile

# 必须提供证书以保证数据安全性
#require_certificate false

# 若require_certificate值为true,use_identity_as_username也必须为true
#use_identity_as_username false

# 启用PSK(Pre-shared-key)支持
#psk_hint

# SSL/TSL加密算法,可以使用“openssl ciphers”命令获取
# as the output of that command.
#ciphers

# =================================================================
# Persistence
# =================================================================

# 消息自动保存的间隔时间
#autosave_interval 1800

# 消息自动保存功能的开关
#autosave_on_changes false

# 持久化功能的开关
persistence true

# 持久化DB文件
#persistence_file mosquitto.db

# 持久化DB文件目录
#persistence_location /var/lib/mosquitto/

# =================================================================
# Logging
# =================================================================

# 4种日志模式:stdout、stderr、syslog、topic
# none 则表示不记日志,此配置可以提升些许性能
log_dest none

# 选择日志的级别(可设置多项)
#log_type error
#log_type warning
#log_type notice
#log_type information

# 是否记录客户端连接信息
#connection_messages true

# 是否记录日志时间
#log_timestamp true

# =================================================================
# Security
# =================================================================

# 客户端ID的前缀限制,可用于保证安全性
#clientid_prefixes

# 允许匿名用户
#allow_anonymous true

# 用户/密码文件,默认格式:username:password
#password_file

# PSK格式密码文件,默认格式:identity:key
#psk_file

# pattern write sensor/%u/data
# ACL权限配置,常用语法如下:
# 用户限制:user 
# 话题限制:topic [read|write] 
# 正则限制:pattern write sensor/%u/data
#acl_file

# =================================================================
# Bridges
# =================================================================

# 允许服务之间使用“桥接”模式(可用于分布式部署)
#connection 
#address [:]
#topic  [[[out | in | both] qos-level] local-prefix remote-prefix]

# 设置桥接的客户端ID
#clientid

# 桥接断开时,是否清除远程服务器中的消息
#cleansession false

# 是否发布桥接的状态信息
#notifications true

# 设置桥接模式下,消息将会发布到的话题地址
# $SYS/broker/connection//state
#notification_topic

# 设置桥接的keepalive数值
#keepalive_interval 60

# 桥接模式,目前有三种:automatic、lazy、once
#start_type automatic

# 桥接模式automatic的超时时间
#restart_timeout 30

# 桥接模式lazy的超时时间
#idle_timeout 60

# 桥接客户端的用户名
#username

# 桥接客户端的密码
#password

# bridge_cafile:桥接客户端的CA证书文件
# bridge_capath:桥接客户端的CA证书目录
# bridge_certfile:桥接客户端的PEM证书文件
# bridge_keyfile:桥接客户端的PEM密钥文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile

 

----

Linux:

chmod -R 777 jemalloc-4.5.0

netstat -nalp|grep 1883 #查看1883端口的连接情况,观察TCP状态图
netstat -nalp|grep 1883|wc -l #查看1883端口的客户端连接数
ulimit -n 5120000 #修改当前进程的最大文件数

top -p 进程号 #查看某个进程的CPU和内存情况

tail -f *.log #实时查看日志内容

[root@localhost ~]# netstat -nalp|grep 80
[root@localhost ~]# kill -9 43221
更多内容请见:erlang物联网开源项目emqttd下载,编译和图形化监控相关

Windows:

netstat -aon|findstr "1883"  #查看1883端口的连接情况,观察TCP状态图

 

---

参考文章

MQTT的学习之Mosquitto安装&使用(1)

centos7 安装 mosquitto 及集群、高可用

Mosquitto 客户端使用介绍

学习教程:https://blog.csdn.net/houjixin/article/category/2127857

你可能感兴趣的:(Mosquitto-1.4.15源码编译与使用)