赞
踩
一、安装相关软件
1、安装 gcc-c++
yum install gcc-c++
2、安装cmake
yum install cmake
3、安装openssl-devel
yum install openssl-devel
二、安装mosquitto
1、下载并解压mosquitto
wget http://mosquitto.org/files/source/mosquitto-1.4.10.tar.gz --no-check-certificate
tar -xzvf mosquitto-1.4.10.tar.gz
2、修改mosquitto的配置
打开mosquitto-1.4.10文件夹下的config.mk文件将里面的WITH_SRV:=yes和WITH_UUID:=yes都用#号注释掉
3、安装
make
sudo make install
三、配置mosquitto
1、创建用户
sudo groupadd mosquitto
sudo useradd -g mosquitto mosquitto
2、创建配置文件
mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
3、关闭匿名用户登录
打开mosquitto.conf文件( /etc/mosquitto/mosquitto.conf ),找到allow_anonymous节点,这个节点作用是,是否开启匿名用户登录。去掉前面的#,改为false。
4、设置用户密码文件路径
找到#password_file节点,这个节点是告诉服务器你要配置的用户将存放在哪里。打开此配置并指定pwfile.example文件路径(注意是绝对路径),在669行左右。
password_file /etc/mosquitto/pwfile 或者 /pwfile.example
5、配置topic和用户
找到 #acl_file节点进行配置 acl_file /etc/mosquitto/aclfile.example
6、添加用户
终端输入,最后面的是用户名,之后自动弹出密码和确认密码,输入即可。(注意第二次创建用户时不用加 -c 如果加 -c 会把第一次创建的用户覆盖。)
mosquitto_passwd -c /etc/mosquitto/pwfile admin203
然后进入到/etc/mosquitto/mosquitto.conf 的# Authentication and topic access plugin options 下将下面两个用户名和密码加上。
# -----------------------------------------------------------------
# Authentication and topic access plugin options
# -----------------------------------------------------------------
# If the auth_plugin option above is used, define options to pass to the
# plugin here as described by the plugin instructions. All options named
# using the format auth_opt_* will be passed to the plugin, for example:
#
# auth_opt_db_host
# auth_opt_db_port
auth_opt_db_username admin203
auth_opt_db_password admin203
7、添加Topic和用户的关系
编辑/etc/mosquitto/aclfile.example文件在末尾添加以下配置
user admin203
topic write saleAuction/#
user admin203
topic read saleAuction/#
四、启动服务器
mosquitto -c /etc/mosquitto/mosquitto.conf -d
五、接收消息
mosquitto_sub -h localhost -t saleAuction/448180 -u admin203 -P admin203
如果报以下错误
mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
依次在mosquitto安装文件夹所有目录中依次执行以下命令
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
sudo ldconfig
如果报以下错误
Connection Refused: not authorised.
可进行以下操作
mosquitto_passwd /etc/mosquitto/pwfile.example admin203 #重置用户admin203的密码
并确保配置文件mosquitto.conf中password_file /etc/mosquitto/pwfile.example与重置名字时的密码文件路径名称一致。
六、发送信息
mosquitto_pub -h localhost -t saleAuction/448180 -u admin203 -P admin203 -m "testjkhjkh"
七、客户端收到以下消息
[root@yuanq1 ~]# mosquitto_sub -h localhost -t saleAuction/448180 -u admin203 -P admin203
testjkhjkh
八、集群搭建
在安装多台mqtt服务器,从服务器配置不用动,只修改主服务器的Bridges节点
# ================================================================= # Bridges # ================================================================= # A bridge is a way of connecting multiple MQTT brokers together. # Create a new bridge using the "connection" option as described below. Set # options for the bridges using the remaining parameters. You must specify the # address and at least one topic to subscribe to. # Each connection must have a unique name. # The address line may have multiple host address and ports specified. See # below in the round_robin description for more details on bridge behaviour if # multiple addresses are used. # The direction that the topic will be shared can be chosen by # specifying out, in or both, where the default value is out. # The QoS level of the bridged communication can be specified with the next # topic option. The default QoS level is 0, to change the QoS the topic # direction must also be given. # The local and remote prefix options allow a topic to be remapped when it is # bridged to/from the remote broker. This provides the ability to place a topic # tree in an appropriate location. # For more details see the mosquitto.conf man page. # Multiple topics can be specified per connection, but be careful # not to create any loops. # If you are using bridges with cleansession set to false (the default), then # you may get unexpected behaviour from incoming topics if you change what # topics you are subscribing to. This is because the remote broker keeps the # subscription for the old topic. If you have this problem, connect your bridge # with cleansession set to true, then reconnect with cleansession set to false # as normal. #connection <name> #address <host>[:<port>] [<host>[:<port>]] #topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix] connection slave1 slave2 address 192.168.5.116:1883 192.168.5.119:1883 topic # both 0 remote_username admin203 remote_password admin203
connect:节上名称,自定义
adddress:ip加端口
topic:消息主题#指所有主题
remote_username 远程账号
emote_password 远程密码
如果节点服务器密码不一样,可以分开设置
connection slave1
address 192.168.5.116:1883
topic # both 0
remote_username admin203
remote_password admin203
connection slave2
address 192.168.5.119:1883
topic # both 0
remote_username admin203
remote_password admin203
九、如果客户端连接不上mqtt服务器可关闭防火墙
systemctl stop firewalld
或用以下命令配置防火墙
iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 1883 -j ACCEPT
firewall-cmd --zone=public --add-port=1883/tcp --permanent
firewall-cmd --reload
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。