本文共 9073 字,大约阅读时间需要 30 分钟。
安装完成后,确保以下软件版本匹配:
运行以下命令启动项目:
django-admin startproject django_rabbitmq
在Django项目的settings.py中添加以下配置:
BROKER_URL = 'amqp://guest:guest@localhost:15672/'CELERY_RESULT_BACKEND = 'rpc://'
import pikadef callback(ch, method, properties, body): print(f"[x] Received {body.decode()}")def start_consuming(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()if __name__ == "__main__": start_consuming() import pikadef publish_message(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='hello') message = "Hello World!" channel.basic_publish(exchange='', routing_key='hello', body=message) print(f"[x] Sent '{message}'") connection.close()if __name__ == "__main__": publish_message() 运行命令:
python consumer.pypython producer.py
import timeimport pikadef callback(ch, method, properties, body): print(" [x] Received %r" % body) time.sleep(20) print(" [x] Done") ch.basic_ack(delivery_tag=method.delivery_tag)def start_consuming(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='hello2', durable=True) channel.basic_consume(queue='hello2', on_message_callback=callback, auto_ack=False) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()if __name__ == "__main__": start_consuming() import pikadef publish_message(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='hello2', durable=True) channel.basic_publish(exchange='', routing_key='hello2', body='Hello World!', properties=pika.BasicProperties(delivery_mode=2)) connection.close()if __name__ == "__main__": publish_message()
运行命令:
python recv_msg_safe.pypython send_msg_safe.py
import pikadef callback(ch, method, properties, body): print(" [x] %r" % body)def start_consuming(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.exchange_declare(exchange='logs', exchange_type='fanout') result = channel.queue_declare("", exclusive=True) queue_name = result.method.queue channel.queue_bind(exchange='logs', queue=queue_name) print(' [*] Waiting for logs. To exit press CTRL+C') channel.basic_consume(on_message_callback=callback, queue=queue_name, auto_ack=True) channel.start_consuming()if __name__ == "__main__": start_consuming() import pikaimport sysdef publish_message(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.exchange_declare(exchange='logs', exchange_type='fanout') message = ' '.join(sys.argv[1:]) or "info: Hello World!" channel.basic_publish(exchange='logs', routing_key='', body=message) print(" [x] Sent %r" % message) connection.close()if __name__ == "__main__": publish_message() 运行命令:
python fanout_receive.pypython fanout_send.py
import pikaimport sysdef callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body))def start_consuming(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.exchange_declare(exchange='direct_logs', exchange_type='direct') result = channel.queue_declare("", exclusive=True) queue_name = result.method.queue binding_keys = sys.argv[1:] if not binding_keys: sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0]) sys.exit(1) for severity in binding_keys: channel.queue_bind(exchange='direct_logs', queue=queue_name, routing_key=severity) print(' [*] Waiting for logs. To exit press CTRL+C') channel.basic_consume(on_message_callback=callback, queue=queue_name) channel.start_consuming()if __name__ == "__main__": start_consuming() import pikaimport sysdef publish_message(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.exchange_declare(exchange='direct_logs', exchange_type='direct') routing_key = sys.argv[1] if len(sys.argv) > 1 else 'info' message = ' '.join(sys.argv[2:]) or 'Hello World!' channel.basic_publish(exchange='direct_logs', routing_key=routing_key, body=message) print(" [x] Sent %r:%r" % (routing_key, message)) connection.close()if __name__ == "__main__": publish_message() 运行命令:
python direct_recv.py infopython direct_send.py info
import pikaimport sysdef callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body))def start_consuming(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic') result = channel.queue_declare("", exclusive=True) queue_name = result.method.queue binding_keys = sys.argv[1:] if not binding_keys: print("sys.argv[0]", sys.argv[0]) sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0]) sys.exit(1) for binding_key in binding_keys: channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=binding_key) print(' [*] Waiting for logs. To exit press CTRL+C') channel.basic_consume(on_message_callback=callback, queue=queue_name) channel.start_consuming()if __name__ == "__main__": start_consuming() import pikaimport sysdef publish_message(): connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic') routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info' message = ' '.join(sys.argv[2:]) or 'Hello World!' channel.basic_publish(exchange='topic_logs', routing_key=routing_key, body=message) print(" [x] Sent %r:%r" % (routing_key, message)) connection.close()if __name__ == "__main__": publish_message() 运行命令:
python topic_recv.py infopython topic_send.py info
import pikaimport uuidimport timeclass FibonacciRpcClient: def __init__(self): self.response = None credentials = pika.PlainCredentials('guest', 'guest') self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) self.channel = self.connection.channel() result = self.channel.queue_declare("", exclusive=True) self.callback_queue = result.method.queue self.channel.basic_consume(queue=self.callback_queue, auto_ack=True, on_message_callback=self.on_response) def on_response(self, ch, method, props, body): if self.corr_id == props.correlation_id: self.response = body def call(self, n): self.corr_id = str(uuid.uuid4()) self.channel.basic_publish(exchange='', routing_key='rpc_queue', properties=pika.BasicProperties(reply_to=self.callback_queue, correlation_id=self.corr_id), body=str(n)) while self.response is None: self.connection.process_data_events() time.sleep(0.5) return int(self.response)fibonacci_rpc = FibonacciRpcClient()response = fibonacci_rpc.call(5)print(" [.] Got %r" % response) import pikaimport timedef fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2)def on_request(ch, method, props, body): n = int(body) print(" [.] fib(%s)" % n) response = fib(n) ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id=props.correlation_id), body=str(response))channel = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))channel.channel.queue_declare(queue='rpc_queue')channel.basic_consume(queue="rpc_queue", auto_ack=True, on_message_callback=on_request)print(" [x] Awaiting RPC requests")channel.start_consuming() 运行命令:
python rpc_server.pypython rpc_client.py
转载地址:http://uqafk.baihongyu.com/