tarjima

RabbitMQ haqida, ishga tushirish va python'da ishlash

RabbitMQ haqida, ishga tushirish va python'da ishlash

RabbitMQ - xabarlar menjeri bo'lib, u Erlang dasturlash tilida yozilgan. U bir nechta xizmat(servis)lar o'rtasida ma'lumotlarni yuborish(xabarlar) uchun mo'ljallangan: bitta xizmat navbatga xabar joylaydi, boshqa xizmat esa o'sha xabarni qabul qiladi.

Quyida RabbitMQ'ni o'rnatish, uni ishga tushirish va Python dasturlash tili yordamida RabbitMQ bilan ishlash ko'rsatilgan.

O'rnatish

Arch Linux'ga o'rnatish uchun terminaldan quyidagi buyruq beriladi:

sudo pacman -S rabbitmq

Debian/Ubuntuga o'rnatish uchun terminaldan quyidagi buyruq beriladi:

apt install rabbitmq-server

Yoki bo'lmasa, Docker orqali ishga tushirish quyidagicha:

docker run docker pull rabbitmq

RabbitMQ rabbit-plugins yordamida plagin(plugins)lar bilan ishlash imkoni beradi.

Faol plaginlarni ko'rish uchun quyidagi buyruq beriladi:

admin@ip-172-31-33-210:~$ sudo rabbitmq-plugins list
Configured: E = explicitly enabled; e = implicitly enabled
| Status:   * = running on rabbit@ip-172-31-33-210
|/
[  ] amqp_client                       3.6.6
[  ] cowboy                            1.0.3
[  ] cowlib                            1.0.1
[  ] mochiweb                          2.13.1
...

rabbitmq_management plaginini faollashtiramiz:

admin@ip-172-31-33-210:~$ sudo rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@ip-172-31-33-210... started 6 plugins.

Faol plaginlar /etc/rabbitmq/enabled_plugins faylida saqlanadi:

root@ip-172-31-33-210:/home/admin# cat /etc/rabbitmq/enabled_plugins
[rabbitmq_management].

HTTP API

rabbitmq_management plagini RabbitMQ'dan API orqali 15672 portda foydalanish imkonini beradi:

root@ip-172-31-33-210:/home/admin# netstat -anp | grep 15672
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      1348/beam

So'rovga misol:

root@ip-172-31-33-210:/home/admin# curl -i -u guest:guest localhost:15672/api/overview
HTTP/1.1 200 OK
vary: Accept-Encoding, origin
Server: MochiWeb/1.1 WebMachine/1.10.0 (never breaks eye contact)
Date: Tue, 05 Jun 2018 11:05:57 GMT
Content-Type: application/json
Content-Length: 1973
Cache-Control: no-cache
{"management_version":"3.6.6","rates_mode":"basic","exchange_types":[{"name":"direct","description":"AMQP direct exchange, as per the AMQP specification","enabled":true},{"name":"topic","description":"AMQP topic exchange, as per the AMQP specification","enabled":true},{"name":"fanout","description":"AMQP fanout exchange, as per the AMQP specification","enabled":true},{"name":"headers","description":"AMQP headers exchange, as per the AMQP specification","enabled":true}],"rabbitmq_version":"3.6.6","cluster_name":"rabbit@ip-172-31-33-210.us-east-2.compute.internal","erlang_version":"19.2.1","erlang_full_version":"Erlang/OTP 19 [erts-8.2.1] [source] [64-bit] [async-threads:64] [kernel-poll:true]","message_stats":{"deliver":0,"deliver_details":{"rate":0.0},"deliver_no_ack":0,"deliver_no_ack_details":{"rate":0.0},"get":1,"get_details":{"rate":0.0},"get_no_ack":0,"get_no_ack_details":{"rate":0.0},"publish":0,"publish_details":{"rate":0.0},"publish_in":0,"publish_in_details":{"rate":0.0},"publish_out":0,"publish_out_details":{"rate":0.0},"ack":0,"ack_details":{"rate":0.0},"deliver_get":1,"deliver_get_details":{"rate":0.0},"confirm":0,"confirm_details":{"rate":0.0},"return_unroutable":0,"return_unroutable_details":{"rate":0.0},"redeliver":0,"redeliver_details":{"rate":0.0}},"queue_totals":{"messages":1,"messages_details":{"rate":0.0},"messages_ready":1,"messages_ready_details":{"rate":0.0},"messages_unacknowledged":0,"messages_unacknowledged_details":{"rate":0.0}},"object_totals":{"consumers":0,"queues":1,"exchanges":8,"connections":0,"channels":0},"statistics_db_event_queue":0,"node":"rabbit@ip-172-31-33-210","statistics_db_node":"rabbit@ip-172-31-33-210","listeners":[{"node":"rabbit@ip-172-31-33-210","protocol":"amqp","ip_address":"::","port":5672},{"node":"rabbit@ip-172-31-33-210","protocol":"clustering","ip_address":"::","port":25672}],"contexts":[{"node":"rabbit@ip-172-31-33-210","description":"RabbitMQ Management","path":"/","port":"15672"}]}

rabbitmqadmin pythondan yaratilgan dasturiy uskuna bo'lib, HTTP API yordamida turli xil operatsiyalar bajarish imkonini beradi, masalan, navbatlarni ko'rish, almashish nuqtalarni kuzatish, foydalanuvchilarni tekshirish va boshqalar. U rabbitmq_management plaginini talab qiladi.

