系统环境
win7/win10 x64
下载安装erlang
erlang官网下载不是一般的慢,这边提供快捷通道
安装完添加环境变量:E:\install\rabbitmq\erlang\erl9.3\bin(换成实际路径/bin)
测试win+r->cmd输入:erl,出现下图说明成功
下载安装rabbitmq server
同上和erlang打包好的下载地址
安装完添加环境变量:E:\install\rabbitmq\rabbitmq_server-3.7.7\sbin(换成实际路径/sbin)
测试win+r->cmd:输入rabbitmq-server(cmd控制台不能退出,退出则服务关闭)或者输入:rabbitmq-server –detached(在后台运行控制台关闭不会关闭服务,需要用rabbitmqctl stop关闭),出现下图说明成功启动rabbitmq server
添加用户
注:必须在服务开启的情况下才能添加
1.rabbitmq-server –detached // 启动服务
2.rabbitmqctl add_user kaychan root // rabbitmqctl add_user <username> <password> 添加用户
3.rabbitmqctl list_users // 查看当前所有用户
4.rabbitmqctl set_user_tags kaychan administrator // rabbitmqctl set_user_tags <username> <tag>,设置为管理员,否则无法登录
5.rabbitmqctl set_permissions -p / kaychan .* .* .* // rabbitmqctl set_permissions [-p <vhost>] <user> <conf> <write> <read>,设置权限,否则客户端连接不上
登录服务
打开网页:http://localhost:15672 ,输入用户名和密码登录
登录成功出现以下界面
C#编程测试
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
public class RabbitMQTest{
public IModel Channel;
public IConnection Conn;
public void TestRabbitMQServer() {
// 连接服务
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
factory.Port = 5672;
factory.UserName = "kaychan";
factory.Password = "root";
factory.VirtualHost = "/";
try {
Conn = factory.CreateConnection();
{
Channel = Conn.CreateModel();
{
Channel.ExchangeDeclare(exchange: "test_exchange", type: "direct", durable: true);
Channel.QueueDeclare("test_queue", true, false, false, null);
Channel.QueueBind(queue: "test_queue", exchange: "test_exchange", routingKey: "");
Console.WriteLine("rabbitmq succeed");
}
}
}
catch (Exception ex) {
Console.WriteLine(ex);
}
// 发送
string str = "Hello RabbitMQ.";
byte[] msg = Encoding.UTF8.GetBytes(str);
IBasicProperties properties = Channel.CreateBasicProperties();
properties.ContentEncoding = "UTF-8";
Channel.BasicPublish(exchange:"", routingKey: "test_queue", basicProperties: properties, body: msg);
Console.WriteLine("发送: {0}", str);
// 接收
var consumer = new EventingBasicConsumer(Channel);
Channel.BasicConsume("test_queue", false, consumer);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("接收: {0}", message);
};
}
}
调用运行结果如下