赞
踩
近期对消息队列的学习让我对消息中间件产生了兴趣,而作为消息队列的使用的rabbitmq,就是我学习中间件的第一步。
安装之前先检查环境:
[root@VM-12-4-centos ~]# cat /proc/version
Linux version 3.10.0-1160.66.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Wed May 18 16:02:34 UTC 2022
rabbitmq是基于Erlang语言编写的消息中间件,所以安装rabbitmq之前还需要安装erlang作为支撑
我们可以先把需要安装的包给准备好(要说明的是我安装的是centos7对应版本的rpm包)
erlang语言rpm包下载入口
如上找到el7对应的x86_64,因为我的云服务器是centos7的x86架构
rabbitmq的rpm包下载入口
如上下载压缩包,然后上传至云服务器,再进行安装
[root@VM-12-4-centos ~]# ls erlang-23.3.4.8-1.el7.x86_64.rpm rabbitmq-server-3.10.7-1.suse.noarch.rpm #安装erlang的依赖 [root@VM-12-4-centos ~]# yum install socat ...... Transaction test succeeded Running transaction Installing : socat-1.7.3.2-2.el7.x86_64 1/1 Verifying : socat-1.7.3.2-2.el7.x86_64 1/1 Installed: socat.x86_64 0:1.7.3.2-2.el7 Complete! #rpm安装前面下载的两个包 [root@VM-12-4-centos ~]# rpm -ivh erlang-23.3.4.8-1.el7.x86_64.rpm ...... [root@VM-12-4-centos ~]# rpm -ivh rabbitmq-server-3.10.7-1.suse.noarch.rpm ......
这就完成了安装了。
当然除了上面的方式外也可以直接在云服务器上面操作
[root@VM-12-4-centos ~]# wget https://github.com/rabbitmq/erlang-rpm/releases/download/v23.3.4.8/erlang-23.3.4.8-1.el7.x86_64.rpm ...... [root@VM-12-4-centos ~]# wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.10.7/rabbitmq-server-3.10.7-1.suse.noarch.rpm ...... #上面两个包的下载url是我从对应地址复制,然后直接用来wget的,不代表url一直不变动,所以还是要自己去复制 [root@VM-12-4-centos ~]# ls erlang-23.3.4.8-1.el7.x86_64.rpm rabbitmq-server-3.10.7-1.suse.noarch.rpm [root@VM-12-4-centos ~]# yum install socat ...... Transaction test succeeded Running transaction Installing : socat-1.7.3.2-2.el7.x86_64 1/1 Verifying : socat-1.7.3.2-2.el7.x86_64 1/1 Installed: socat.x86_64 0:1.7.3.2-2.el7 Complete! #rpm安装前面下载的两个包 [root@VM-12-4-centos ~]# rpm -ivh erlang-23.3.4.8-1.el7.x86_64.rpm ...... [root@VM-12-4-centos ~]# rpm -ivh rabbitmq-server-3.10.7-1.suse.noarch.rpm ......
启动服务并设置开机自启
[root@VM-12-4-centos ~]# systemctl start rabbitmq-server [root@VM-12-4-centos ~]# systemctl enable rabbitmq-server Created symlink from /etc/systemd/system/multi-user.target.wants/rabbitmq-server.service to /usr/lib/systemd/system/rabbitmq-server.service. [root@VM-12-4-centos ~]# systemctl status rabbitmq-server ● rabbitmq-server.service - RabbitMQ broker Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2022-08-16 10:58:38 CST; 1min 57s ago Main PID: 4261 (beam.smp) CGroup: /system.slice/rabbitmq-server.service ├─4261 /usr/lib64/erlang/erts-11.2.2.7/bin/beam.smp -W w -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -sbwt none -sbwtdcpu non... ├─4276 erl_child_setup 32768 ├─4301 /usr/lib64/erlang/erts-11.2.2.7/bin/epmd -daemon ├─4324 inet_gethost 4 └─4325 inet_gethost 4 ...... [root@VM-12-4-centos ~]# rabbitmqctl status Status of node rabbit@VM-12-4-centos ... Runtime ...... Plugins Enabled plugin file: /etc/rabbitmq/enabled_plugins Enabled plugins: Data directory Node data directory: /var/lib/rabbitmq/mnesia/rabbit@VM-12-4-centos Raft data directory: /var/lib/rabbitmq/mnesia/rabbit@VM-12-4-centos/quorum/rabbit@VM-12-4-centos Config files Log file(s) * /var/log/rabbitmq/rabbit@VM-12-4-centos.log * /var/log/rabbitmq/rabbit@VM-12-4-centos_upgrade.log * <stdout> ...... Listeners Interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication Interface: [::], port: 5
上面的status信息非常重要,注意config files字段和log files字段,关于自定义配置和运行日志,这两个字段信息是重点关注对象,然后是开启前台访问和添加访问用户
#添加admin用户配置密码为admin [root@VM-12-14-centos ~]# rabbitmqctl add_user admin admin Adding user "admin" ... Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more. # 设置admin用户为管理员 [root@VM-12-14-centos ~]# rabbitmqctl set_user_tags admin administrator Setting tags for user "admin" to [administrator] ... # 启用网页后台管理插件 [root@VM-12-4-centos ~]# rabbitmq-plugins enable rabbitmq_management Enabling plugins on node rabbit@VM-12-4-centos: rabbitmq_management The following plugins have been configured: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatch Applying plugin configuration to rabbit@VM-12-4-centos... The following plugins have been enabled: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatch started 3 plugins. [root@VM-12-4-centos ~]# rabbitmqctl status Status of node rabbit@VM-12-4-centos ... Runtime ...... Listeners Interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication Interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0 Interface: [::], port: 15672, protocol: http, purpose: HTTP API
上面搞定以后,其实就开启了网页访问功能了,但对于服务器来说,其实还需要开启防火墙的拦截,从上面我们的检车就可以看出来,提供服务的端口为5672端口,即rabbitmq提供给客户端的访问端口,以及15672端口,即后台管理界面ui端口,访问ui的url通常为:http://[ip]:15672/
# 检查firewalld运行状态和开放端口
[root@VM-12-14-centos ~]# firewall-cmd --state
running
[root@VM-12-14-centos ~]# firewall-cmd --permanent --zone=public --list-ports
20/tcp 21/tcp 22/tcp 80/tcp 8888/tcp 39000-40000/tcp 8099/tcp 443/tcp 15672/tcp 5672/tcp
从上面可以看到重要的15672端口和5672端口已经放开了,如果没放开,那就手动开放
[root@VM-12-14-centos ~]# firewall-cmd --permanent --zone=public --add-port=15672/tcp
success
[root@VM-12-14-centos ~]# firewall-cmd --permanent --zone=public --add-port=5672/tcp
success
firewalld开通了对应端口的访问以后,我们访问前台试试
可以看到访问失败,那我们检测一下端口的连通性
D:\Users\User\Desktop>ping x.x.x.x -t 正在 Ping x.x.x.x 具有 32 字节的数据: 来自 x.x.x.x 的回复: 字节=32 时间=13ms TTL=248 来自 x.x.x.x 的回复: 字节=32 时间=13ms TTL=248 来自 x.x.x.x 的回复: 字节=32 时间=13ms TTL=248 x.x.x.x 的 Ping 统计信息: 数据包: 已发送 = 3,已接收 = 3,丢失 = 0 (0% 丢失), 往返行程的估计时间(以毫秒为单位): 最短 = 13ms,最长 = 13ms,平均 = 13ms Control-C ^C D:\Users\User\Desktop>telnet x.x.x.x 15672 正在连接x.x.x.x...无法打开到主机的连接。 在端口 15672: 连接失败 D:\Users\User\Desktop>
可以看到,我们的ip访问当然是没问题的,但对应端口的连接是失败的,所以还是缺了配置,而我们用的是腾讯云服务器,所以还需要在控制台上面开放对应端口的访问
腾讯云服务器开放端口访问官方文档
具体就不加赘述了,照着文档来就ok了,插一句:如果确定服务准确开启了,但外部pc无法访问,往往先需要检查对端ip的连通性,然后是对应服务的开启端口的连通性检测,连通性没问题以后就考虑外部pc访问该服务是否兼容。
回归正题,当我们搞定防火墙的问题后,检测连通性,可行后我们重新访问前台服务
可行,输入前面添加的用户和密码就可登录成功
关于rabbitmq的远程服务我们已经开启并确认了可访问了,现在我们用python来操作一下,作为拥有丰富的第三方库和简单易用特性的python,操作mq可以说是特别适合的了。
首先安装操作rabbitmq的第三方库,注意要用python3对应pip,因为目前python2不进行维护了,作为第三方库的pika的安装和使用也是容易出问题的。
# 因为我是在windows下进行的python练习,它的python3对应的各种命令都是pip和python即可 D:\Users\User\Desktop>pip install pika 。。。。。。 D:\Users\User\Desktop>pip show pika Name: pika Version: 1.3.0 Summary: Pika Python AMQP Client Library Home-page: None Author: None Author-email: None License: BSD-3-Clause Location: d:\software\python38\lib\site-packages Requires: Required-by: D:\Users\User\Desktop>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import pika >>> cred = pika.PlainCredentials('admin', 'admin') >>> connection = pika.BlockingConnection(pika.ConnectionParameters(host='x.x.x.x',credentials=cred)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\software\python38\lib\site-packages\pika\adapters\blocking_connection.py", line 360, in __init__ self._impl = self._create_connection(parameters, _impl_class) File "D:\software\python38\lib\site-packages\pika\adapters\blocking_connection.py", line 451, in _create_connection raise self._reap_last_connection_workflow_error(error) pika.exceptions.ProbableAccessDeniedError: ConnectionClosedByBroker: (530) "NOT_ALLOWED - access to vhost '/' refused for user 'admin'"
使用python比较重要的一点是学会看报错的堆栈信息,比如上面就说的很清楚了,admin用户没有针对"/"这一vhost的访问权限,所以得添加权限
# centos服务器
[root@VM-12-4-centos ~]# rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/" ...
# 本地PC,承接上面python使用
>>> connection = pika.BlockingConnection(pika.ConnectionParameters(host='114.132.160.125',credentials=cred))
>>>
生产者和消费者是很典型的设计模式的典型,对于消息系统来说,实现这个设计模式的简单实例作为测试再好不过。
在windows上面新开两个终端,一个作为生产者,一个作为消费者
D:\Users\User\Desktop>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> >>> >>> print("生产者") 生产者 >>> import pika >>> cred = pika.PlainCredentials("admin", "admin") #进行连接 >>> connection = pika.BlockingConnection(pika.ConnectionParameters("x.x.x.x", credentials=cred, heartbear=0)) >>> channel = connection.channel() # 创建队列 >>> channel.queue_declare(queue='hello') <METHOD(['channel_number=1', 'frame_type=1', "method=<Queue.DeclareOk(['consumer_count=0', 'message_count=0', 'queue=hello'])>"])> # 往队列里面生产一条'test from queue of the rabbitmq'的信息 >>> channel.basic_publish(exchange='', routing_key='hello', body='test from queue of the rabbitmq') >>>
上面进行了"产品"的生产后,后台查看信息队列情况
[root@VM-12-14-centos ~]# rabbitmqctl list_queues
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
name messages
hello 1
[root@VM-12-14-centos ~]#
另外在前台可以更加直观地看到队列的信息,这里就不展示了
>>>
>>> print("消费者")
消费者
>>>
>>> def callf(ch, method, properties, mes):
... print("Got {}".format(mes))
...
>>> print('Waiting for messages. To exit press CTRL+C')
Waiting for messages. To exit press CTRL+C
# 连接上hello通道,并连接处理函数
>>> channel.basic_consume('hello', callf, True)
'ctag1.e2826632074d436284b2b52096ab0dd2'
# 开始消费
>>> channel.start_consuming()
Got b'test from queue of the rabbitmq'
上面的生产消费是一一对应的,生产者调用basic_publish方法往通道里面存放一条信息,消费者持续监听通道,来一条信息就对应调用一次处理函数callf,这就是经典的生产者消费者,这也是最朴素的应用,但实际上的应用环境并不会这么一板一眼,简简单单。
来到后台查看通道,我们可以查看下面信息
[root@VM-12-14-centos ~]# rabbitmqctl list_queues
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
name messages
hello 0
[root@VM-12-14-centos ~]#
在此之前,首先你要安装了docker并且开放了对应访问端口15672和5672
[root@VM-12-14-centos ~]# docker run -d --hostname localhost --name rabbit-management --restart=always -p 15672:15672 -p 5672:5672 rabbitmq:3.6-management-alpine
......
[root@VM-12-14-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
784571a3bbef rabbitmq:3.6-management-alpine "docker-entrypoint..." 5 minutes ago Up 5 minutes 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbit-management
如上,嗯,docker有点香,哈哈哈
上面就是我的一次关于rabbitmq在腾讯云服务器的配置以及远程调用记录。I’m Jack, over.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。