在Debian上使用RabbitMQ消息队列的步骤如下:
sudo apt-get update
sudo apt-get install rabbitmq-server
安装完成后,RabbitMQ服务器会自动启动。
sudo service rabbitmq-server start
sudo service rabbitmq-server stop
sudo service rabbitmq-server status
可以通过编辑 /etc/default/rabbitmq-server
文件来调整系统限制,例如打开文件的句柄数量。
默认情况下,RabbitMQ的管理插件可能未启用。可以通过以下命令启用它:
sudo rabbitmq-plugins enable rabbitmq_management
http://your_server_ip:15672
,使用配置文件中设置的用户名和密码登录。以下是一个简单的示例,展示如何使用RabbitMQ发送和接收消息:
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()
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(queue='hello',
on_message_callback=callback,
auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
以上步骤涵盖了在Debian上安装、配置、启动RabbitMQ服务器以及使用Python进行基本的消息发送和接收。根据实际需求,还可以进一步配置RabbitMQ的高级特性,如持久化、交换器和绑定等。