RabbitMQ
As a 消息队列
transmission of a common message and receiving platforms , and to ensure that the message during transmission secure .
Message (Message) by the Client
transmission, RabbitMQ
after receiving the message by 交换机
forwarding to the corresponding 队列
above. Worker
Data acquisition processing will not read from the queue.
The process of message processing
switch
There are four different types of switches:
- Direct Switch: Direct exchange
- Sector switch: Fanout exchange
- Switch Theme: Topic exchange
- Switch headers: Headers exchange
Fan switch
Fan switch is the most basic type of switch, it can do things very simple --- a broadcast message. Sector switch would receive the message sent to all the queues themselves bound. Speed because the broadcast does not need to "think", the fan switch is handling all message types of switches inside the fastest.
Fan switch
Direct Switch
Switch Router is a direct function of the switch, a switch and a queue will bind, in addition to a further binding routing_key
, when the message is sent, a need to specify binding_key
, when the message is delivered to the switch, it will be sent to the designated queue inside the switch. A same binding_key
is applied to a support a plurality of queues.
Such a queue when a plurality of switch queues binding, will be sent to the corresponding process.
Direct Switch
Application scenario: there priority task, to a corresponding queue according to the priority of the message task, so that more resources may be assigned to process higher priority queue.
Switch Theme
Direct-connect switch routing_key
program is very simple, if we want to send a message to multiple queues, then the switch needs to bind a lot of routing_key
, on the assumption that each switch to bind a bunch of routing_key
connected to each queue. Then the management messages will be exceptionally difficult.
所以RabbitMQ
提供了一种主题交换机,发送到主题交换机上的消息需要携带指定规则的routing_key
,主题交换机会根据这个规则将数据发送到对应的(多个)队列上。
主题交换机的routing_key
需要有一定的规则,交换机和队列的binding_key
需要采用*.#.*.....
的格式,每个部分用.
分开,其中:
*
表示一个单词#
表示任意数量(零个或多个)单词。
假设有一条消息的routing_key
为fast.rabbit.white
,那么带有这样binding_key
的几个队列都会接收这条消息:
- fast..
- ..white
- fast.#
- ……
这个图是网上找的,感觉对主题交换机的描述比较到位:
主题交换机
当一个队列的绑定键为#
的时候,这个队列将会无视消息的路由键,接收所有的消息。
首部交换机
首部交换机是忽略routing_key
的一种路由方式。路由器和交换机路由的规则是通过Headers
信息来交换的,这个有点像HTTP
的Headers
。将一个交换机声明成首部交换机,绑定一个队列的时候,定义一个Hash
的数据结构,消息发送的时候,会携带一组hash数据结构的信息,当Hash
的内容匹配上的时候,消息就会被写入队列。
绑定交换机和队列的时候,Hash结构中要求携带一个键“x-match”,这个键的Value
可以是any
或者all
,这代表消息携带的Hash
是需要全部匹配(all),还是仅匹配一个键(any)就可以了。相比直连交换机,首部交换机的优势是匹配的规则不被限定为字符串(string)。
--转载