首先展示網(wǎng)絡(luò)上的兩種圖:
AMQP :

Rabbit各關(guān)鍵組件交換流程:

Rabbit 內(nèi)部線程圖:

1.ConnectionFactory、Connection、Channel
ConnectionFactory、Connection、Channel都是RabbitMQ對外提供的API中最基本的對象。
Connection是RabbitMQ的socket鏈接,它封裝了socket協(xié)議相關(guān)部分邏輯。
ConnectionFactory如名稱,是客戶端與broker的tcp連接工廠,負(fù)責(zé)根據(jù)uri創(chuàng)建Connection。
Channel是我們與RabbitMQ打交道的最重要的一個(gè)接口,我們大部分的業(yè)務(wù)操作是在Channel這個(gè)接口中完成的,包括定義Queue、定義Exchange、綁定Queue與Exchange、發(fā)布消息等。如果每一次訪問RabbitMQ都建立一個(gè)Connection,在消息量大的時(shí)候建立TCP Connection的開銷將是巨大的,效率也較低。Channel是在connection內(nèi)部建立的邏輯連接,如果應(yīng)用程序支持多線程,通常每個(gè)thread創(chuàng)建單獨(dú)的channel進(jìn)行通訊,AMQP method包含了channel id幫助客戶端和message broker識別channel,所以channel之間是完全隔離的。Channel作為輕量級的Connection極大減少了操作系統(tǒng)建立TCP connection的開銷
注:channel 與 connection區(qū)別
A Connection represents a real TCP connection to the message broker, whereas aChannelis a virtual connection (AMPQ connection) inside it. This way you can use as many (virtual) connections as you want inside your application without overloading the broker with TCP connections.
You can use one Channel for everything. However, if you have multiple threads, it's suggested to use a different Channel for each thread.
There is no direct relation betweenChannelandQueue. AChannelis used to send AMQP commands to the broker. This can be the creation of a queue or similar, but these concepts are not tied together.
Consumerruns in its own thread allocated from the consumer thread pool. If multiple Consumers are subscribed to the same Queue, the broker uses round-robin to distribute the messages between them equally.
It is also possible to attach the sameConsumerto multiple Queues. You can understand Consumers as callbacks. These are called everytime a message arrives on a Queue the Consumer is bound to. For the case of the Java Client, each Consumers has a methodhandleDelivery(...), which represents the callback method. What you typically do is, subclassDefaultConsumerand overridehandleDelivery(...). Note: If you attach the same Consumer instance to multiple queues, this method will be called by different threads. So take care of synchronization if necessary.
部分源碼:

2.producer & consumer
producer 為生產(chǎn)者,consumer為消費(fèi)者
3.Exchange
Exchange類似于數(shù)據(jù)通信網(wǎng)絡(luò)中的交換機(jī),提供消息路由策略。rabbitmq中,producer不是通過信道直接將消息發(fā)送給queue,而是先發(fā)送給Exchange。一個(gè)Exchange可以和多個(gè)Queue進(jìn)行綁定,producer在傳遞消息的時(shí)候,會傳遞一個(gè)ROUTING_KEY,Exchange會根據(jù)這個(gè)ROUTING_KEY按照特定的路由算法,將消息路由給指定的queue。和Queue一樣,Exchange也可設(shè)置為持久化,臨時(shí)或者自動刪除。
Exchange有4種類型:direct(默認(rèn)),fanout, topic, 和headers,不同類型的Exchange轉(zhuǎn)發(fā)消息的策略有所區(qū)別:
Direct:
直接交換器,工作方式類似于單播,Exchange會將消息發(fā)送完全匹配ROUTING_KEY的Queue
fanout:
廣播是式交換器,不管消息的ROUTING_KEY設(shè)置為什么,Exchange都會將消息轉(zhuǎn)發(fā)給所有綁定的Queue。
topic
主題交換器,工作方式類似于組播,Exchange會將消息轉(zhuǎn)發(fā)和ROUTING_KEY匹配模式相同的所有隊(duì)列,比如,ROUTING_KEY為user.stock的Message會轉(zhuǎn)發(fā)給綁定匹配模式為 * .stock,user.stock, * . * 和#.user.stock.#的隊(duì)列。( * 表是匹配一個(gè)任意詞組,#表示匹配0個(gè)或多個(gè)詞組)
headers
消息體的header匹配(ignore)
4.routing key
生產(chǎn)者在將消息發(fā)送給Exchange的時(shí)候,一般會指定一個(gè)routing key,來指定這個(gè)消息的路由規(guī)則,而這個(gè)routing key需要與Exchange Type及binding key聯(lián)合使用才能最終生效。
在Exchange Type與binding key固定的情況下(在正常使用時(shí)一般這些內(nèi)容都是固定配置好的),我們的生產(chǎn)者就可以在發(fā)送消息給Exchange時(shí),通過指定routing key來決定消息流向哪里。
RabbitMQ為routing key設(shè)定的長度限制為255 bytes
5.Binding
所謂綁定就是將一個(gè)特定的 Exchange 和一個(gè)特定的 Queue 綁定起來。Exchange 和Queue的綁定可以是多對多的關(guān)系
6.Binding key
在綁定(Binding)Exchange與Queue的同時(shí),一般會指定一個(gè)binding key;消費(fèi)者將消息發(fā)送給Exchange時(shí),一般會指定一個(gè)routing key;當(dāng)binding key與routing key相匹配時(shí),消息將會被路由到對應(yīng)的Queue中。
在綁定多個(gè)Queue到同一個(gè)Exchange的時(shí)候,這些Binding允許使用相同的binding key。
binding key 并不是在所有情況下都生效,它依賴于Exchange Type,比如fanout類型的Exchange就會無視binding key,而是將消息路由到所有綁定到該Exchange的Queue。
7.virtual host
在rabbitmq server上可以創(chuàng)建多個(gè)虛擬的message broker,又叫做virtual hosts (vhosts)。每一個(gè)vhost本質(zhì)上是一個(gè)mini-rabbitmq server,分別管理各自的exchange,和bindings。vhost相當(dāng)于物理的server,可以為不同app提供邊界隔離,使得應(yīng)用安全的運(yùn)行在不同的vhost實(shí)例上,相互之間不會干擾。producer和consumer連接rabbit server需要指定一個(gè)vhost。
參考link:http://blog.csdn.net/tengxy_cloud/article/details/52808415
http://blog.csdn.net/whycold/article/details/41119807
https://www.rabbitmq.com/tutorials/amqp-concepts.html
http://www.cnblogs.com/frankyou/p/5283539.html