rabbitmqctl

Bu plagin asosan klasterdagi nodlarni boshqarish uchun qo'llaniladi - yangi qo'shish, o'chirish, qayta ishga tushirish va loglarni boshqarish uchun.

Shunga o'xshash rabbitmqadmin plagini foydalanuvchilarni, navbatlarni, almashinuv nuqtalarini boshqarish uchun ishlatilinadi.

root@ip-172-31-33-210:/home/admin# rabbitmqctl status
Status of node 'rabbit@ip-172-31-33-210' ...
[{pid,1348},
{running_applications,
[{rabbitmq_management,"RabbitMQ Management Console","3.6.6"},
{rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.6.6"},
{webmachine,"webmachine","1.10.3"},
{mochiweb,"MochiMedia Web Server","2.13.1"},
{rabbitmq_management_agent,"RabbitMQ Management Agent","3.6.6"},

Foydalanuvchilarni ko'rish:

root@ip-172-31-33-210:/home/admin# rabbitmqctl list_users
Listing users ...
guest   [administrator]

Web-UI

Plagin faollashtirilganidan so'ng, veb interfeys orqali unga ulanish mumkin: localhost:15672


guest foydalanuvchi taqiqlangan. Shu sababli yangi foydalanuvchi qo'shib olamiz:

root@ip-172-31-33-210:/home/admin# rabbitmqctl add_user test test
Creating user "test" ...

Uni administrator qilib belgilaymiz:

root@ip-172-31-33-210:/home/admin# rabbitmqctl set_user_tags test administrator
Setting tags for user "test" to [administrator] ...

Va unga barcha huquqlarni beramiz:

root@ip-172-31-33-210:/home/admin# rabbitmqctl set_permissions -p / test ".*" ".*" ".*"
Setting permissions for user "test" in vhost "/" ...

RabbitMQ ni ichiga kiramiz:

RabbitMQ'ning 4 ta muhim tushunchasi bor:

  • producer - mijoz, u xabarlarni yubadi;
  • queue - xabarlar navbati;
  • consumer - mijoz, u navbatdan xabarlarni oladi;
  • exchange - u producerdan xabarlarni oladi, xabar turiga qarab uni kerakli navbatga yuboradi.

Misol.

RabbitMQ bilan ishlash uchun AMQP (Advanced Message Queuing Protocol) kerak bo'ladi. Python'da u bilan pika, py-amqplib va boshqa kutubxonalar yordamida ishlash mumkin.

pika kutubxonasini o'rnatamiz:

apt install python-pika

Xabar yuborish

producer uchun skript yozamiz, u navbatga xabar yuboradi:

#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
                     routing_key='hello',
                     body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

Bu yerda quyidagilar bajariladi:

  1. localhost'dagi rabbitmq serverga bog'lanish - connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
  2. hello nomli navbat yaratiladi - queue_declare(queue='hello')
  3. Xabar almashinuvi nuqtasi(exchange) orqali navbatga hello(routing_key='hello') xabar yuboriladi -
  4. Server bilan bog'lanish yopiladi - connection.close()

Skriptni ishga tushiramiz

admin@ip-172-31-33-210:~$ ./producer.py
[x] Sent 'Hello World!'

rabbitmqadmin orqali tekshiramiz

root@ip-172-31-33-210:/home/admin# rabbitmqadmin get queue='hello'
+-------------+----------+---------------+--------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count |   payload    | payload_bytes | payload_encoding | properties | redelivered |
+-------------+----------+---------------+--------------+---------------+------------------+------------+-------------+
| hello       |          | 0             | Hello World! | 12            | string           |            | False       |
+-------------+----------+---------------+--------------+---------------+------------------+------------+-------------+

rabbitmqctl buyrug'i orqali navbatlar ro'yxatini tekshiramiz:

root@ip-172-31-33-210:/home/admin# rabbitmqctl list_queues
Listing queues ...
hello   1

hello navbatida 1 dona xabar mavjud. Uni qabul qilamiz.

Navbatdan xabarni o'qib olamiz:

Ikkinchi skript consumer.py - navbatdan xabarni qabul qiladi va uni ekranga chiqaradi:

#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

Bu yerda quyidagilar bajariladi:

  1. Birinchi skript kabi rabbitmq bilan bog'lanamiz;
  2. Agar hello nomli navbat bo'lmasa, u yaratilinadi;
  3. callback() funksiyasini yaratamiz, u xabarni qabul qiladi va uni ekranga chiqaradi;
  4. Uni basic_consumer() ni chaqiramiz, unga navbat nomi yuboramiz;
  5. Cheksiz siklni ishga tushiramiz start_consumer().

Ishga tushiramiz:

root@ip-172-31-33-210:/home/admin# ./consumer.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!'

Navbatni tekshiramiz, endi u bo'sh!

root@ip-172-31-33-210:/home/admin# rabbitmqctl list_queues
Listing queues ...
hello   0
Manba:


WinStyle - Texnoman foydalanuvchisi

Muallif haqida

WinStyle Java, Javascript, C++, MySql, NoSql, Oracle, Raspberry


Qiziq bo‘ladi:


Birinchi bo‘ling!

Iltimos, fikr bildirish uchun saytga kiring yoki ro‘yxatdan o‘ting!