同时发送许多 POST 请求 Nodejs
Sending Many POST requests simultaneously Nodejs
我是 Nodejs 新手,如有任何错误,请多多包涵。:)
让我解释一下我想做什么:
基本上我正在为我的平台做一个推送通知服务..我会进一步解释..
我有两个 NodeJs 服务器(使用 express):
服务器 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,你的用户可能会错过通知。
你的流程应该是这样的:
服务器 1:它从数据库中获取所需的一切,例如(设备注册、标识符 ..)并且应该 push/publish 到任何排队服务 例如 rabbitMQ/Google 发布订阅等
服务器 2:此服务器应递归地从队列中拉取消息,然后接收 JSON(包含我需要的所有内容)以创建 FCM 和 APNS 负载,然后发送到方便的提供商(FCM、APNS)。
这是有益的,因为即使您的服务器发生任何事情,例如 busy/crashes,消息仍会保留在队列中,并且在重新启动服务器时您将能够完成您的工作(发送通知或其他)。
我是 Nodejs 新手,如有任何错误,请多多包涵。:)
让我解释一下我想做什么:
基本上我正在为我的平台做一个推送通知服务..我会进一步解释.. 我有两个 NodeJs 服务器(使用 express):
服务器 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,你的用户可能会错过通知。
你的流程应该是这样的:
服务器 1:它从数据库中获取所需的一切,例如(设备注册、标识符 ..)并且应该 push/publish 到任何排队服务 例如 rabbitMQ/Google 发布订阅等
服务器 2:此服务器应递归地从队列中拉取消息,然后接收 JSON(包含我需要的所有内容)以创建 FCM 和 APNS 负载,然后发送到方便的提供商(FCM、APNS)。
这是有益的,因为即使您的服务器发生任何事情,例如 busy/crashes,消息仍会保留在队列中,并且在重新启动服务器时您将能够完成您的工作(发送通知或其他)。