오늘은 메시지 큐 미들웨어 중 하나인 RabbitMQ에 대해 내용을 정리하고자 한다.
RabbitMQ란?
RabbitMQ는 오픈 소스 메시지 브로커 소프트웨어(메시지 지향 미들웨어)로서, AMQP를 구현하였으며 그 이후로 STOMP ,MQTT 등의 프로토콜을 지원하기 위해 플러그인 구조와 함께 확장되고 있다.
메시지를 생산하는 생산자(Producer)가 메시지를 큐에 저장해 두면, 메시지를 수신하는 소비자(Consumer)가 메시지를 가져와 처리하는 Publish/Subscribe 방식의 메시지 전달 브로커이다.
RabbitMQ Routing Diagram
1) 메시지는 exchange로 publish된다.
2) exchange는 binding에 따라 메시지를 queue로 분배한다.
3.1) queue로 전달된 메시지는 consumers에게 배달되거나 (consumer가 subscribe했을 경우)
3.2) consumer가 queue에서 메시지를 필요할 때마다 가져갈 수 있다.
즉,
- Exchange : 우체국의 역할
- Routes : 메시지 분류 작업, binding이라고 불림
아래 캡쳐를 보면 Binding이 어떻게 이뤄지는 지 쉽게 확인이 가능하다.
RabbitMQ 구성하기
RabbitMQ는 Docker 이미지로 제공되고 있기 때문에, 손쉽게 구성이 가능하다.
# 바로 실행해보기, Management Console은 15672 Port로 접근하면 된다.
docker run -d --hostname my-rabbit --name rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq:3-management
혹은, 아래와 같이 Docker Compose로 구성도 가능하다. 필요하신 분들이 있다면 참조하시면 되겠다.
rabbitmq.conf
# ======================================
# RabbitMQ broker section
# ======================================
## Example config : https://github.com/rabbitmq/rabbitmq-server/blob/v3.8.x/deps/rabbit/docs/rabbitmq.conf.example
## Related doc guide: https://rabbitmq.com/configure.html. See
## https://rabbitmq.com/documentation.html for documentation ToC.
## Networking
## ====================
##
listeners.tcp.default = 5672
management.tcp.port = 15672
##
## Security, Access Control
## ==============
##
loopback_users.guest = false
##
## Default User / VHost
## ====================
##
default_vhost= /
default_user = user
default_pass = password
# =======================================
# Management section
# =======================================
management.load_definitions = /etc/rabbitmq/my_definition.json
my_definition.json
definition은 위의 Docker 명령어를 통해 실행시킨 후, User, Exchange, Queue 등을 구성한 뒤 Export하여 구성하는 게 훨씬 편하다. (혹은 cli를 사용하시거나 직접 만들셔도 문제되지는 않습니다.)
docker-compose.yml
services:
rabbitmq:
image: 'rabbitmq:3-management'
ports:
# The standard AMQP protocol port
- '5672:5672'
# HTTP management UI
- '15672:15672'
volumes:
- ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
- ./my_definition.json:/etc/rabbitmq/my_definition.json
위 파일을 모두 동일 Directory에 만든 뒤, docker-compose up 명령어를 통해 실행시킨다.
Python Tutorial 따라해보기
RabbitMQ 공식사이트에 Python 튜토리얼 문서가 있다. 해당 문서를 참조해서 진행하면 수월하게.. 진행되지는 않고, receiver.py를 일부 변경해주어야 한다. 그건 이 글을 참조!
※ 참고문서
www.rabbitmq.com/definitions.html
Schema Definition Export and Import — RabbitMQ
Schema Definition Export and Import Nodes and clusters store information that can be thought of schema, metadata or topology. Users, vhosts, queues, exchanges, bindings, runtime parameters all fall into this category. Definitions are stored in an internal
www.rabbitmq.com
codeburst.io/get-started-with-rabbitmq-on-docker-4428d7f6e46b
Get Started with RabbitMQ on Docker
How to quickly spin up RabbitMQ instances with Docker and Docker Compose.
codeburst.io
www.rabbitmq.com/tutorials/amqp-concepts.html
AMQP 0-9-1 Model Explained — RabbitMQ
AMQP 0-9-1 Model Explained This guide provides an overview of the AMQP 0-9-1 protocol, one of the protocols supported by RabbitMQ. AMQP 0-9-1 (Advanced Message Queuing Protocol) is a messaging protocol that enables conforming client applications to communi
www.rabbitmq.com