繁体   English   中英

node.js http服务器并发问题

[英]node.js http server concurrency issue

每当我尝试连续发送快速HTTP Post时,服务器有时会崩溃(java.net.ConnectException:连接被拒绝:connect),有时会死机(没有错误,但不能再使用它),并且有时可以工作。

如果我将HTTP Post缓慢发送到服务器,似乎根本没有问题。

我在node.js中有一个用于http服务器的简单代码-我的猜测是,有时NodeJS服务器会收到一个请求,然后在发出响应之前接收另一个请求,从而引起各种问题。 如何使服务器能够一次接受多个请求?

var server = http.createServer(function (req, res) { 

     if (req.method != 'POST') {
          res.end();
     }
     else {
          req.on('data', function(chunk) {
               //Do some stuff here
               file1=JSON.parse(chunk.toString());
               console.log("Hello World") ;
          }

          req.on('end', function() {
               res.writeHead(200, "OK", {'Content-Type': 'text/html'});
               res.end();
          });
     }
} server.listen(9000);

编辑这是发送HTTP POST的Java程序

 public static String httpUrlRequest(String requestURL, String json) {
       URL url;
       String response = "";
       HttpURLConnection connection = null;
       InputStream is = null;
       try {
           url = new URL(requestURL);

           connection = (HttpURLConnection) url.openConnection();
           connection.setDoOutput(true);
           connection.setRequestMethod("POST");
           connection.getOutputStream().write(json.getBytes());
           connection.getOutputStream().flush();
           connection.getOutputStream().close();
           int code = connection.getResponseCode();
           System.out.println("code" + code);

       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           connection.disconnect();
       }
       return response;
    }

    public static void main(String[] args) {
        Date date = new Date();
       Gson gson = new Gson();
       Map<String, Object> tempMap = gson.fromJson(json, new TypeToken<Map<String, Object>>(){}.getType());

       for(int i = 0; i < 10; i++){
           date = new Date();
           tempMap.put("GetOn", getDateString(date));
           httpUrlRequest("http://111.111.11.111:9000" ,gson.toJson(tempMap));
       }

    }

更新:如果我在nodejs服务器中解析JSON,则有时会收到错误,拒绝连接。

因此,由于某种原因,当我解析请求数据时,nodejs无法接收发送的整个json文件(5KB)。 相反,它仅接收一半,而我的Java控制台显示连接错误。 并且在nodejs正确解析大约3到5个请求之后,就会发生此问题。 然后,在下一个请求时,一切都会出错。 如何判断Java是否断开连接导致仅发送一半的JSON,或者是否仅发送一半的JSON导致nodejs崩溃,最终导致连接错误。

如果我注释掉所有解析,那么我再也不会收到错误了。 我什至不明白为什么nodejs中的JSON.Parse会引发Java连接错误....

您的问题出在这里:

 req.on('data', function(chunk) {
   //Do some stuff here
   file1=JSON.parse(chunk.toString());
   console.log("Hello World") ;
 }

由于可以在多个数据包中发送正文,因此您可能会收到多个'data'事件,从而尝试解析不完整的JSON。 尝试以下方法:

var chunks = [];

req.on('data', function(chunk) {
  chunks.push(chunk);
}

req.on('end', function () {
  // assemble all chunks
  var body = Buffer.concat(chunks).toString(),
      file1 = JSON.parse(body);

  res.writeHead(200, "OK", {'Content-Type': 'text/html'});
  res.end();
});

在这种测试过程中,很容易通过连接端口达到最大吞吐量。

  1. http://www.slideshare.net/sh1mmer/a-million-connections-and-beyond-nodejs-at-scale
  2. https://groups.google.com/forum/#!topic/nodejs/cRRS7ZJkyzc

简单的解决方案是在运行node.js之前ulimit -n 100000 但是绝对不要在生产中这样做,如果需要处理群集,最好考虑使用群集进行真正的大型连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM