RabbitMQ简明笔记

2018-05-12  本文已影响0人  逸筱幻
环境说明

RabbitMQ版本:3.7.5
操作系统:Mac OSX


RabbitMQ的安装

https://www.rabbitmq.com/download.html


启动RabbitMQ

rabbitmq-server


RabbitMQ 中exchange 类型

什么是fanout?

AMQP中的三个要素

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



如何保证应用的可用性

可以使用Nagios进行安全监测
官网:https://www.nagios.org/


账号安全性认证

openssl和其他
操作流程: https://www.rabbitmq.com/ssl.html


RabbitMQ 插件列表

https://www.rabbitmq.com/plugins.html


上一篇下一篇

猜你喜欢

热点阅读