简体   繁体   English

node.js表示集群和高CPU使用率

[英]node.js express cluster and high CPU usage

My node.js app uses express, socket.io and talks to mongodb through mongoose. 我的node.js应用程序使用express,socket.io并通过mongoose与mongodb对话。 All these are working fine with low cpu usage. 所有这些工作正常,CPU使用率低。 When I made the app run with cluster, it works fine, but the CPU usage really goes very high. 当我使用集群运行应用程序时,它运行正常,但CPU使用率确实非常高。 Here is what i am doing. 这就是我在做的事情。

var settings = require("./settings"),
    cluster = require('cluster');

cluster('./server')
  .use(cluster.logger('logs'))
  .use(cluster.stats())
  .use(cluster.pidfiles('pids'))
  .use(cluster.cli())
  .use(cluster.repl(8888))
  .listen(7777);

When I check the master.log, I see 当我检查master.log时,我明白了

[Fri, 21 Oct 2011 02:59:51 GMT] INFO master started
[Fri, 21 Oct 2011 02:59:53 GMT] ERROR worker 0 died
[Fri, 21 Oct 2011 02:59:53 GMT] INFO spawned worker 0
[Fri, 21 Oct 2011 02:59:54 GMT] ERROR worker 0 died
[Fri, 21 Oct 2011 02:59:54 GMT] INFO spawned worker 0
[Fri, 21 Oct 2011 02:59:56 GMT] ERROR worker 0 died
[Fri, 21 Oct 2011 02:59:56 GMT] INFO spawned worker 0
.....

[Fri, 21 Oct 2011 03:11:08 GMT] INFO spawned worker 0
[Fri, 21 Oct 2011 03:11:10 GMT] WARNING shutting down master
[Fri, 21 Oct 2011 03:12:07 GMT] INFO spawned worker 0
[Fri, 21 Oct 2011 03:12:07 GMT] INFO spawned worker 1
[Fri, 21 Oct 2011 03:12:07 GMT] INFO master started
[Fri, 21 Oct 2011 03:12:09 GMT] ERROR worker 1 died
[Fri, 21 Oct 2011 03:12:09 GMT] INFO spawned worker 1
[Fri, 21 Oct 2011 03:12:10 GMT] ERROR worker 1 died
[Fri, 21 Oct 2011 03:12:10 GMT] INFO spawned worker 1

In workers.access.log, I see all console messages, socket.io logs etc... 在workers.access.log中,我看到所有控制台消息,socket.io日志等...

In workers.error.log, I see the following error messages, looks like something wrong... 在workers.error.log中,我看到以下错误消息,看起来有些不对劲......

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: EADDRINUSE, Address already in use
    at HTTPServer._doListen (net.js:1106:5)
    at net.js:1077:14
    at Object.lookup (dns.js:153:45)
    at HTTPServer.listen (net.js:1071:20)
    at Object.<anonymous> (/cygdrive/c/HTML5/RENT/test/server/server.js:703:5)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at require (module.js:346:19)

server.js:703 - points to app.listen(9999); server.js:703 - 指向app.listen(9999);

EDIT: server.js code 编辑:server.js代码

var express = require("express"),
    fs = require("fs"),
    form = require('connect-form'),
    app = module.exports = express.createServer(
        form({ keepExtensions: true })
    ),
    sys = require("sys"),
    RentModel = require("./rent_schema"),
    UserModel   = require("./track_schema"),
    email   = require("./email_connect"),
    SubscriptionModel = require("./subscription_schema"),
    io = require("socket.io"),
    fb = require('facebook-js'),
    Twitter = require('./Twitter_Analysis'),
    Foursquare = require('./Foursquare_Analysis'),
    YQL = require("yql"),
    settings = require("./settings");
//    


 var cluster = require('cluster');
 cluster(app)
  .use(cluster.logger('logs'))
  .use(cluster.stats())
  .use(cluster.pidfiles('pids'))
  .use(cluster.cli())
  .use(cluster.debug())
  .use(cluster.repl(settings.ADMIN_PORT))
  .listen(settings.PORT);



socket = io.listen(app);
.....
.....
//app.listen(settings.PORT);

It looks like you're trying to bind your workers with the same port, that is crashing the workers, but cluster is restarting the workers. 看起来您正试图将您的工作人员绑定到同一个端口,这会导致工作人员崩溃,但群集正在重新启动工作人员。 So you're in an infinite death cycle. 所以你处在一个无限的死亡循环中。

I'm not sure if you need the app.listen(9999) in your server.js file, which is probably trying to bind port 9999 in all your workers. 我不确定你的server.js文件中是否需要app.listen(9999),这可能是试图在所有工作者中绑定端口9999。 See the examples in the cluster package for a good example: https://github.com/LearnBoost/cluster/blob/master/examples/express.js 请参阅集群包中的示例以获取一个很好的示例: https//github.com/LearnBoost/cluster/blob/master/examples/express.js

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

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