Skip to content

Commit 09d9567

Browse files
committed
add rabbitmq practice
1 parent 954ce9f commit 09d9567

17 files changed

+491
-0
lines changed

rabbitmq/emit_logs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding: utf-8
2+
import pika
3+
import sys
4+
5+
connection = pika.BlockingConnection(
6+
pika.ConnectionParameters(host='localhost'))
7+
channel = connection.channel()
8+
9+
channel.exchange_declare(exchange='logs', type='fanout')
10+
messages = ''.join(sys.argv[1:]) or 'info: Hello World!'
11+
12+
channel.basic_publish(exchange='logs',
13+
routing_key='',
14+
body=messages)
15+
16+
print("[x] Send {}".format(messages))
17+
connection.close()

rabbitmq/emit_logs_direct.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# coding=utf-8
2+
import pika
3+
import sys
4+
5+
connection = pika.BlockingConnection(
6+
pika.ConnectionParameters(host='localhost'))
7+
channel = connection.channel()
8+
9+
channel.exchange_declare(exchange='direct_logs',
10+
type='direct')
11+
12+
severity = sys.argv[1] if len(sys.argv) > 2 else 'info'
13+
messages = ''.join(sys.argv[2:]) or "Hello World!"
14+
15+
channel.basic_publish(exchange="direct_logs",
16+
routing_key=severity,
17+
body=messages)
18+
print('[x] Send {}:{}'.format(severity, messages))
19+
connection.close()

rabbitmq/emit_logs_topic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# coding=utf-8
2+
import pika
3+
import sys
4+
5+
connection = pika.BlockingConnection(
6+
pika.ConnectionParameters(host='localhost'))
7+
channel = connection.channel()
8+
9+
channel.exchange_declare(exchange='topic_logs',
10+
type='topic')
11+
12+
routing_key = sys.argv[1] if len(sys.argv) > 2 else 'anonymous.info'
13+
messages = ''.join(sys.argv[2:]) or "Hello World!"
14+
15+
channel.basic_publish(exchange='topic_logs',
16+
routing_key=routing_key,
17+
body=messages)
18+
19+
print("[x] Send {}:{}".format(routing_key, messages))
20+
connection.close()

rabbitmq/kombu_emit_logs_topic.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding=utf-8
2+
import sys
3+
4+
from kombu import Connection, Producer, Queue, Exchange
5+
6+
logs_exchange = Exchange('logs', 'topic', durable=True)
7+
8+
URL = 'amqp://localhost'
9+
10+
kombu_learn = sys.argv[1] if len(sys.argv) > 2 else 'anonymous.info'
11+
messages = ''.join(sys.argv[2:]) or "Hello World!"
12+
13+
with Connection(URL) as conn:
14+
producer = Producer(conn)
15+
producer.publish(messages, exchange=logs_exchange,
16+
routing_key=kombu_learn,
17+
serializer='json')
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# coding=utf-8
2+
import sys
3+
4+
from kombu import Exchange, Queue, Connection, Consumer
5+
from kombu.async import Hub
6+
7+
8+
logs_exchange = Exchange(name='logs', type="topic", durable=True)
9+
10+
URL = 'amqp://localhost'
11+
hub = Hub()
12+
13+
binding_keys = sys.argv[1:]
14+
if not binding_keys:
15+
sys.stderr.write("Usage: {} [binding_keys]...\n".format(sys.argv[0]))
16+
sys.exit()
17+
18+
tasks_queues = [Queue(binding_key,
19+
logs_exchange,
20+
exclusive=True,
21+
routing_key=binding_key)
22+
for binding_key in binding_keys]
23+
24+
print("[*] Waitting for logs. To exit press Ctrl+C")
25+
26+
27+
def on_messages(body, messages):
28+
print("""
29+
Body: {0}
30+
Properties: {1}
31+
DeliveryInfo: {2}
32+
""".format(body, messages.properties, messages.delivery_info)
33+
)
34+
35+
with Connection(URL) as conn:
36+
conn.register_with_event_loop(hub)
37+
with Consumer(conn, tasks_queues, callbacks=[on_messages]):
38+
try:
39+
hub.run_forever()
40+
except KeyboardInterrupt:
41+
exit()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# coding=utf-8
2+
import sys
3+
4+
from kombu import Exchange, Queue, Connection
5+
from kombu.mixins import ConsumerMixin
6+
7+
8+
class Worker(ConsumerMixin):
9+
logs_exchange = Exchange(name='logs', type="topic", durable=True)
10+
11+
def __init__(self, connection):
12+
self.connection = connection
13+
14+
binding_keys = sys.argv[1:]
15+
if not binding_keys:
16+
sys.stderr.write('Usage: {} [binding_keys] ...\n'.format(sys.argv[0]))
17+
18+
def get_consumers(self, Consumer, channel):
19+
return [Consumer([Queue(binding_key,
20+
self.logs_exchange,
21+
exclusive=True,
22+
routing_key=binding_key)
23+
for binding_key in self.binding_keys],
24+
callbacks=[self.on_messages])]
25+
26+
def on_messages(self, body, messages):
27+
print('Body: {}'.format(body))
28+
29+
30+
URL = 'amqp://localhost'
31+
with Connection(URL) as connection:
32+
Worker(connection).run()

