Cloud

RabbitMQ 파악하기

arisu1000 2015. 3. 11. 09:00
메세지 브로커(broker). 메세지를 중개/관리해주는 역할을 수행.
데이터 전달, 논블럭킹 운영, 푸시 알림. 발행/구독, 비동기 처리, 작업 큐 등의 역할.

지원 프로토콜 : AMQP 0-9-1/0-9/0-8/1.0, STOMP, MQTT, HTTP

설치
OS : Mac OS X 10.10.2
RabbitMQ 버전 : 3.4.4

파일 다운로드 
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.4/rabbitmq-server-mac-standalone-3.4.4.tar.gz
tar zxvf rabbitmq-server-mac-standalone-3.4.4.tar.gz
cd rabbitmq_server-3.4.4/

서버 실행
sbin/rabbitmq-server

기본적인 메세지 보내고 받기
파이썬용 AMQP 0-9-1 클라이언트 라이브러리인 Pika 설치
sudo pip install pika==0.9.8

메세지 보내는 파이썬 스크립트 : send.py
# coding=utf-8
#!/usr/bin/env python
import pika

# rabbitmq에 연결
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# hello 큐를 생성
channel.queue_declare(queue='hello')

# hello 큐에 Hello World!라는 메세지를 보냄.
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print " [x] Sent 'Hello World!'"

connection.close()

메세지 받는 파이썬 스크립트 : receive.py
# coding=utf-8
#!/usr/bin/env python
import pika

# rabbitmq에 연결
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()


# hello 큐를 생성
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()

메세지 보내기
python send.py

메세지 받기
python receive.py


참고