先来个官方教程
http://www.rabbitmq.com
在windows 下 命名 去掉sudo 我是在windows下测试 用net调用
常用命令
控制台命令:sudo rabbitmqctl#只能在root权限下使用
本地节点默认被命名为”rabbit”。可以通过这个命令前使 用”-n”标志明确的指定节点名称, 例如:
sudo rabbitmqctl -n rabbit@hostname **** 控制台命令
启动:sudo rabbitmq-server start
启动应用:sudo rabbitmqctl start_app
停止应用:sudo rabbitmqctl stop_app
停止rabbitmq:sudo rabbitmqctl stop
显示RabbitMQ中间件各种信息:sudo rabbitmqctl status
sudo rabbitmqctl force_reset
列出所有用户
删除虚拟主机
sudo rabbitmqctl delete_vhost vhostpath
sudo rabbitmqctl set_permissions [-p vhostpath] username regexp regexp regexp
example:sudo rabbitmqctl set_permissions -p vhostname testuser ".*" ".*" ".*"
sudo rabbitmqctl list_user_permissions username
列出全部队列,exchanger,binder
sudo rabbitmqctl list_queues [-p <vhostpath>] [<queueinfoitem> ...]
list_exchanges [-p <vhostpath>] [<exchangeinfoitem> ...]
list_bindings [-p <vhostpath>] [<bindinginfoitem> ...]
list_connections [<connectioninfoitem> ...]
list_channels [<channelinfoitem> ...]
list_consumers [-p <vhostpath>]
环境搭建
环境还是简单的说下
1、从官方下载http://www.rabbitmq.com/download.html 服务版本
2、没有erlang需要下载http://www.erlang.org/download.html
3、启动服务
4、验证是否成功
sudo rabbitmqctl status
简单消息(Hello World)
发送消息
static void Main(string[] args)
{
var factory = new ConnectionFactory();
factory.HostName = "Localhost";
factory.UserName = "shuzhengyi";
factory.Password = "shuzhengyi";
while (true)
{
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("hello", false, false, false, null);
string message = "Hello World";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "hello", null, body);
Console.WriteLine(" set {0}", message);
Console.ReadLine();
}
}
}
}
接受消息
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
factory.UserName = "shuzhengyi";
factory.Password = "shuzhengyi";
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
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);
}
}
}
}
运行结果