为了账号安全,请及时绑定邮箱和手机立即绑定

centos7 安装 mosquitto 及集群、高可用

标签:
Linux

mosquitto 是一个基于 mqtt 协议的 broker

基于阿里云的centos7服务器

安装所需的环境

第一步下载 yum 源

cd /etc/yum.repos.d/
wget http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/home:oojah:mqtt.repo

重命名文件

mv home\:oojah\:mqtt.repo mosquitto.repo

开始安装

yum search all mosquitto --查找所有可选安装项
yum install mosquitto mosquitto-clients

安装完成

所有的配置文件都在 /etc/mosquitto/下

主要的配置文件 mosquitto.conf

# Place your local configuration in /etc/mosquitto/conf.d/
# 将自己的配置文件放在 /etc/mosquitto/conf.d/ 目录下
pid_file /var/run/mosquitto.pid
#持久化
persistence true
#持久化文件目录 
persistence_location /var/lib/mosquitto/
#日志文件
log_dest file /var/log/mosquitto/mosquitto.log
#引入 conf.d 目录下的配置文件
include_dir /etc/mosquitto/conf.d

完整配置示例

网上很多配置文件说明我觉得很值得思考,跟注释相差很大,所以我自己翻译一遍

# Config file for mosquitto
#
# See mosquitto.conf(5) for more information.
#
# Default values are shown, uncomment to change.
#
# Use the # character to indicate a comment, but only if it is the 
# very first character on the line.

# =================================================================
# General configuration
# =================================================================
# 重新发送已经发出去的Qos 为1或者2的消息的等待时间
retry_interval 20

# 系统状态的刷新时间,设置为0表示不刷新
sys_interval 10

#清除在内部消息存储里面的未引用的消息的时间。
#较低的值将占用较少的内存,但处理器时间较长,
#越高的值将产生相反的效果。
#设置值为0意味着未引用的消息将以尽可能快的速度处理。
store_clean_interval 10

#pid_file

# 以什么用户启动 mosquitto,此配置在 windows 下无效,以非 root 运行无效
#user mosquitto

#当前每个客户端正在传输的Qo1和2消息的最大数量。
#这包括通过握手信息,以及那些正在重试的信息。
#默认为20。设置为0表示无上限。
#设置为1将保证QoS 1 和2的消息按顺序传递
max_inflight_messages 20

#当前正在进行的队列中Qos 1和2条消息的最大数量。默认为100。
#设置到0表示没有上限(不推荐)。同样可参见queue_qos0_messages
max_queued_messages 100

#设置为true,当一个持久客户端被断开连接时,以Qos为0将消息放到队列中。
#这些消息受max_queued_messages限制
queue_qos0_messages false

#此选项设置被代理允许发布的消息的大小。
#超过这个尺寸的消息将不会被代理接受。
#默认值为0,这意味着所有有效的MQTT消息都被接受。
#MQTT的最大有效大小为268435455字节

message_size_limit 0

# 用于设置客户端长连接的过期时间,默认永不过期,必须以h d w m y为单位
#分别代表 小时,天,星期,月,念
#persistent_client_expiration

# 如果客户端订阅了多个重叠的订阅,例如foo/#和foo/+/baz,然后MQTT期望当代理接
#收到一个与两个订阅相匹配的主题的消息时,例如foo/bar/baz,那么客户端应该只接
#收一次消息。为了满足这一要求,mosquitto不断跟踪发送给客户的消息。允许重复的
#消息选项允许禁用此行为,如果您有大量的客户端订阅相同的主题集合,并且非常关注
#最小化内存使用的情况,那么这个选项可能是有用的。如果你事先知道你的客户端永不
#会有重叠的订阅,那么你的客户必须能够正确处理重复的信息,即使在Qo = 2的时候,
#你的客户端也必须能够正确地处理重复的信息
#allow_duplicate_messages false

# =================================================================
# 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

启动服务

mosquitto -c /etc/mosquitto/mosquitto.conf -d   -d 表示后台启动

配置集群

cd conf.d
vim cloud.conf

connection mytest
address ip:1883
topic # both 2 "" ""
notifications true
cleansession false
start_type automatic

先开始从服务器,再开启主服务器,这时会看到Connecting bridge mytest (ip:1883)

[root@************** ~]# mosquitto -c /etc/mosquitto/mosquitto.conf
1501000938: mosquitto version 1.4.14 (build date 2017-07-10 22:57:17+0000) starting
1501000938: Config loaded from /etc/mosquitto/mosquitto.conf.
1501000938: Opening ipv4 listen socket on port 1883.
1501000938: Opening ipv6 listen socket on port 1883.
1501000938: Connecting bridge mytest (ip:1883)
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消