[英]Sending Many POST requests simultaneously Nodejs
我是 Nodejs 的新手,所以请原谅我的任何错误.. :)
让我解释一下我要做什么:
基本上我正在为我的平台制作推送通知服务..我将进一步解释..我有两个 NodeJs 服务器(使用 express):
SERVER 1 :它从数据库中获取所需的一切,例如(设备注册、标识符..),并应发送到第二个服务器。
SERVER 2 :此服务器接收 JSON(包含我需要的所有内容)以创建 FCM 和 APNS 有效负载,然后发送到方便的提供程序(FCM、APNS)。
我在使用什么:我正在使用axios发送 POST 请求。
问题:由于第一台服务器将同时发送大量请求(通常为 5K 或更多——它是动态的),axios 无法处理,我尝试了许多其他 axios 替代方案,但面临同样的问题。
我的问题:我怎样才能毫无问题地发送那么多请求?
PS:当我发送很少的请求(100 个或更多)时,我没有遇到任何错误......
我希望一切都清楚,我真的很感激任何帮助。
使用 Axios 请求的代码示例:
PS:它总是落在“[请求错误] ...”
try
{
axios.post(endpoint,{sendPushRequest})
.then( response => {
console.log(response.data);
})
.catch( er => {
if (er.response) {
console.log("[Response Error] on sending to Dispatcher...");
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(er.response.data);
console.log(er.response.status);
console.log(er.response.headers);
} else if (er.request) {
console.log("[Request Error] on sending to Dispatcher...");
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
} else {
// Something happened in setting up the request that triggered an Error
console.log('[Error]', er.message);
}
console.log(er.config);
});
}
catch (e) {
console.log('[ Catch Error]', e);
}
通常,为了做这种异步的事情,你应该使用任何排队服务,就好像第二个服务器很忙一样,在处理如此大量的休息 API 的情况下,你的用户可能会错过通知。
你的流程应该是这样的:
SERVER 1:它从数据库中获取所需的一切,例如(设备注册、标识符..),并且应该推送/发布到任何排队服务,例如 rabbitMQ/Google pubsub 等。
服务器 2:该服务器应该从队列中递归地提取消息,然后接收 JSON(包含我需要的所有内容)以创建 FCM 和 APNS 有效负载,然后发送到方便的提供程序(FCM、APNS),而不是使用其余的 API。
这是有益的,因为即使您的服务器发生任何事情,例如忙/崩溃,消息也会保留在队列中,并且在重新启动服务器时,您将能够完成您的工作(发送通知或其他)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.