环境说明
RabbitMQ版本:3.7.5
操作系统:Mac OSX
RabbitMQ的安装
https://www.rabbitmq.com/download.html
启动RabbitMQ
rabbitmq-server
RabbitMQ 中exchange 类型
- direct
- fanout
- topic
- header
什么是fanout?
AMQP中的三个要素
- exchanges
- queues
- bindings
RabbitMQ 创建vhost
rabbitmqctl add_vhost first
查看vhost列表
rabbitmqctl list_vhosts
RabbitMQ 通过将消息写入log文件实现持久化
Python 生产者-消费者模式代码示例
消费者代码
#consumer.py
import pika
credentials = pika.PlainCredentials('guest', 'guest')
conn_params = pika.ConnectionParameters('localhost', credentials=credentials)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
channel.exchange_declare(exchange='hello-exchange',
exchange_type='direct')
channel.queue_declare(queue='hello-queue')
channel.queue_bind(queue='hello-queue',
exchange='hello-exchange',
routing_key='hola')
def msg_consumer(channel, method, header, body):
channel.basic_ack(delivery_tag=method.delivery_tag)
if body == 'quit':
channel.basic_cancel(consumer_tag='hello-consumer')
channel.stop_consuming()
else:
print(body)
return
channel.basic_consume(msg_consumer, queue='hello-queue', consumer_tag='hello-consumer')
channel.start_consuming()
生产者代码
#producer.py
import pika, sys
credentials = pika.PlainCredentials('guest', 'guest')
conn_params = pika.ConnectionParameters('localhost', credentials=credentials)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
channel.exchange_declare(exchange="hello-exchange", exchange_type="direct")
msg = sys.argv[1]
msg_props = pika.BasicProperties()
msg_props.content_type = "text/plain"
channel.basic_publish(body=msg,
exchange="hello-exchange",
properties=msg_props,
routing_key='hola')
用例
第一步: 打开终端,执行 python3 consumer.py
第二步: 新开一个终端,执行 python3 producer.py message
可以在终端上看到相应的输出
可以通过启用shovel
插件开启异地通讯
,异地备份
的功能
如何启动RabbitMQ 图形化界面
# 开启插件(3.7.5中已默认开启)
rabbitmq-plugins enable rabbitmq_management
打开页面 http://localhost:15672
,默认的账号为guest
,密码为guest
- Management: Web UI
- Management: HTTP API
- Management: Command Line Tool
相关操作请查看文档:https://www.rabbitmq.com/management.html
如何保证应用的可用性
可以使用Nagios进行安全监测
官网:https://www.nagios.org/
账号安全性认证
openssl和其他
操作流程: https://www.rabbitmq.com/ssl.html
RabbitMQ 插件列表
https://www.rabbitmq.com/plugins.html
- rabbitmq_amqp1_0
- rabbitmq_auth_backend_ldap
- rabbitmq_auth_backend_http
- rabbitmq_auth_mechanism_ssl
- abbitmq_consistent_hash_exchange
- rabbitmq_federation
- rabbitmq_federation_management
- rabbitmq_management
- rabbitmq_management_agent
- rabbitmq_mqtt
- rabbitmq_shove
- rabbitmq_shovel_management
- rabbitmq_stomp
- rabbitmq_tracing
- rabbitmq_web_stomp
- rabbitmq_web_mqtt
网友评论