1.下载Erlang,并安装
http://www.erlang.org/download.html
2.下载安装rabbitmq
http://www.rabbitmq.com/download.html
3.配置环境
C:\Documents and Settings\Administrator>cd C:\Program Files\RabbitMQ Server\rabb itmq_server-3.5.6\sbin C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>rabbitmq-plugins.bat enable rabbitmq_management The following plugins have been enabled: mochiweb webmachine rabbitmq_web_dispatch amqp_client rabbitmq_management_agent rabbitmq_management Applying plugin configuration to rabbit@PHICOMM-20537FA... started 6 plugins. C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>net stop RabbitMQ && net start RabbitMQ RabbitMQ 服务正在停止. RabbitMQ 服务已成功停止。 RabbitMQ 服务正在启动 . RabbitMQ 服务已经启动成功。 C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>rabbitmqctl.bat list _users Listing users ... guest [administrator] C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>rabbitmqctl.bat list _vhosts Listing vhosts ... / C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>rabbitmqctl.bat add_ user wuchao 123456 Creating user "wuchao" ... C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>rabbitmqctl.bat list _users Listing users ... guest [administrator] wuchao [] C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>rabbitmqctl.bat set user_tags wuchao administrator Setting tags for user "wuchao" to [administrator] ... C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>rabbitmqctl.bat set permissions -p / wuchao ".*" ".*" ".*" Setting permissions for user "wuchao" in vhost "/" ... C:\Program Files\RabbitMQ Server\rabbitmq_server-3.5.6\sbin>rabbitmqctl.bat lis _users Listing users ... guest [administrator] wuchao [administrator]
4.登录管理工具
http://localhost:15672/
后台虚拟机
查看vhost信息,vhost主要用于业务划分,一个业务系统一个vhost比较合理,防止队列交叉,也可也add vhost
回到user页面,可也点击任意一个账号,进入详细页面,设置账号的控制权限 virtual Host:/ 或者 上一步add 的vhost,这样此user账号才可以连接队列操作
5.在.NET程序上使用RabbitMQ
Install-Package RabbitMQ.Client
如果无法下载,可以先网页下载https://www.nuget.org/packages/RabbitMQ.Client/3.5.6,再复制到vs中。
6.vs调用RabbitMQ Client
分send和receive
using System; using System.Collections.Generic; using System.Linq; using System.Text; using RabbitMQ.Client; namespace RabbitMQ { class Program { static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "118.193.188.59", UserName = "wuchao", Password = "123456", VirtualHost = "/" }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null); string message = "Hello World!"; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body); Console.WriteLine(" [x] Sent {0}", message); } Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using RabbitMQ.Client; using RabbitMQ.Client.Events; namespace RabbitMQReceive { class Program { static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "118.193.188.59", UserName = "wuchao", Password = "123456", VirtualHost = "/" }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "hello", 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); Console.WriteLine(" [x] Received {0}", message); }; channel.BasicConsume(queue: "hello", noAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } } }
参考:http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
7.将日志保存到数据库中
注:rabbitmq建议安装在linux上,Windows上极不稳定。