使用默認帳號:guest/guest登陸http://localhost:15672/#/進去,添加一個新用戶(Administrator權限),並設置其Permissionspa
新建兩個控制檯程序code
安裝RabbitMQ.Client:https://www.nuget.org/packages/RabbitMQ.Client/blog
生產者(producer)rabbitmq
static void Main(string[] args) { try { ConnectionFactory factory = new ConnectionFactory { UserName = "admin",//用戶名 Password = "123456",//密碼 HostName = "localhost"//rabbitmq ip }; //建立鏈接 var connection = factory.CreateConnection(); //建立通道 var channel = connection.CreateModel(); //聲明一個隊列 channel.QueueDeclare("hello", false, false, false, null); Console.WriteLine("\nRabbitMQ鏈接成功,請輸入消息,輸入exit退出!"); string input; do { input = Console.ReadLine(); var sendBytes = Encoding.UTF8.GetBytes(input); //發佈消息 channel.BasicPublish("", "hello", null, sendBytes); } while (input.Trim().ToLower() != "exit"); channel.Close(); connection.Close(); } catch(Exception ex) { Console.WriteLine(ex.Message); Console.Read(); } }
消費者(consumer)隊列
static void Main(string[] args) { ConnectionFactory factory = new ConnectionFactory { UserName = "admin",//用戶名 Password = "123456",//密碼 HostName = "localhost"//rabbitmq ip }; //建立鏈接 var connection = factory.CreateConnection(); //建立通道 var channel = connection.CreateModel(); //事件基本消費者 EventingBasicConsumer consumer = new EventingBasicConsumer(channel); //接收到消息事件 consumer.Received += (ch, ea) => { var message = Encoding.UTF8.GetString(ea.Body); Console.WriteLine($"收到消息: {message}");//確認該消息已被消費 channel.BasicAck(ea.DeliveryTag, false); // Console.WriteLine($"已發送回執[{ea.DeliveryTag}]"); }; //啓動消費者 設置爲手動應答消息 channel.BasicConsume("hello", false, consumer); Console.WriteLine("消費者已啓動"); Console.ReadKey(); channel.Dispose(); connection.Close(); }
在rabbitmq中,exchange有4個類型:direct,topic,fanout,header事件
direct類型ip
在direct類型的exchange中,只有這兩個routingkey徹底相同,exchange纔會選擇對應的binging進行消息路由。路由
//定義一個Direct類型交換機 channel.ExchangeDeclare(exchangeName, ExchangeType.Direct, false, false, null); //定義一個隊列 channel.QueueDeclare(queueName, false, false, false, null); //將隊列綁定到交換機 channel.QueueBind(queueName, exchangeName, routeKey, null);
fanout類型get
此exchange的路由規則很簡單直接將消息路由到全部綁定的隊列中,無須對消息的routingkey進行匹配操做。input
//定義一個Direct類型交換機 channel.ExchangeDeclare(exchangeName, ExchangeType.Fanout, false, false, null); //定義隊列1 channel.QueueDeclare(queueName1, false, false, false, null); //定義隊列2 channel.QueueDeclare(queueName2, false, false, false, null); //將隊列綁定到交換機 channel.QueueBind(queueName1, exchangeName, routeKey, null); channel.QueueBind(queueName2, exchangeName, routeKey, null);
topic類型
此類型exchange和上面的direct類型差很少,但direct類型要求routingkey徹底相等,這裏的routingkey能夠有通配符:'*','#'.
其中'*'表示匹配一個單詞, '#'則表示匹配沒有或者多個單詞
string routeKey = "TestRouteKey.*";
//定義一個Direct類型交換機 channel.ExchangeDeclare(exchangeName, ExchangeType.Topic, false, false, null); //定義隊列1 channel.QueueDeclare(queueName, false, false, false, null); //將隊列綁定到交換機 channel.QueueBind(queueName, exchangeName, routeKey, null); //發佈消息 channel.BasicPublish(exchangeName, "TestRouteKey.one", null, sendBytes);
以上就是exchange 類型的總結,通常來講direct和topic用來具體的路由消息,若是要用廣播的消息通常用fanout的exchange。
header類型用的比較少就很少介紹了