Exchange
exchange 包含4中類型, direct, topic,fanout,header
-
direct :
exchage 和 queue 進(jìn)行 binding 時(shí)會(huì)設(shè)置 routingKey ,channel.QueueBind(queue: "create_pdf_queue", exchange: "pdf_events", routingKey: "pdf_create", arguments: null);然后,消息發(fā)送時(shí)也會(huì)設(shè)置對(duì)應(yīng)的 routingKey , 只有兩個(gè) routingKey 完全相同,exchange才會(huì)選擇對(duì)應(yīng)的 binding 進(jìn)行消息路由。
channel.BasicPublish(exchange: "pdf_events", routingKey: "pdf_create", basicProperties: properties, body: body); topic
類似 direct exchange , 只是此模式 routingKey 可以使用通配符 '*','#'.fanout
將消息路由到所有綁定的隊(duì)列中, 無(wú)需對(duì)消息的 routingKey 進(jìn)行匹配操作。-
header
路由規(guī)則是根據(jù) header 進(jìn)行判斷,header 就是下面的 arguments 參數(shù)。Dictionary<string, object> aHeader = new Dictionary<string, object>(); aHeader.Add("format", "pdf"); aHeader.Add("type", "report"); aHeader.Add("x-match", "all"); channel.QueueBind(queue: "queue.A", exchange: "agreements", routingKey: string.Empty, arguments: aHeader);其中 x-match 為特殊的 header , all 表示匹配所有的 header ,如果為 any 表示只要匹配其中一個(gè) header 即可。
發(fā)布消息時(shí)傳入 header 值。var properties = channel.CreateBasicProperties(); properties.Persistent = true; Dictionary<string, object> mHeader1 = new Dictionary<string, object>(); mHeader1.Add("format", "pdf"); mHeader1.Add("type", "report"); properties.Headers = mHeader1; channel.BasicPublish(exchange: "agreements", routingKey: string.Empty, basicProperties: properties, body: body);
總結(jié)
一般來(lái)說(shuō)direct和topic用來(lái)具體的路由消息,如果要用廣播的消息一般用fanout的exchange。
header類型用的比較少,但還是知道一點(diǎn)好。