将json从一个节点服务器传递到另一个 – 最有效的方法?

我是节点中的中间层。 我需要将JSON从一个node.js服务器传递到另一个node.js(两个服务器是相互独立的,用于不同的function)。 JSON数据可以在200字节到50千字节之间。 (高达500次的请求)

我正在使用http后,但它停止了node.js 0.12.7工作。 我无法降级节点,并冒着破坏其他function的风险。 所以我正在寻找另一种方法来传递两个服务器之间的JSON数据。

这是我的POST请求的代码:

function contactPushServerToSendMessage(recipients, entireMessage) { var post_options = { host: '<devServer>.cloudapp.net', port: '80', path: '/api/'+'sendMessage', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': entireMessage.length } }; console.log("with body: " + entireMessage); // Set up the request var post_req = http.request(post_options, function(res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('Response: ' + chunk); }); }); // post the data post_req.write(entireMessage); post_req.end(); } } 

这是这个返回的错误信息:

 Error: "name" and "value" are required for setHeader(). at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:333:11) at new ClientRequest (_http_client.js:101:14) at Object.exports.request (http.js:49:10) at contactPushServerToSendMessage (/home/azureuser/myAppServer/nodeServer.js:297:23) at /home/azureuser/myAppServer/nodeServer.js:65:8 at Layer.handle [as handle_request] (/home/azureuser/node_modules/express/lib/router/layer.js:95:5) at next (/home/azureuser/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/home/azureuser/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/azureuser/node_modules/express/lib/router/layer.js:95:5) at /home/azureuser/node_modules/express/lib/router/index.js:277:22 

请build议什么将是一个有效和可靠的方法来做到这一点。

提前致谢。

看看这个博客文章:

http://www.sebastianseilund.com/json-socket-sending-json-over-tcp-in-node.js-using-sockets

它详细介绍了如何使用套接字通过TCP传递JSON数据。

您始终可以使用一些队列解决scheme,例如AWS SQS,Kinesis等

正如你所提到的,你也可以使用POST请求,但是你不能保证到达目的地的请求。 如果你想要使用这种模式,请确保包含一些重试逻辑,如果其他服务器已收到消息,ACK响应等,这就是为什么使用某些服务更快。

SQS,您需要生产者和消费者的基于队列的解决scheme – https://aws.amazon.com/sqs/

Kinesis,允许实时stream消费 – https://aws.amazon.com/kinesis/