[生产问题]大神帮忙看看NODEJS请求TOMCAT很慢
发布于 1年前 作者 xuhaijinsky2008 901 次浏览

这两天纠结于一个生产问题,麻烦大神有时间帮忙看看。

1、server1:nodejs写的一个webserver,主要负责消息转发,逻辑很简单。 2、server2:tomcat起的一个webserver,负责业务处理

server1通过http请求到server2的时候,发现某种情况下会很慢,一个http请求出去,发现server2半小时后才收到,找不到原因,摸不到头脑,麻烦大神帮忙看看。

有两个问题来着: 1、为何这个请求迟迟不不到sip那边(server1上同时起了其他nodejs服务server3,server3到server2是正常的) 2、有没有中间件可以查询出来nodejs服务有哪些任务正在排队?

server1请求server2的代码如下:

/**

  • 执行发送信息给SIP

  • [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) microLetter 微信号

  • [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) type 类型

  • [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) content 内容:文本为文本内容;语音、图片为nas上文件路径

  • [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) messageId 消息ID

  • [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) callback 回调方法 */ var sendMsgToSipAction = exports.sendMsgToSipAction = function(messageObj, callback) { logger.info(‘准备发送消息给SIP’); var rtnObj = {};

    if (messageObj.type == constants.msg_type.msg_image || messageObj.type == constants.msg_type.msg_voice) { //图片或语音 //messageObj.content = (‘filepath=’ +messageObj.content); }

    var msg = JSON.stringify(messageObj); logger_sip.debug(‘发送给SIP区的消息内容:%s’ , msg);

    //post数据 var post_data = querystring.stringify({ //encodeURIComponent防止出现空格、特殊符号等问题,SIP端要decode message: encodeURIComponent(msg)

    });

    var options = { host: sysconf.sip_server.sip_host, port: sysconf.sip_server.sip_port, path: sysconf.sip_server.sip_path + '?’, method: 'POST’, headers: { 'Content-Type’: 'application/x-www-form-urlencoded’, 'Content-Length’: post_data.length } }; var reqToSip = http.request(options, function (resToSip) {

     var resStatus = resToSip.statusCode;
     logger.debug('发送消息给SIP返回STATUS:' , resStatus);
     //如果返回错误,需做处理
     resToSip.setEncoding('utf8');
     var all_data ='';
     resToSip.on('data', function (body) {
         all_data = all_data + body;
     });
     resToSip.on('end', function () {
         logger.debug('发送消息给SIP返回完整BODY:%s',all_data);
         if (resStatus == "200" && all_data) {
             try{
                 //SIP区回复内容不可信,防止把应用搞挂了
                 var bodyObj = JSON.parse(all_data);
                 if (bodyObj.resMsg.code == "SIP000") {
                     //sip区反馈处理成功
                     rtnObj.rtnFlag = "ok";
                     rtnObj.messageObj = messageObj;
                     callback(rtnObj);
                 }else if(bodyObj.resMsg.code == "SIP002"){
                   rtnObj.rtnFlag = "reply";
                     rtnObj.rtnMsg = bodyObj.resMsg.msg;
                     callback(rtnObj);
                 } else {
                     callback({rtnFlag:constants.rtn_flag.error,rtnMsg:'发送信息给SIP区发生异常:'+ all_data});
                 }
             }catch(e){
                 logger.error('发送信息给SIP区发生异常:', e.message);
                 callback({rtnFlag:constants.rtn_flag.error,rtnMsg:'发送信息给SIP区发生异常:'+ e.message});
                 return;
             }
    
    
         }else{
             logger.error("发送信息给SIP区发生异常。");
             rtnObj.rtnFlag = "error";
             rtnObj.rtnMsg = "发送信息给SIP区发生异常。";
             callback(rtnObj);
             return;
         }
    
     });
    

    });

    reqToSip.write(post_data); reqToSip.end(); reqToSip.on('error’, function (e) { logger.error(“系统异常,未成功发送到服务人员,请稍后再试。:” + e.message); rtnObj.rtnFlag = "error"; rtnObj.rtnMsg = "系统异常,未成功发送到服务人员,请稍后再试。"; callback(rtnObj); return; }); }

4 回复

var reqToSip = http.request(options, function (resToSip) 这行堵住了,在这行前面打印日志是可以立马看到结果的

补充下:生产上nodejs版本是0.8.14;

nodejs的默认http连接池是多少?是不是当前nodejs服务的连接被用完了,再等待连接释放?

搞定了,加一行代码就行了。如果不设置的话,默认值连接数是5; 修改下最大连接数,nodejs的瓶颈就过去了。

require(‘http’).globalAgent.maxSockets = 1024

不错,学习了。我也正打算用这种架构,node负责消息转发,java负责处理后台逻辑。

回到顶部