赞
踩
本文 基于Rabbit MQ的mqtt插件,针对MQTT进行简单的测试。 包括MQTT协议的介绍,rabbitmq的安装、配置、开启mqtt插件及基于MQTTX进行的测试。
随着5G时代的到来,连接一切正在成为现实。大量设备的接入和设备管理给网络宽带、通信协议和平台服务体系结构带来了巨大的挑战。
MQTT协议是基于发布-订阅模型的物联网消息传递协议。由于其实现简单、易于实现、支持QoS和小分组,已占据物联网协议的一半
特点如下:
适用范围:在低带宽、不可靠的网络下提供基于云平台的远程设备的数据传输和监控。
以在centos 7.2中安装为例
在Red Hat 7 或 CentOS 7 上面 基于PackageCloud yum源安装rabbitmq
创建rabbitmq.repo 文件
vi /etc/yum.repos.d/rabbitmq.repo
##
## Zero dependency Erlang
##
[rabbitmq_erlang]
name=rabbitmq_erlang
baseurl=https://packagecloud.io/rabbitmq/erlang/el/7/$basearch
repo_gpgcheck=1
gpgcheck=1
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[rabbitmq_erlang-source]
name=rabbitmq_erlang-source
baseurl=https://packagecloud.io/rabbitmq/erlang/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
##
## RabbitMQ server
##
[rabbitmq_server]
name=rabbitmq_server
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/$basearch
repo_gpgcheck=1
gpgcheck=1
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[rabbitmq_server-source]
name=rabbitmq_server-source
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
install these dependencies from standard OS repositories
yum install socat logrotate -y
yum install erlang -y
yum install rabbitmq-server -y
#配置开机启动
chkconfig rabbitmq-server on
/sbin/service rabbitmq-server start
/sbin/service rabbitmq-server status
/sbin/service rabbitmq-server stop
# 开启管理插件
rabbitmq-plugins enable rabbitmq_management
#开启mqtt插件,也可同时开启 rabbitmq-plugins enable rabbitmq_web_mqtt
rabbitmq-plugins enable rabbitmq_mqtt
# 查看已经安装的插件,输出结果见下图
rabbitmq-plugins list
# 禁止xxx插件,如rabbitmq_mqtt
rabbitmq-plugins disable rabbitmq_mqtt
# 查看开启的插件,返回[rabbitmq_management,rabbitmq_mqtt].
more /etc/rabbitmq/enabled_plugins
用户权限配置请参考access-control
当服务器第一次开始运行,并检测到它的数据库未初始化或已被删除,它将使用以下资源初始化一个新的数据库
默认的Virtual Host and User如下:
为了安全和能远程登录管理页面,需要删除guest用户并创建新用户,用户的标签有5种:administrator ,monitoring,policymaker,management和none ,详见rabbitmq management
#查看用户列表:
rabbitmqctl list_users --formatter=json
# 删除guest用户
rabbitmqctl delete_user guest
#增加用户
rabbitmqctl add_user "admin" 根据提示输入密码,如输入20212021
#增加vhost, 根据需要执行,默认的vhost为/
rabbitmqctl add_vhost vnode22
# 给用户赋予权限
#https://www.rabbitmq.com/access-control.html
# First ".*" for configure permission on every entity
# Second ".*" for write permission on every entity
# Third ".*" for read permission on every entity
rabbitmqctl set_permissions -p "/" "admin" ".*" ".*" ".*"
# 给用户增加管理员标签
rabbitmqctl set_user_tags admin administrator
http://172.25.11.22:15672/#/
输入用户 admin / 20212021 进行登录,首页展示如下:
下载mqttx, 然后直接安装,安装后打开如下:
mqttx下载地址
新增加一个exchange,类型是: topic
可以看到,发送到mq中的消息,订阅者可以立即收到
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。