RabbitMQ简明笔记

环境说明

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



如何保证应用的可用性

可以使用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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 来源 RabbitMQ是用Erlang实现的一个高并发高可靠AMQP消息队列服务器。支持消息的持久化、事务、拥塞控...
    jiangmo阅读 10,407评论 2 34
  • http://liuxing.info/2017/06/30/Spring%20AMQP%E4%B8%AD%E6%...
    sherlock_6981阅读 16,010评论 2 11
  • rabbitMQ是一款基于AMQP协议的消息中间件,它能够在应用之间提供可靠的消息传输。在易用性,扩展性,高可用性...
    点融黑帮阅读 3,046评论 3 41
  • Exchanges, queues, and bindings(转) exchanges, queues, and...
    杨传池chris阅读 284评论 0 0
  • 2017年7月12日,星期三,深圳晴 我是日记星球262号星宝宝燕子,我正在参加日记星球21天蜕变之旅第7期,这是...
    沙沙的健康乐园阅读 262评论 2 0