RabbitMQ 学习

112 阅读2分钟

RabbitMQ

#Go #消息队列

github.com/rabbitmq/am…

因为api更新,使用时的插件改变了,包名需要重新定义

Pasted Graphic 4.png

消息中间件 进程间的通信方式 有高并发再使用

基于消息中间件的分布式

  • 通讯能力
    • 发消息
    • 传消息
  • 高性能
  • 跨平台
    • 系统间的语言差异
  • 持久化
  • 高可用
  • MQ有自己的协议

MQ的协议

  • 消息中间件负责数据传递、存储和分发
  • AMQP
  • 为什么不用http
    • 很多没用的部分,比如cookie,Status
    • 为短链接,请求可能中断,不会进行持久化,请求就会丢失,影响可靠性
  • Kafka
  • MQTT

MQ持久化

  • 防止故障时数据丢失

分发策略

  • 三种角色
    • 生产者
    • 存储消息
    • 消费者
  • 发布订阅
  • 轮询分发
    • 数据公平倾斜
    • 没有顺序
  • 公平分发
    • 数据会有倾斜现象
  • 重发
    • 保证可靠性
  • 消息拉取

MQ高可用& 高可靠

  • Master-Slave主从数据共享集群
  • MS多主多从
  • 多主集群转发-元数据

RabbitMQ角色分类

  • none
  • management
  • policymaker
  • monitoring
  • Adminstrator

AMQP详解

  • 生产者
    • 创建连接
    • 获取通道
    • 发送消息
    • 释放资源
  • 消费者
    • 创建连接
    • 获取通道
    • 获取消息
    • 应答确认
    • 释放资源
  • 核心组成
    • 虚拟机节点: 隔离作用
    • 生产者
    • 消费者
    • 连接
    • 通道
    • 交换机(存在默认交换机)
    • 路由key
    • 队列

RabbitMQ 简单模式

Pasted Graphic 3.png

  • Dial() -> create a connection
  • Con.Channel() -> create a channel
  • Channel.QueueDeclare() -> declare a queue
  • Channel.Publish() -> publish a message, the body of which is contained in amqp.Publishing
  • Channel.Consume() -> consume messages, and stored in one channel (Golang structure)

RabbitMQ 工作队列

Pasted Graphic 5.png We have multiple consumers.

  • Deliver a message to one consumer.
  • Encapsulate a task as a message
  • Round-robin dispatching 多个接受者消费时,自动进行轮询分发,各消费者收到的消息数量相同

Pasted Graphic 6.png

Pasted Graphic 7.png

Pasted Graphic 8.png

  • To protect if one consumer dies and losses our messages, we need an ACK to delete messages.
    • msgs.ack() 》 use ack() to send ACK back to the server
  • Durability
    • If MQ server dies, our messages will lose
    • Need Durability
    • Declare Queue as durable
      • We can’t redefine an existing queue
    • Mark msgs as persistent (amqp.Publishing.DeliveryMode)
  • Fair Dispatch 公平分发
    • channel.Qos() > put prefetch count in, ep. If I put 1 in, that means one time we pass one message to this channel, and not until it has processed with the message and returns us with an ACK will we give it the next message.

RabbitMQ Publishing/Subscribing 发布/订阅

Pasted Graphic 9.png

  • Deliver a message to multiple consumers. ——> Broadcast
  • Producer never send messages directly to queues. There is an exchanger(or one default(is direct mode) between producer and queues.

Pasted Graphic 10.png

  • Exchange types
    • Direct
    • Topic
    • Headers
    • Fanout
      • Not much flexible…
      • Ch.ExchangeDeclare() -> declare an exchanger,returning errors
      • When ch.publish(), fill the name of exchanger.
      • To make messages passed to all consumers, we should let the name of queue empty.
      • Now we need a **binding,**to bind our queue to the exchanger, telling the exchanger to send messages to our queue. Use channel.QueueBind()

RabbitMQ Routing

  • Direct exchange
    • Filter
    • Message only go to the binding queue.
  • Topics
    • Multi-criteria filter
    • In binding keys,
      • Use . do divide the key into serval parts.
      • *(star) represents for any one single word.
      • #(hash) represents for any zero or more words.

Pasted Graphic.png * 就是说绑定的路由key可以模糊匹配的