1. RabbitMQ

1.1. rabbit message queue的基本概念

介绍图

producer:消息生产者,就是投递消息的程序。 consumer:消息消费者,就是接受消息的程序。

  • Broker: 接收和分发消息的应用,RabbitMQ Server就是Message Broker。

  • Virtual host: 出于多租户和安全因素设计的,把AMQP的基本组件划分到一个虚拟的分组中,类似于网络中的namespace概念。当多个不同的用户使用同一个RabbitMQ server提供的服务时,可以划分出多个vhost,每个用户在自己的vhost创建exchange/queue等。

  • Connection: publisher/consumer和broker之间的TCP连接。断开连接的操作只会在client端进行,Broker不会断开连接,除非出现网络故障或broker服务出现问题。

  • Channel: 如果每一次访问RabbitMQ都建立一个Connection,在消息量大的时候建立TCP Connection的开销将是巨大的,效率也较低。Channel是在connection内部建立的逻辑连接,如果应用程序支持多线程,通常每个thread创建单独的channel进行通讯,AMQP method包含了channel id帮助客户端和message broker识别channel,所以channel之间是完全隔离的。Channel作为轻量级的Connection极大减少了操作系统建立TCP connection的开销。

  • Exchange: message到达broker的第一站,根据分发规则,匹配查询表中的routing key,分发消息到queue中去。常用的类型有:direct (point-to-point), topic (publish-subscribe) and fanout (multicast)。

  • Queue: 消息最终被送到这里等待consumer取走。一个message可以被同时拷贝到多个queue中。

  • Binding: exchange和queue之间的虚拟连接,binding中可以包含routing key。Binding信息被保存到exchange中的查询表中,用于message的分发依据。


1.2. exchange的类型 (交换机)

生产者并非将消息直接发送到queue,而是发送到exchange中,具体将消息发送到特定的队列还是多个队列,或者是丢弃,取决于exchange的类型

  • direct 直连交换机 (直接交换模式/默认)
  • topic 主题交换机 模糊匹配 routingkey ①*(星号)仅代表一个单词 ②#(井号)代表任意个单词
  • headers 首部交换机 headers与direct的模式不同,不是使用routingkey去做绑定。而是通过消息headers的键值对匹配
  • fanout 扇形交换机 已知的所有队列 相当于广播 速度最快 忽略routingkey

bind方法将一个队列和一个exchange绑定到一起,第三个参数 routing key,相当于描述这个绑定关系的key,会根据不同类型的exchange,来决定将消息发送到queue中,多个queue中,或者消费时,将消息递送到绑定的queue中

1.3. Queue && arguments 扩展

queue:声明的队列名称 durable:是否持久化,是否将队列持久化到mnesia数据库中,有专门的表保存我们的队列声明。 exclusive:排外,①当前定义的队列是connection的channel是共享的,其他的connection是访问不到的。②当connection关闭的时候,队列将被删除。

1.4. autoDelete:自动删除,当最后一个consumer(消费者)断开之后,队列将自动删除。

arguments:参数是rabbitmq的一个扩展,功能非常强大,基本是AMPQ中没有的。

  • x-message-ttl:Number ,发布的消息在队列中存在多长时间后被取消(单位毫秒) 可以对单个消息设置过期时间

  • x-expires:Number

  当Queue(队列)在指定的时间未被访问,则队列将被自动删除。

  • x-max-length:Number

  队列所能容下消息的最大长度。当超出长度后,新消息将会覆盖最前面的消息,类似于Redis的LRU算法。

  • x-max-length-bytes:Number

  限定队列的最大占用空间,当超出后也使用类似于Redis的LRU算法。

  • x-overflow:String

  设置队列溢出行为。这决定了当达到队列的最大长度时,消息会发生什么。有效值为Drop Head或Reject Publish。

  • x-dead-letter-exchange:String 如果消息被拒绝或过期或者超出max,将向其重新发布邮件的交换的可选名称

  • x-dead-letter-routing-key:String

  如果不定义,则默认为溢出队列的routing-key,因此,一般和6一起定义。

  • x-max-priority:Number

  如果将一个队列加上优先级参数,那么该队列为优先级队列。

    1)、给队列加上优先级参数使其成为优先级队列

    x-max-priority=10【值不要太大,本质是一个树结构】

    2)、给消息加上优先级属性

  • x-queue-mode:String

队列类型  x-queue-mode=lazy  懒队列,在磁盘上尽可能多地保留消息以减少RAM使用;如果未设置,则队列将保留内存缓存以尽可能快地传递消息。

  • x-queue-master-locator:String

  将队列设置为主位置模式,确定在节点集群上声明时队列主位置所依据的规则。

PHP Laravel : https://packagist.org/packages/bschmitt/laravel-amqp
Python pika :  https://pika.readthedocs.io/en/stable/

1.5. 字段类型

Here's a tabular summary of the state of things:

  0-9   0-9-1   Qpid/Rabbit  Type               Remarks
---------------------------------------------------------------------------
        t       t            Boolean
        b       b            Signed 8-bit
        U       s            Signed 16-bit      (A1)
        u       u            Unsigned 16-bit
  I     I       I            Signed 32-bit
        i       i            Unsigned 32-bit
        L       l            Signed 64-bit      (B)
        l                    Unsigned 64-bit
        f       f            32-bit float
        d       d            64-bit float
  D     D       D            Decimal
        s                    Short string       (A2)
  S     S       S            Long string
        A       A            Array              (C)
  F     F       F            Nested Table
                x            Byte array         (D)

Remarks:

 A1, A2: Notice how the types CONFLICT here. In Qpid and Rabbit,
         's' means a signed 16-bit integer; in 0-9-1, it means a
         short string.

 B: Notice how the signednesses CONFLICT here. In Qpid and Rabbit,
    'l' means a signed 64-bit integer; in 0-9-1, it means an unsigned
    64-bit integer.

 C: I cannot find any discussion about the addition of this. Is my
    archive missing a few messages?

 D: John [O'Hara]objected to this when he proposed his list. I believe it to
    be vital: byte arrays are not strings. Furthermore, Qpid and
    Rabbit already have code deployed that uses this type specifier.
RabbitMQ continues to use the tags in the third column.

Other notes:

Decimals encoding: "They are encoded as an octet representing the number of places followed by a long signed integer", but the grammar contradicts that and says: "decimal-value = scale long-uint". We treat the decimal value as signed integer.

results matching ""

    No results matching ""