一、关于管理
1. 允许 15672端口 web服务
rabbitmq-plugins enable rabbitmq_management
2. 用户管理
(1) 新增一个用户
rabbitmqctl add_user Username Password
(2) 删除一个用户
rabbitmqctl delete_user Username
(3) 修改用户的密码
rabbitmqctl change_password Username Newpassword
(4) 查看当前用户列表
rabbitmqctl list_users
(5)设置用户角色:
rabbitmqctl set_user_tags Username Tag
其中tag可以是:dministrator,monitoring,policymaker,managemen
3.重启服务
systemctl restart rabbitmq-server
二、 网页服务
网页服务
http://localhost:15672
默认服务端口:5672
三、简单测试收发信息
1. 发送信息到队列 send1.py
import pika
user_pwd = pika.PlainCredentials("admin","pwd")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.10', credentials=user_pwd))
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()
2. 接受(消费)信息 consume1.py
import pika
user_pwd = pika.PlainCredentials("admin","pwd")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.10', credentials=user_pwd))
channel = connection.channel()
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()
四、模拟远程调用客户服务器应答
以下程序两个shell中分别运行,客户端发送30到队列,服务器接受后,作+1计算,得到31再把结果送回应答队列,客户端接收到返回结果。
1. rpc_server.py
#!/usr/bin/env python
#coding=utf8
import pika
user_pwd = pika.PlainCredentials("admin","pwd")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.10', credentials=user_pwd))
channel = connection.channel()
channel.queue_declare(queue='compute_queue')
print ' [*] Waiting for n'
def increase(n):
return n + 1
def request(ch, method, properties, body):
print " [.] increase(%s)" % (body,)
response = increase(int(body))
ch.basic_publish(exchange='',
routing_key=properties.reply_to,
body=str(response))
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(request, queue='compute_queue')
channel.start_consuming()
2. rpc_client.py
#!/usr/bin/env python
#coding=utf8
import pika
class Center(object):
def __init__(self):
user_pwd = pika.PlainCredentials("admin","pwd")
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.10', credentials=user_pwd))
self.channel = self.connection.channel()
result = self.channel.queue_declare(exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(self.on_response,
no_ack=True,
queue=self.callback_queue)
def on_response(self, ch, method, props, body):
self.response = body
def request(self, n):
self.response = None
self.channel.basic_publish(exchange='',
routing_key='compute_queue',
properties=pika.BasicProperties(
reply_to = self.callback_queue,
),
body=str(n))
while self.response is None:
self.connection.process_data_events()
return int(self.response)
center = Center()
print " [x] Requesting increase(30)"
response = center.request(30)
print " [.] Got %r" % (response,)
以上代码收集自网络,记录备用。