吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3812|回复: 5
收起左侧

[Java 转载] 【MQTT】Cento7 安装mosquitto以及实现离线消息功能

  [复制链接]
yljungg 发表于 2020-12-23 17:46
本帖最后由 yljungg 于 2020-12-23 17:54 编辑

简介:MQTT常用物联网开发中,如今工业物联发展呈上升趋势,MQTT的使用还是很有必要的。
1.安装运行环境
因为mosquitto是基于c语言开发的,所有在centos中需要c的运行环境。
[Shell] 纯文本查看 复制代码
yum -y install gcc gcc-c++
yum -y install openssl-devel
yum -y install c-ares-devel
yum -y install libuuid-devel
yum -y install wget
yum -y install cmake
yum -y install build-essential python quilt devscripts python-setuptools python3 
yum -y install libssl-dev libc-ares-dev uuid-dev daemon openssl-devel

openssl-devel安装websokcet插件libwebsocketsmqtt支持websocket链接,比如网页链接Mqtt需要)
[Shell] 纯文本查看 复制代码
# 下载并编译安装libwebsockets(mqtt支持websocket链接,比如网页链接Mqtt需要)
wget https://github.com//warmcat/libwebsockets/archive/v4.0.15.tar.gz
tar zxvf 4.0.15.tar.gz
cd libwebsockets-4.0.15
mkdir build
cd build
cmake .. -DLIB_SUFFIX=64
make install
ldconfig

2.下载并安装
[Shell] 纯文本查看 复制代码
wget http://mosquitto.org/files/source/mosquitto-1.6.9.tar.gz
tar -xzf mosquitto-1.6.9.tar.gz
cd mosquitto-1.6.9

# 修改配置文件(打开websocket插件)
vim config.mk
WITH_WEBSOCKETS:=yes
# 安装
make && make install
# 移动配置文件
mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

插件参数

选项说明make时的出错信息
WITH_SRV启用c-areas库的支持,一个支持异步DNS查找的库,见http://c-ares.haxx.semissing ares.h
WITH_UUID启用lib-uuid支持,支持为每个连接的客户端生成唯一的uuidmissing uuid.h
WITH_WEBSOCKETS启用websocket支持,需安装libwebsockets,对于需要使用websocket协议的应用开启missing libwebsockets.h

3.运行
[Shell] 纯文本查看 复制代码
# 前台运行,可以看简单的日志
mosquitto -c /etc/mosquitto/mosquitto.conf
# degbug运行可以看更多日志
mosquitto -c /etc/mosquitto/mosquitto.conf -v
# 后台运行
mosquitto -c /etc/mosquitto/mosquitto.conf -d

4.配置文件详解
[Shell] 纯文本查看 复制代码
# 配置文件
vim /etc/mosquitto/mosquitto.conf

[Shell] 纯文本查看 复制代码
# =================================================================
# 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 <username>
# 话题限制:topic [read|write] <topic>
# 正则限制:pattern write sensor/%u/data
#acl_file
# =================================================================
# Bridges
# =================================================================
# 允许服务之间使用“桥接”模式(可用于分布式部署)
#connection <name>
#address <host>[:<port>]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
# 设置桥接的客户端ID
#clientid
# 桥接断开时,是否清除远程服务器中的消息
#cleansession false
# 是否发布桥接的状态信息
#notifications true
# 设置桥接模式下,消息将会发布到的话题地址
# $SYS/broker/connection/<clientid>/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

5.离线消息配置
修改配置文件
[Shell] 纯文本查看 复制代码
# 首先编辑配置文件
autosave_interval 60
persistence true
persistence_file mosquitto.db
persistence_location /var/lib/mosquitto/

创建mosquitto文件夹,并chomd 777,是mosquitto能够创建存储文件
[Shell] 纯文本查看 复制代码
cd /var/lib
mkdir mosquitto
chomd 777 mosquitto

6.常见问题
a.libwebsockets.so.12
[Shell] 纯文本查看 复制代码
mosquitto: error while loading shared libraries: libwebsockets.so.12: cannot open shared object file

解决方案:修改/etc/ld.so.conf文件,加入libwebsockets的so路径,如下
vim /etc/ld.so.conf
[Shell] 纯文本查看 复制代码
include ld.so.conf.d/*.conf
/usr/local/software/libwebsockets-4.0.15/build/lib #自己的安装路径

执行命令
[Shell] 纯文本查看 复制代码
ldconfig

重新开启mosquitto
[Shell] 纯文本查看 复制代码
mosquitto -c /etc/mosquitto/mosquitto.conf -d

b. Invalid user 'mosquitto'
方法1:修改配置文件:mosquitto.conf ,增加登录的用户,例如当前登录用户为root
[Shell] 纯文本查看 复制代码
user root

方法2:添加用户(推荐)
[Shell] 纯文本查看 复制代码
adduser mosquitto

对应稳定的依赖包(dupan):
https://pan.baidu.com/s/1h2Nas_vtIx8JwU4xmFMOBQ
x4rt

免费评分

参与人数 2吾爱币 +3 热心值 +1 收起 理由
xcdmpj + 1 用心讨论,共获提升!
howsk + 2 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Eaglecad 发表于 2020-12-23 19:21
不错,整个是安装配置过程。赞一个
 楼主| yljungg 发表于 2020-12-23 20:20
离线功能:
还需订阅短设置cleansession:false qos1及以上
推送端设置qos1级以上
tips:mosquitto配置queue_qos0_messages true;可以达到qos0也能离线功能,cleansession人要要设置false
feiyu361 发表于 2020-12-24 17:04
a3576419 发表于 2020-12-25 10:47
不知道该如何整起,看着网上的教程也一头雾水
依然小圣 发表于 2020-12-28 15:32
建议顺道出以下测试步骤,比如用MQTT.fx 订阅 消费
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-14 03:31

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表