[英]How can I determine what sort of socket I should use to get messages from a RabbitMQ queue?
[英]How do I send messages to RabbitMQ Queue, using nodejs
我目前正在構建一個負載平衡工具,以對服務器如何處理 http 協議請求進行不同的壓力測量。 目前,我只能發送一噸,但我無法處理它們。 這就是為什么我希望使用 AMQP 將其全部放入像兔子一樣的消息隊列中。
我當前的代碼https://github.com/yugely/Stork
我目前正在使用事件循環和超時來充分完成我打算完成的工作。
我想通過使用我當前的記錄器之一來使用 RabbitMQ 將消息“發送”到消息隊列中。 我不知道如何模塊化它,所以我不必經常創建頻道,因為所有教程似乎只是相互復制粘貼,而沒有討論如何在外部文件中使用它。
我希望有人可以引導我做我可能被欺騙的事情。 我什至不確定如何問這個問題。 我一直在研究 RabbitMQ 和 AMQP 來處理項目的消息隊列。 我面臨的問題是我不知道如何向rabbit mq 發送消息。 讓我通過在 rabbitmq 站點上復制第一個教程來說明我的理解:
發送.js
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
var queue = 'hello';
var msg = 'Hello World!';
channel.assertQueue(queue, {
durable: false
});
/*
How do I do this outside the functions so receive gets it? How can I call a method/function to send a message to an existing queue through an existing channel?
*/
channel.sendToQueue(queue, Buffer.from(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function() {
connection.close();
process.exit(0);
}, 500);
});
接收.js
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
var queue = 'hello';
channel.assertQueue(queue, {
durable: false
});
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
/*
How do I set up a consumer outside this function?
*/
channel.consume(queue, function(msg) {
console.log(" [x] Received %s", msg.content.toString());
}, {
noAck: true
});
});
});
對於發件人,我是否總是為每條消息創建一個新頻道?
當您運行“node”終端命令時,所有教程都接受參數。 我目前看到這個工作的唯一方法是使用“子進程”庫,但那將是壞消息,對吧? 那不會創建另一個nodejs集群嗎?
如果我必須使用客戶端發送消息,我可以使用 axios 嗎? (我看到有些人聲稱他們能夠做到,但我不確定)。 既然將使用 amqp 協議,那么 amqp 客戶端是什么?
或者這些隊列是否在入口文件中實例化? 就像您在運行入口點命令時設置隊列,然后允許不同的“事件”向隊列發送消息一樣?
我該如何模塊化?
只是為了說明,這是我當前的 axios 代碼
RadioFlyer.js
await axios(flight.journal.actions[modkey]).then(function (response) {
reaction.key.type = messageType.HTTPResponse.Okay
reaction.message = response === undefined ? response : "no response"
let smaug = typeof reaction.message.status === "undefined" ? reaction.message : reaction.message.status
flight.journal.reactions.push(reaction)
let pulse = {
id: flight.id + "-" + index,
timestamp: Date.now(),
body: {
payload: {
protocol : reaction.protocol,
type: messageType.HTTPResponse.Okay,
url: flight.journal.actions[modkey].baseURL,
status: smaug
}
}
}
/*
Can I emit a messaging event to my logger
*/
//emit
seidCar.HTTPLogger.emit("logHttp", reaction)
//emit
seidCar.HeartbeatLogger.emit("pulse", pulse)
}).catch(function (error) {
reaction.key.type = messageType.HTTPResponse.Error
reaction.message = error.response === undefined ? error.code : error.response
let smaug = typeof reaction.message.status === "undefined" ? reaction.message : reaction.message.status
let pulse = {
id: flight.id + "-" + index,
timestamp: Date.now(),
body: {
payload: {
protocol : reaction.protocol,
type: messageType.HTTPResponse.Error,
url: flight.journal.actions[modkey].baseURL,
status: smaug
}
}
}
let err = {
id: flight.id+"-"+index+"-ERROR",
timestamp : Date.now(),
fatal : false,
potentialFix : "Examine Http Call with id: " + flight.id + "-" + index,
body: {
payload: {
protocol : reaction.protocol,
type: messageType.HTTPResponse.Error,
url: flight.journal.actions[modkey].baseURL,
status: smaug
}
}
}
flight.journal.reactions.push(reaction)
//emit
seidCar.HTTPLogger.emit("logHttp", reaction)
//emit
seidCar.ErrorLogger.emit("logError", err)
//emit
seidCar.HeartbeatLogger.emit("pulse", pulse)
})
並讓我的記錄器處理程序發送到隊列?
HTTPLogger.js
/*
Can I now send the message to the queue here, in this file?
*/
const HTTPEventLogger = require("events")
const emitter = new HTTPEventLogger()
const errorEmitter = require("./ErrorLogger").Emitter
class HTTPLogger extends HTTPEventLogger {
logHttp(message) {
switch (message.key.type) {
case "ERROR":
if (message !== undefined) {
console.log(message)
} else {
errorEmitter.emit("tossIt", {
error:"HTTP error Message is undefined in ~/Homestasis/Agent/HTTPLoggerjs.",
poi:"check for recent additions to pilot agents in ~/Pilots/Agent",
timestamp: Date.now(),
potentialFix:"look to where you are emitting the message. Function Scope"
})
}
break;
case "OKAY":
if (message !== undefined) {
console.log(message)//bState.message.status)
} else {
errorEmitter.emit("tossIt", {
error:"HTTP okay Message is undefined in ~/Homestasis/Agent/HTTPLoggerjs.",
poi:"check for recent additions to pilot agents in ~/Pilots/Agent",
timestamp: Date.now(),
potentialFix:"look to where you are emitting the message. Function Scope"
})
}
break;
default:
errorEmitter.emit("tossIt", "this is a tossIt error log. No http key type was caught bSate = " + bState)
}
}
}
var logger = new HTTPLogger()
emitter.on("logHttp",logger.logHttp)
exports.Emitter = emitter
這就是我想發送消息的方式。
我希望能夠以同樣的方式接收它
鑒於我如何看待它目前的工作方式,我不確定如何實施它,而且我錯過了等式的關鍵部分。 感謝您的時間!
我決定不使用RabbitMQ。
對於節點,RSMQ 是我要使用的。 我意識到我從根本上需要改變我對消息隊列實際工作的看法,以使它們實際工作。 我正在使用 RSMQ,因為我可以理解它如何適合我構建我的項目的方式。 起初我打算使用 ZeroMQ(我仍然可以,類固醇套接字的柔韌性)。 但是,當我真正考慮像“主線神經”一樣使用它時,我確實喜歡實際的功能和東西。
我想構建它的方式是這樣的:
Axios 進行調用,觸發一個事件到 httplogger,觸發一個事件到消息隊列。 所以:
RadioFlyer.js > HttpLogger.js > RSMQ-Messagequeue.js > 下一步 > 和下一步...
RSMQ-Messagequeue.js 充當“中間人”或中介,在超載時停止工作。
RSMQ 的優點是我還打算實現一個 Redis 緩存(我可以實現 ZeroMQ 並做很多相同的事情),但我喜歡 RSMQ 允許更多隊列選項,而不是 1 個具有許多主題的隊列。
有一個更充實的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.