RabbitMQ是一個由erlang開發的基於AMQP(Advanced Message Queue)協議的開源實現。用於在分佈式系統中存儲轉發消息,在易用性、擴展性、高可用性等方面都很是的優秀。是當前最主流的消息中間件之一。html
RabbitMQ的官方git
操做起來很簡單,只須要在DOS下面,進入安裝目錄(安裝路徑\RabbitMQ Server\rabbitmq_server-3.2.2\sbin
)執行以下命令就能夠成功安裝。github
rabbitmq-plugins enable rabbitmq_management
能夠經過訪問:http://localhost:15672
進行測試,默認的登錄帳號爲:guest,密碼爲:guest。bash
1. 安裝完之後erlang須要手動設置ERLANG_HOME 的系統變量。服務器
set ERLANG_HOME=F:\Program Files\erl9.0 #環境變量`path`里加入:%ERLANG_HOME%\bin #環境變量`path`里加入: 安裝路徑\RabbitMQ Server\rabbitmq_server-3.6.10\sbin
2.激活Rabbit MQ’s Management Plugin分佈式
使用Rabbit MQ 管理插件,能夠更好的可視化方式查看Rabbit MQ 服務器實例的狀態,你能夠在命令行中使用下面的命令激活。ide
rabbitmq-plugins.bat enable rabbitmq_management
3.建立管理用戶工具
rabbitmqctl.bat add_user sa 123456
4. 設置管理員測試
rabbitmqctl.bat set_user_tags sa administrator
5.設置權限ui
rabbitmqctl.bat set_permissions -p / sa ".*" ".*" ".*"
6. 其餘命令
#查詢用戶: rabbitmqctl.bat list_users #查詢vhosts: rabbitmqctl.bat list_vhosts #啓動RabbitMQ服務: net stop RabbitMQ && net start RabbitMQ
以上這些,帳號、vhost、權限、做用域等基本就設置完了。
RabbitMQ.Client 是RabbiMQ 官方提供的的客戶端
EasyNetQ 是基於RabbitMQ.Client 基礎上封裝的開源客戶端,使用很是方便
如下操做RabbitMQ的代碼例子,都是基於EasyNetQ的使用和再封裝,在文章底部有demo例子的源碼下載地址
建立 IBus
/// <summary> /// 消息服務器鏈接器 /// </summary> public class BusBuilder { public static IBus CreateMessageBus() { // 消息服務器鏈接字符串 // var connectionString = ConfigurationManager.ConnectionStrings["RabbitMQ"]; string connString = "host=127.0.0.1:5672;virtualHost=TestQueue;username=sa;password=123456"; if (connString == null || connString == string.Empty) throw new Exception("messageserver connection string is missing or empty"); return RabbitHutch.CreateBus(connString); } }
Fanout Exchange
全部發送到Fanout Exchange的消息都會被轉發到與該Exchange 綁定(Binding)的全部Queue上。
Fanout Exchange 不須要處理RouteKey 。只須要簡單的將隊列綁定到exchange 上。這樣發送到exchange的消息都會被轉發到與該交換機綁定的全部隊列上。相似子網廣播,每臺子網內的主機都得到了一份複製的消息。 因此,Fanout Exchange 轉發消息是最快的。
/// <summary> /// 消息消耗(fanout) /// </summary> /// <typeparam name="T">消息類型</typeparam> /// <param name="handler">回調</param> /// <param name="exChangeName">交換器名</param> /// <param name="queueName">隊列名</param> /// <param name="routingKey">路由名</param> public static void FanoutConsume<T>(Action<T> handler, string exChangeName = "fanout_mq", string queueName = "fanout_queue_default", string routingKey = "") where T : class { var bus = BusBuilder.CreateMessageBus(); var adbus = bus.Advanced; var exchange = adbus.ExchangeDeclare(exChangeName, ExchangeType.Fanout); var queue = CreateQueue(adbus, queueName); adbus.Bind(exchange, queue, routingKey); adbus.Consume(queue, registration => { registration.Add<T>((message, info) => { handler(message.Body); }); }); } /// <summary> /// 消息上報(fanout) /// </summary> /// <typeparam name="T">消息類型</typeparam> /// <param name="topic">主題名</param> /// <param name="t">消息命名</param> /// <param name="msg">錯誤信息</param> /// <returns></returns> public static bool FanoutPush<T>(T t, out string msg, string exChangeName = "fanout_mq", string routingKey = "") where T : class { msg = string.Empty; try { using (var bus = BusBuilder.CreateMessageBus()) { var adbus = bus.Advanced; var exchange = adbus.ExchangeDeclare(exChangeName, ExchangeType.Fanout); adbus.Publish(exchange, routingKey, false, new Message<T>(t)); return true; } } catch (Exception ex) { msg = ex.ToString(); return false; } }
全部發送到Direct Exchange的消息被轉發到RouteKey中指定的Queue。
Direct模式,可使用RabbitMQ自帶的Exchange:default Exchange 。因此不須要將Exchange進行任何綁定(binding)操做 。消息傳遞時,RouteKey必須徹底匹配,纔會被隊列接收,不然該消息會被拋棄。
/// <summary> /// 消息發送(direct) /// </summary> /// <typeparam name="T">消息類型</typeparam> /// <param name="queue">發送到的隊列</param> /// <param name="message">發送內容</param> public static void DirectSend<T>(string queue, T message) where T : class { using (var bus = BusBuilder.CreateMessageBus()) { bus.Send(queue, message); } } /// <summary> /// 消息接收(direct) /// </summary> /// <typeparam name="T">消息類型</typeparam> /// <param name="queue">接收的隊列</param> /// <param name="callback">回調操做</param> /// <param name="msg">錯誤信息</param> /// <returns></returns> public static bool DirectReceive<T>(string queue, Action<T> callback, out string msg) where T : class { msg = string.Empty; try { var bus = BusBuilder.CreateMessageBus(); bus.Receive<T>(queue, callback); } catch (Exception ex) { msg = ex.ToString(); return false; } return true; } /// <summary> /// 消息發送 /// <![CDATA[(direct EasyNetQ高級API)]]> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <param name="msg"></param> /// <param name="exChangeName"></param> /// <param name="routingKey"></param> /// <returns></returns> public static bool DirectPush<T>(T t, out string msg, string exChangeName = "direct_mq", string routingKey = "direct_rout_default") where T : class { msg = string.Empty; try { using (var bus = BusBuilder.CreateMessageBus()) { var adbus = bus.Advanced; var exchange = adbus.ExchangeDeclare(exChangeName, ExchangeType.Direct); adbus.Publish(exchange, routingKey, false, new Message<T>(t)); return true; } } catch (Exception ex) { msg = ex.ToString(); return false; } } /// <summary> /// 消息接收 /// <![CDATA[(direct EasyNetQ高級API)]]> /// </summary> /// <typeparam name="T">消息類型</typeparam> /// <param name="handler">回調</param> /// <param name="exChangeName">交換器名</param> /// <param name="queueName">隊列名</param>