一、文档资料
1、官方网站:http://www.rabbitmq.com/
二、安装指南
1、下载并按照OTP(必须在安装RabbitMQ Server前安装):http://www.erlang.org/download.html
32位:OTP 17.5 Windows 32-bit Binary File (91.0 MB)
32位:OTP 17.5 Windows 32-bit Binary File (91.0 MB)
64位:OTP 17.5 Windows 64-bit Binary File (91.1 MB)
2、下载并安装RabbitMQ Server:http://www.rabbitmq.com/download.html
选择windows平台,直接从官方链接下载,同时也可以从github上下载,安装完成后,服务自动启动(services.msc)
3、下载并安装.Net客户端:http://www.rabbitmq.com/dotnet.html
http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v3.5.1/rabbitmq-dotnet-client-3.5.1-dotnet-4.0.zip
3、下载并安装.Net客户端:http://www.rabbitmq.com/dotnet.html
http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v3.5.1/rabbitmq-dotnet-client-3.5.1-dotnet-4.0.zip
(选择的是.net 4.0版本,同时也可以通过nuget下载)
三、命令行工具
1、定位到rabbitmq安装目录:
windows 7 cd C:Program Files (x86)RabbitMQ Server
abbitmq_server-3.5.1sbin
Widows Serve cd /d C:Program Files (x86)RabbitMQ Server
abbitmq_server-3.5.1sbin
2、执行rabbitmq命令行工具(rabbitmqctl ):
rabbitmqctl -q status //打印了一些rabbitmq服务状态信息,包括内存,硬盘,和使用erlong的版本信息
rabbitmqctl list_queues //查看所有队列消息
四、C#客户端使用
1、服务端代码
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); } } } } } }
2、客户端代码
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); } } } } }
如果启动服务端前启动了客户端,消息就会存放到队列中,da
五、RabbitMQ GUID使用
1、官方提供的一个web管理工具(rabbitmq_management)
2、安装了Rabbitmq后,默认也安装了该管理工具,执行命令即可启动
rabbitmq-plugins enable rabbitmq_management(先定位到rabbitmq安装目录)
3、启动后,直接在浏览器地址输入:http://localhost:15672/ 账号密码都是:guest
六、异常问题
1、None of the specified endpoints were reachable
生产端和消费端的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;