我可以从我的 NodeJS 应用动态创建 RabbitMQ 铲吗?
Can I dynamically create RabbitMQ shovel from my NodeJS app?
RabbitMQ 铲子插件提供了一个 HTTP API 来创建和配置铲子。假设我已经为我的 RabbitMQ 服务器实例启用了铲子和铲子管理插件,我可以从我的 NodeJS 应用程序动态创建铲子吗?
我目前使用 amqplib 连接到交易所。 https://www.squaremobius.net/amqp.node/channel_api.html
但是我没有看到任何 API 动态创建铲子。这是可以实现的还是有任何其他库支持这个?
amqplib does not provide any specific method to create dynamically shovels, but you can do that using the Management HTTP-based API as specified in the docs: https://www.rabbitmq.com/shovel-dynamic.html#tutorial
因此,在纯 Node.js 中,这是以编程方式创建铲子的 HTTP 请求(PUT 方法)示例:
var http = require('http');
var rabbit_host = "myrabbitmq.com";
var token = Buffer.from("admin:admin_pwd").toString('base64');
var shovel_name = "my_shovel";
var payload = {
"component": "shovel",
"vhost": "/",
"name": "my_shovel",
"value": {
"src-uri": "amqp://user1:pwd1@myrabbitmq.com",
"src-exchange": "test",
"dest-uri": "amqp://user2:pwd2@anotherbroker.com",
"dest-exchange-key": "test2",
"add-forward-headers": false,
"ack-mode": "on-confirm",
"delete-after": "never"
}
};
var options = {
"host": rabbit_host,
"port": 15672,
"path": "/api/parameters/shovel/%2F/" + shovel_name,
"method": "PUT",
"headers": {
"Authorization" : "Basic " + token,
"Content-Type" : "application/json",
}
}
var callback = function(response) {
var str = ''
response.on('data', function(chunk){
str += chunk;
});
response.on('end', function(){
console.log("end: response="+str);
});
}
var body = JSON.stringify(payload);
http.request(options, callback).end(body).on('error', function(e) {
console.log("error: " + e.message);
});
显然主机 "myrabbitmq.com" 必须可以从端口 15672 上的 Node.js 脚本访问。
上面的示例创建了一个从 "myrabbitmq.com" 到 "anotherbroker.com" 的铲子,指定了路由消息的交换。
RabbitMQ 铲子插件提供了一个 HTTP API 来创建和配置铲子。假设我已经为我的 RabbitMQ 服务器实例启用了铲子和铲子管理插件,我可以从我的 NodeJS 应用程序动态创建铲子吗?
我目前使用 amqplib 连接到交易所。 https://www.squaremobius.net/amqp.node/channel_api.html
但是我没有看到任何 API 动态创建铲子。这是可以实现的还是有任何其他库支持这个?
amqplib does not provide any specific method to create dynamically shovels, but you can do that using the Management HTTP-based API as specified in the docs: https://www.rabbitmq.com/shovel-dynamic.html#tutorial
因此,在纯 Node.js 中,这是以编程方式创建铲子的 HTTP 请求(PUT 方法)示例:
var http = require('http');
var rabbit_host = "myrabbitmq.com";
var token = Buffer.from("admin:admin_pwd").toString('base64');
var shovel_name = "my_shovel";
var payload = {
"component": "shovel",
"vhost": "/",
"name": "my_shovel",
"value": {
"src-uri": "amqp://user1:pwd1@myrabbitmq.com",
"src-exchange": "test",
"dest-uri": "amqp://user2:pwd2@anotherbroker.com",
"dest-exchange-key": "test2",
"add-forward-headers": false,
"ack-mode": "on-confirm",
"delete-after": "never"
}
};
var options = {
"host": rabbit_host,
"port": 15672,
"path": "/api/parameters/shovel/%2F/" + shovel_name,
"method": "PUT",
"headers": {
"Authorization" : "Basic " + token,
"Content-Type" : "application/json",
}
}
var callback = function(response) {
var str = ''
response.on('data', function(chunk){
str += chunk;
});
response.on('end', function(){
console.log("end: response="+str);
});
}
var body = JSON.stringify(payload);
http.request(options, callback).end(body).on('error', function(e) {
console.log("error: " + e.message);
});
显然主机 "myrabbitmq.com" 必须可以从端口 15672 上的 Node.js 脚本访问。
上面的示例创建了一个从 "myrabbitmq.com" 到 "anotherbroker.com" 的铲子,指定了路由消息的交换。