本文轉載自:https://www.cnblogs.com/gossip/p/4475978.htmlhtml
windows安裝手冊請參考:http://www.rabbitmq.com/install-windows-manual.htmlgit
1、文檔資料github
namespace Server { class Program { static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { //定義隊列(hello爲隊列名) channel.QueueDeclare("hello", false, false, false, null); var consumer = new QueueingBasicConsumer(channel); channel.BasicConsume("hello", true, consumer); Console.WriteLine(" [*] Waiting for messages." + "To exit press CTRL+C"); while (true) { //接受客戶端發送的消息並打印出來 var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue(); var body = ea.Body; var message = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] Received {0}", message); } } } } } }
namespace Client { class Program { static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { //定義隊列(hello爲隊列名) channel.QueueDeclare("hello", false, false, false, null); //發送到隊列的消息,包含時間戳 string message = "Hello World!" + "_" + DateTime.Now.ToString(); var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish("", "hello", null, body); Console.WriteLine(" [x] Sent {0}", message); } } } } }
6、異常問題web
一、None of the specified endpoints were reachablewindows
生產端和消費端的factory參數要統一瀏覽器
var factory = new ConnectionFactory();
factory.UserName = QueueSetttiong.UserName; //用戶名,對應Management工具的admin-->user
factory.Password = QueueSetttiong.Password; //密碼,對應Management工具的admin-->密碼
factory.HostName = QueueSetttiong.HostName; //本地部署服務直接用hostname便可
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
factory.VirtualHost = QueueSetttiong.VirtualHost; //使用默認值: "/"
factory.Protocol = Protocols.DefaultProtocol;工具