- 下载安装
- 启用管理工具
- 打开 RabbitMQ Command Prompt (sbin dir) 输入命令:
rabbitmq-plugins enable rabbitmq_management - 停止:net stop RabbitMQ
启动:net start RabbitMQ - 在浏览器中输入地址查看:http://127.0.0.1:15672/
- 使用默认账号登录:guest/ guest
- 打开 RabbitMQ Command Prompt (sbin dir) 输入命令:
-
Install-Package RabbitMQ.Client
- 生产消息
var factory = new ConnectionFactory() { HostName = "localhost", UserName = "guest", Password = "guest", }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "writeLog", durable: false, exclusive: false, autoDelete: false, arguments: null); for (int i = 0; i < 8000; i++) { string message = i.ToString()+","+Thread.CurrentThread.ManagedThreadId; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: "writeLog", basicProperties: null, body: body); Console.WriteLine("Program Sent {0}", message); } }
- 消费消息
var factory = new ConnectionFactory() { HostName = "localhost", UserName = "guest", Password = "guest", VirtualHost = "/" }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "writeLog", durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); ExcuateWriteFile(message); Console.WriteLine(" Receiver Received {0}", message); }; channel.BasicConsume(queue: "writeLog", autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); }