rabbitmq/new_task.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
import pika
3+
import sys
4+
5+
connection = pika.BlockingConnection(
6+
pika.ConnectionParameters(host='localhost'))
7+
channel = connection.channel()
8+
channel.queue_declare(queue='task_queue', durable=True)
9+
# durable 持久化,即便rabbitMQ挂了也不会丢失信息
10+
messages = ''.join(sys.argv[1:]) or "Hello World!"
11+
channel.basic_publish(exchange='',
12+
routing_key='task_queue',
13+
body=messages,
14+
properties=pika.BasicProperties(
15+
delivery_mode=2))
16+
print('[x] Send {}'.format(messages))
17+
connection.close()

rabbitmq/receive.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# coding=utf-8
2+
import pika
3+
4+
connection = pika.BlockingConnection(pika.ConnectionParameters(
5+
host="localhost"))
6+
channel = connection.channel()
7+
channel.queue_declare(queue='hello')
8+
9+
10+
def callback(ch, method, properties, body):
11+
print("[x] Receive {}".format(body))
12+
13+
channel.basic_consume(callback,
14+
queue='hello',
15+
no_ack=True) # 显示声明无消息确认回执
16+
17+
print('[*] Waitting for messages. To exit press Ctrl+C')
18+
19+
try:
20+
channel.start_consuming()
21+
except KeyboardInterrupt:
22+
channel.stop_consuming()

rabbitmq/receive_logs.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# coding: utf-8
2+
import pika
3+
4+
connection = pika.BlockingConnection(
5+
pika.ConnectionParameters(host='localhost'))
6+
channel = connection.channel()
7+
8+
channel.exchange_declare(exchange='logs',
9+
type='fanout')
10+
11+
result = channel.queue_declare(exclusive=True)
12+
# disconnect consumer the queue is down
13+
14+
queue_name = result.method.queue
15+
16+
channel.queue_bind(exchange='logs',
17+
queue=queue_name)
18+
19+
print("[*] Waitting for logs. To exit press Ctrl+C")
20+
21+
22+
def callback(ch, method, properties, body):
23+
print("[x] {}".format(body))
24+
25+
channel.basic_consume(callback, queue=queue_name, no_ack=True)
26+
27+
try:
28+
channel.start_consuming()
29+
except KeyboardInterrupt:
30+
channel.stop_consuming()

rabbitmq/receive_logs_direct.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
import pika
3+
import sys
4+
5+
connection = pika.BlockingConnection(
6+
pika.ConnectionParameters(host='localhost'))
7+
channel = connection.channel()
8+
9+
channel.exchange_declare(exchange='direct_logs',
10+
type='direct')
11+
12+
result = channel.queue_declare(exclusive=True)
13+
queue_name = result.method.queue
14+
15+
severities = sys.argv[1:]
16+
if not severities:
17+
sys.stderr.write(
18+
"Usage: {} [info] [warning] [error]\n".format(sys.argv[0]))
19+
sys.exit(1)
20+
21+
for severity in severities:
22+
channel.queue_bind(exchange='direct_logs',
23+
queue=queue_name,
24+
routing_key=severity)
25+
26+
print("[*] Waitting for logs. To exit press Ctrl+C")
27+
28+
29+
def callback(ch, method, properties, body):
30+
print("[x] {}:{}".format(method.routing_key, body))
31+
32+
channel.basic_consume(callback, queue=queue_name, no_ack=True)
33+
try:
34+
channel.start_consuming()
35+
except KeyboardInterrupt:
36+
channel.stop_consuming()

0 commit comments

Comments
 (0)