簡體   English   中英

將json從一台節點服務器傳遞到另一台節點服務器-最有效的方法?

[英]pass json from one node server to another - most efficient way?

我是節點中級。 我需要將JSON從一台node.js服務器傳遞到另一台node.js(這兩個服務器彼此獨立,並且具有不同的功能)。 JSON數據的長度在200字節到50千字節之間。 (每秒最多500個請求)

我正在使用http post,但是它停止在node.js 0.12.7上工作。 我無法降級節點,並且冒着破壞其他某些功能的風險。 因此,我正在尋找另一種在兩個服務器之間傳遞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

請提供建議,這將是一種有效而可靠的方法。

提前致謝。

看看這篇博客文章:

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

它詳細介紹了如何使用套接字通過TCP傳遞JSON數據。

您始終可以使用一些隊列解決方案,例如AWS SQS,Kinesis等。

如前所述,您也可以使用POST請求,但是無法保證請求到達目的地。 如果確實要使用此模式,請確保包括一些重試邏輯,如果其他服務器已收到消息的ACK響應等。這就是為什么為此使用某些服務更快的原因。

SQS,基於隊列的解決方案,你需要生產者和消費者- https://aws.amazon.com/sqs/

室壁運動,允許實時流消費- https://aws.amazon.com/kinesis/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM