Introducción
RabbitMQ es un esquema de mensajería empresarial de código abierto que inicialmente se basa en el estándar AMQP (Advanced Messaging Queuing Protocol), pero ahora es compatible con muchos otros protocolos. Ligero y fácil de implementar en las instalaciones o en la nube, RabbitMQ es la mensajería de código abierto más popular. Para garantizar un rendimiento óptimo de RabbitMQ, es fundamental el monitoreo de RabbitMQ. Sin embargo, es un desafío para las organizaciones encontrar herramientas de monitoreo de RabbitMQ en clúster que brinden una visibilidad completa de su infraestructura. Además, consulte RabbitMQ en Centos8 y la documentación del sitio de rabbitmq
¿Qué puede hacer RabbitMQ?
Confiabilidad
Por lo tanto, RabbitMQ ofrece una variedad de funciones que le permiten intercambiar rendimiento con confiabilidad, incluida la persistencia, el soporte de entrega, las confirmaciones del editor y la alta disponibilidad.
Enrutamiento flexible
Los mensajes se enrutan a través de los intercambios antes de llegar a las colas.
Además, RabbitMQ presenta varios tipos de intercambio incorporados para la lógica de enrutamiento típica.
Además, para un enrutamiento más complejo, puede unir los intercambios o incluso escribir su propio tipo de intercambio como un complemento.
Agrupación de RabbitMQ
Se pueden agrupar varios servidores RabbitMQ en una red local, formando un único intermediario lógico.
Federación
Para los servidores que necesitan estar conectados de manera menos flexible y poco confiable de lo que permite el agrupamiento, RabbitMQ ofrece un modelo de federación.
Colas altamente disponibles
Por lo tanto, las colas se pueden duplicar en varias máquinas en un clúster, lo que garantiza que, incluso en caso de falla del hardware, sus mensajes estén seguros.
Multiprotocolo
RabbitMQ admite la mensajería a través de una variedad de protocolos de mensajería.
Muchos Clientes Agrupados
También hay clientes de RabbitMQ para casi cualquier idioma que se te ocurra.
IU de administración
RabbitMQ se envía con una interfaz de usuario de administración fácil de usar que le permite monitorear y controlar todos los aspectos de su agente de mensajes.
Requisitos
- El requisito esencial para la configuración de RabbitMQ Cluster es tener un mínimo de 2 nodos con CentOS 7 que usaremos en este artículo. Luego, configure su FQDN y la configuración de red básica
- Al menos dos instancias de CentOS 7 recién usadas y actualizadas en la misma subred con redes privadas habilitadas
- RabbitMQ instalado con la consola de administración habilitada en cada servidor.
- Un usuario no administrador con derechos sudo
Instalación de RabbitMQ
Instale el repositorio Epel y el paquete Clustered RabbitMQ
[root@master ~]# yum install epel-release -y
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.icidc.com
* extras: mirrors.icidc.com
* updates: mirrors.icidc.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 242 kB 00:00:00
(2/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:02
(4/4): updates/7/x86_64/primary_db | 9.6 MB 00:00:05
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================================================================
Installing:
epel-release noarch 7-11 extras 15 k
Transaction Summary
==================================================================================================================================================================================================================
Install 1 Package
Total download size: 15 k
Installed size: 24 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for epel-release-7-11.noarch.rpm is not installed
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-8.2003.0.el7.centos.x86_64 (@anaconda)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release-7-11.noarch 1/1
Verifying : epel-release-7-11.noarch 1/1
Installed:
epel-release.noarch 0:7-11
Complete!
[root@master ~]#
Instale también Clustered RabbitMQ y los paquetes necesarios
[root@master ~]# yum -y install rabbitmq-server erlang
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 8.4 kB 00:00:00
* base: mirrors.icidc.com
* epel: download.nus.edu.sg
* extras: mirrors.icidc.com
* updates: mirrors.icidc.com
epel | 4.7 kB 00:00:00
(1/3): epel/x86_64/group_gz | 96 kB 00:00:00
(2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/3): epel/x86_64/primary_db | 6.9 MB 00:00:01
Resolving Dependencies
--> Running transaction check
---> Package rabbitmq-server.noarch 0:3.3.5-34.el7 will be installed
--> Processing Dependency: erlang-erts >= R12B-3 for package: rabbitmq-server-3.3.5-34.el7.noarch
--> Processing Dependency: erlang-kernel >= R12B-3 for package: rabbitmq-server-3.3.5-34.el7.noarch
--> Processing Dependency: erlang-mnesia >= R12B-3 for package: rabbitmq-server-3.3.5-34.el7.noarch
--> Processing Dependency: erlang-os_mon >= R12B-3 for package: rabbitmq-server-3.3.5-34.el7.noarch
......................
Dependencies Resolved
==================================================================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================================================================
Installing:
rabbitmq-server noarch 3.3.5-34.el7 epel 4.0 M
Installing for dependencies:
erlang-asn1 x86_64 R16B-03.18.el7 epel 758 k
erlang-compiler x86_64 R16B-03.18.el7 epel 1.0 M
erlang-crypto x86_64 R16B-03.18.el7 epel 86 k
erlang-erts x86_64 R16B-03.18.el7 epel 2.5 M
erlang-hipe x86_64 R16B-03.18.el7 epel 2.4 M
293 k
erlang-sd_notify x86_64 0.1-1.el7 epel 9.1 k
erlang-snmp x86_64 R16B-03.18.el7 epel 1.5 M
erlang-ssl x86_64 R16B-03.18.el7 epel 588 k
erlang-stdlib x86_64 R16B-03.18.el7 epel 2.2 M
erlang-syntax_tools x86_64 R16B-03.18.el7 epel 336 k
erlang-tools x86_64 R16B-03.18.el7 epel 566 k
erlang-xmerl x86_64 R16B-03.18.el7 epel 998 k
lksctp-tools x86_64 1.0.17-2.el7 base 88 k
Transaction Summary
==================================================================================================================================================================================================================
Install 1 Package (+21 Dependent packages)
Total download size: 21 M
Installed size: 36 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/erlang-asn1-R16B-03.18.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY ] 0.0 B/s | 624 kB --:--:-- ETA
Public key for erlang-asn1-R16B-03.18.el7.x86_64.rpm is not installed
(1/22): erlang-asn1-R16B-03.18.el7.x86_64.rpm | 758 kB 00:00:00
.......... 4/22
Verifying : erlang-compiler-R16B-03.18.el7.x86_64 5/22
Verifying : erlang-mnesia-R16B-03.18.el7.x86_64 6/22
Verifying : erlang-crypto-R16B-03.18.el7.x86_64 7/22
Verifying : erlang-hipe-R16B-03.18.el7.x86_64 8/22
Verifying : erlang-inets-R16B-03.18.el7.x86_64
Verifying : erlang-os_mon-R16B-03.18.el7.x86_64 17/22
Verifying : erlang-otp_mibs-R16B-03.18.el7.x86_64 18/22
Verifying : erlang-tools-R16B-03.18.el7.x86_64 19/22
Verifying : erlang-snmp-R16B-03.18.el7.x86_64 20/22
Verifying : erlang-syntax_tools-R16B-03.18.el7.x86_64 21/22
Verifying : erlang-asn1-R16B-03.18.el7.x86_64 22/22
Installed:
rabbitmq-server.noarch 0:3.3.5-34.el7
Dependency Installed:
erlang-asn1.x86_64 0:R16B-03.18.el7 erlang-compiler.x86_64 0:R16B-03.18.el7 erlang-crypto.x86_64 0:R16B-03.18.el7 erlang-erts.x86_64 0:R16B-03.18.el7
erlang-hipe.x86_64 0:R16B-03.18.el7 erlang-inets.x86_64 0:R16B-03.18.el7 erlang-kernel.x86_64 0:R16B-03.18.el7 erlang-mnesia.x86_64 0:R16B-03.18.el7
erlang-os_mon.x86_64 0:R16B-03.18.el7 erlang-otp_mibs.x86_64 0:R16B-03.18.el7 erlang-public_key.x86_64 0:R16B-03.18.el7 erlang-runtime_tools.x86_64 0:R16B-03.18.el7
erlang-sasl.x86_64 0:R16B-03.18.el7 erlang-sd_notify.x86_64 0:0.1-1.el7 erlang-snmp.x86_64 0:R16B-03.18.el7 erlang-ssl.x86_64 0:R16B-03.18.el7
erlang-stdlib.x86_64 0:R16B-03.18.el7 erlang-syntax_tools.x86_64 0:R16B-03.18.el7 erlang-tools.x86_64 0:R16B-03.18.el7 erlang-xmerl.x86_64 0:R16B-03.18.el7
lksctp-tools.x86_64 0:1.0.17-2.el7
Complete!
[root@master ~]#
Iniciar y habilitar el servicio
[root@master ~]# systemctl start rabbitmq-server
[root@master ~]# 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.
Agregar usuario para RabbitMQ
Use RabbitMQ, agregue el usuario primero. De forma predeterminada, solo existe un usuario invitado y puede conectarse al único host local
[root@master ~]# rabbitmqctl add_user rabbit password
Creating user "rabbit" ...
...done.
[root@master ~]# rabbitmqctl list_users
Listing users ...
guest [administrator]
rabbit []
...done.
[root@master ~]#
Otorgar derechos de administrador al usuario rabbitmq
Otorgar permisos a un usuario para hosts virtuales
[root@master ~]# rabbitmqctl set_user_tags rabbit administrator
Setting tags for user "rabbit" to [administrator] ...
...done.
[root@master ~]#
Añadir host virtual a RabbitMQ en clúster
Use RabbitMQ, agregue hosts virtuales
[root@master ~]# rabbitmqctl add_vhost /my_vhost
Creating vhost "/my_vhost" ...
...done.
[root@master ~]# rabbitmqctl list_vhosts
Listing vhosts ...
/
/my_vhost
...done.
[root@master ~]#
Instalar biblioteca cliente AMQP
Implementa AMQP (Advanced Message Queuing Protocol) utilizado para Clustered RabbitMQ
[root@master ~]# yum -y install python2-pika
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.rise.ph
* epel: mirror.telkomuniversity.ac.id
* extras: mirror.rise.ph
* updates: mirror.rise.ph
Resolving Dependencies
--> Running transaction check
---> Package python2-pika.noarch 0:0.10.0-10.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================================================================
Installing:
python2-pika noarch 0.10.0-10.el7 epel 170 k
Transaction Summary
==================================================================================================================================================================================================================
Install 1 Package
Total download size: 170 k
Installed size: 1.0 M
Downloading packages:
python2-pika-0.10.0-10.el7.noarch.rpm | 170 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : python2-pika-0.10.0-10.el7.noarch 1/1
Verifying : python2-pika-0.10.0-10.el7.noarch 1/1
Installed:
python2-pika.noarch 0:0.10.0-10.el7
Complete!
[root@master ~]#
Crear script de python para probar la conexión
Envío de un mensaje en Python Clustered RabbitMQ
[root@master ~]# cat send_msg.py
#!/usr/bin/env python
import pika
credentials = pika.PlainCredentials('unixcop', 'password')
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost',
5672,
'/my_vhost',
credentials))
channel = connection.channel()
channel.queue_declare(queue='Hello_World')
channel.basic_publish(exchange='',
routing_key='Hello_World',
body='Hello RabbitMQ World!')
print(" [x] Sent 'Hello_World'")
connection.close()
[root@master ~]#
[root@master ~]# python send_msg.py
[x] Sent 'Hello_World'
[root@master ~]#
Crear secuencia de comandos para comprobar si se ha recibido el mensaje
[root@master ~]# cat receive_msg.py
#!/usr/bin/env python
import signal
import pika
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
signal.signal(signal.SIGINT, signal.SIG_DFL)
credentials = pika.PlainCredentials('unixcop', 'password')
connection = pika.BlockingConnection(pika.ConnectionParameters(
'master.example.com',
5672,
'/my_vhost',
credentials))
channel = connection.channel()
channel.queue_declare(queue='Hello_World')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(callback,
queue='Hello_World',
no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
[root@master ~]#
[root@master ~]# python receive_msg.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello RabbitMQ World!'
[x] Received 'Hello RabbitMQ World!'
^C
Establezca la cookie erlang en todos los nodos para Clustered RabbitMQ
[root@master ~]# chmod 777 /var/lib/rabbitmq/.erlang.cookie
[root@master ~]# scp -r /var/lib/rabbitmq/.erlang.cookie root@worker1:/var/lib/rabbitmq/.erlang.cookie
[root@master ~]# scp -r /var/lib/rabbitmq/.erlang.cookie root@worker2:/var/lib/rabbitmq/.erlang.cookie
[root@master ~]# chmod 400 /var/lib/rabbitmq/.erlang.cookie
[root@master ~]# chown rabbitmq /var/lib/rabbitmq/.erlang.cookie
[root@master ~]# chgrp rabbitmq /var/lib/rabbitmq/.erlang.cookie
Unir nodos a Clustered RabbitMQ
[root@worker1~]# rabbitmqctl stop_app
[root@worker1 ~]# rabbitmqctl join_cluster rabbit@master
[root@worker1 ~]# rabbitmqctl start_app
#---------------------------------------------------------------------------------#
[root@worker2 ~]# rabbitmqctl stop_app
[root@worker2 ~]# rabbitmqctl join_cluster rabbit@master
[root@worker2 ~]# rabbitmqctl start_app
Instalar complemento de gestión RabbitMQ
[root@master ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.
[root@master ~]# systemctl restart rabbitmq-server
Acceda a "http://nombre de host o dirección IP):15672/" de un cliente, luego, se expone el formulario de inicio de sesión de RabbitMQ, inicie sesión con un usuario administrador.
Tablero con nodos de clúster