参考:https://blog.csdn.net/wulex/article/details/64127224 —— 操作指定集
https://www.cnblogs.com/hujiapeng/p/7352904.html —— centos7安装介绍
因为最近准备用rabbitmq, 理解了很久, 对于权限设置也想了很久。
我的需要很简单, 生产者以一定方式写入,并按指定的routing key 写入指定的queue。并且需要限制消费者只能向指定queue进行读消息的权限
前提:rabbitmq已提前装好
主要用到的指令:
1、分别创建一个两个用户:
rabbitmqctl add_user producer producer123
rabbitmqctl add_user consumer consumer123
2、创建vhost
rabbitmqctl add_vhost test
3、向producer分配vhost为test的所有的权限
rabbitmqctl set_permissions -p test producer ".*" ".*" ".*"
4、向consumer分配vhost为test的读权限
rabbitmqctl set_permissions -p test consumer ".*" "" ".*"
5、producder代码(nodejs, 前提:已装好amqp和rabbitmq的库。 npm install amqp --save; npm install rabbitmq --save)
let amqp = require("amqp");
let connection = amqp.createConnection({url:"amqp://producer:producer123@10.0.1.27:5672/test"});
connection.on('error', function(err){
console.log("error:", err);
})
connection.on('ready', function(){
console.log("ready");
connection.exchange("test", {type:"topic", autoDelete:false, durable:true}, function(exchange){
console.log(" create exchange success")
connection.queue("testque1", {durable:true, autoDelete:false},function(queue){
console.log("create que success");
queue.bind("test", "testrout1");
exchange.publish("testrout1", "test message5",{mandatory:true}, function(a, b){
console.log(a, b);
})
})
})
})
6、consumer测试代码
let amqp = require("amqp");
let connection = amqp.createConnection({url:"amqp://consumer:consumer123@10.0.1.27:5672/order"});
connection.on('ready', function(){
let que = connection.queue("testque1", {durable:true, autoDelete:false},function(queue){
queue.subscribe(function(message, header, deliveryInfo){
console.log(message.data.toString());
})
})
测试执行结果: