环境:
[root@iZj6cbkwacooznwuigmo49Z ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@iZj6cbkwacooznwuigmo49Z ~]# uname -a
Linux iZj6cbkwacooznwuigmo49Z 2.6.32-696.16.1.el6.x86_64 #1 SMP Wed Nov 15 16:51:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
1.YUM安装:
wget http://mirrors.ustc.edu.cn/fedora/epel//6/x86_64/epel-release-6-8.noarch.rpmyum install epel-release-6-8.noarch.rpm
yum install epel-release -y
yum install rabbitmq-server -y
添加环境变量:
vim /root/.bash_profile
PATH=$PATH:$HOME/bin:/usr/lib/rabbitmq/bin
export PATH
最后安装的版本:
[root@iZj6cbkwacooznwuigmo49Z ~]# rpm -qa | grep rabbitmq
rabbitmq-server-3.1.5-1.el6.noarch
启动服务:
[root@master ~]# systemctl start rabbitmq-server
[root@node130-master rabbitmq]# lsof -i:5672
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
beam 8039 rabbitmq 14u IPv6 60203 0t0 TCP *:amqp (LISTEN)
安装管理插件:
rabbitmq-plugins enable rabbitmq_management
配置超级管理员:
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
删除默认用户:
rabbitmqctl delete_user guest
通过浏览器访问:
http://rabbitmq-server:15672/
角色与授权:
用户分类
Rabbitmq用户角色分为五类:
(1) 超级管理员(administrator)
可登陆管理控制台,可查看所有的信息,并且可以对用户,策略(policy)进行操作。
(2) 监控者(monitoring)
可登陆管理控制台,同时可以查看rabbitmq节点的相关信息(进程数,内存使用情况,磁盘使用情况等)
(3) 策略制定者(policymaker)
可登陆管理控制台, 同时可以对policy进行管理。
(4) 普通管理者(management)
仅可登陆管理控制台,无法看到节点信息,也无法对策略进行管理。
(5) 其他的
无法登陆管理控制台,通常就是普通的生产者和消费者
列出用户(有一个默认账号,用户名和密码都是:guest)
$ rabbitmqctl list_users
Listing users ...
guest [administrator]
新增用户
新建用户 admin 密码 123456
rabbitmqctl add_user admin 123456
Adding user "admin" ...
分配用户角色
给 admin 分配administrator角色
rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...
显示所有用户
rabbitmqctl list_users
Listing users ...
admin [administrator]
guest [administrator]
修改密码
rabbitmqctl change_password admin 654321
Changing password for user "admin" ...
删除用户
rabbitmqctl.bat add_user chy 123456
Adding user "chy" ...
rabbitmqctl delete_user chy
Deleting user "chy" ...
RabbitMQ常用命令
开启浏览器访问http://localhost:15672
默认userName:guest password:guest
创建用户
rabbitmqctl.bat add_user leimiaotang 123456
授权角色
(1) 超级管理员(administrator)
可登陆管理控制台(启用management plugin的情况下),可查看所有的信息,并且可以对用户,策略(policy)进行操作。
(2) 监控者(monitoring)
可登陆管理控制台(启用management plugin的情况下),同时可以查看rabbitmq节点的相关信息(进程数,内存使用情况,磁盘使用情况等)
(3) 策略制定者(policymaker)
可登陆管理控制台(启用management plugin的情况下), 同时可以对policy进行管理。
(4) 普通管理者(management)
仅可登陆管理控制台(启用management plugin的情况下),无法看到节点信息,也无法对策略进行管理。
(5) 其他的
进入到sbin目录下执行命令
rabbitmq-server stop
rabbitmq-server start
生产者
class Program
{
static void Main(string[] args)
{
DirectExchangeSendMsg();
}
/// <summary>
/// 连接配置
/// </summary>
private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
{
HostName = "192.168.1.28",
UserName = "admin",
Password = "admin"
};
/// <summary>
/// 路由名称
/// </summary>
const string ExchangeName = "justin.exchange";
//队列名称
const string QueueName = "justin.queue";
public static void DirectExchangeSendMsg()
{
using (IConnection conn = rabbitMqFactory.CreateConnection())
{
using (IModel channel = conn.CreateModel())
{
channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName);
var props = channel.CreateBasicProperties();
props.Persistent = true;
string vadata = Console.ReadLine();
while (vadata != "exit")
{
var msgBody = Encoding.UTF8.GetBytes(vadata);
channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: msgBody);
Console.WriteLine(string.Format("***发送时间:{0},发送完成,输入exit退出消息发送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
vadata = Console.ReadLine();
}
}
}
}
}
消费者
static void Main(string[] args)
{
DirectAcceptExchangeEvent();
}
/// <summary>
/// 连接配置
/// </summary>
private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
{
HostName = "192.168.1.28",
UserName = "admin",
Password = "admin"
};
/// <summary>
/// 路由名称
/// </summary>
const string ExchangeName = "justin.exchange";
//队列名称
const string QueueName = "justin.queue";
public static void DirectAcceptExchangeEvent()
{
using (IConnection conn = rabbitMqFactory.CreateConnection())
{
using (IModel channel = conn.CreateModel())
{
channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var msgBody = Encoding.UTF8.GetString(ea.Body);
Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
//CreateDB,EntCode CreateDB,1234567890ABCDEF
string[] oper=msgBody.Split(',');
if (oper[0].Equals("CreateDB"))
{
CreateDB createDB = new CreateDB();
createDB.CreateEntDB(oper[1], @"C:/Project/src/Code/CSTFinance/Ent.sql");
}
};
channel.BasicConsume(QueueName, true, consumer: consumer);
Console.WriteLine("按任意值,退出程序");
Console.ReadKey();
}
}
}
}