带有MSSQL的Node JS可伸缩服务器

时间:2015-05-17 10:36:31

标签: sql-server node.js scalability

我正在尝试实现可扩展的生产服务器,它结合了Node JS和MSSQL vis node-mssql。在最简单的实现中,它需要监听5000个并发客户端(调制解调器),这些客户端将每5秒“不一定同时”发送信息。服务器将处理并存储SQL数据库中的数据。为此,将在下面创建一个模拟客户端脚本。它在几百个客户端的限制下工作正常。在此之上,下面的错误显示:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: read ECONNRESET     //alternatively it s a 'write'
    at errnoException (net.js:900:11)
    at TCP.onread (net.js:555:19)

客户是:

var net = require('net');
var HOST = '127.0.0.1';
var PORT = 8901;

var clients = [];

for (var i=0; i <  100  ; i++ )
{

 var socket  = new net.Socket();
 socket.connect(PORT, HOST, function() { });    
 clients.push(socket);  

 }
writeThings();


function writeThings()
{
// var starting= new Date().getTime()  ;
for (var c=0; c <  clients.length    ; c++)
{
    clients[c].write('message from: ' + c + ',message text' );
    //clients[c].end();
}
 //var ending = new Date().getTime() ; 
    //console.log(ending - starting);
    setTimeout(writeThings, 5000);
}

服务器代码:

var sql = require('mssql'); 
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 8901;

var sqlconfig = 
{
user: ' ',
password: ' ',
server: ' ',  
database: ' ...' ,
pool: {
    max: 100,
    min: 20,
    idleTimeoutMillis: 3000
     }
} // end sql config

var connection = new sql.Connection(sqlconfig, function(err) {});
var request = new sql.Request(connection);
request.stream = true;

net.createServer(function(sock) {
sock.on('data', function(unitMessage) {  

console.log(sock.remoteAddress + ' : ' + sock.remotePort + ' : ' +    unitMessage.toString());
  console.log('\r\n');
  request.input('message', sql.VarChar(120) , unitMessage);
  request.execute('updateSkyLog', function(err, recordsets, returnValue) {     /* you can do things here  */ }); 
  request.on('error', function(err) { console.log('Error: ' + err.message);        }); 


 });

 sock.on('close',function(client){ console.log('connection closed by client'); });
 sock.on('error' , function(){ console.log('error on connection')});

 }).listen(PORT, HOST);

 console.log('Server started ... @' + HOST + ':' + PORT);

任何指向可以处理此数量的客户端和数据的真实世界脚本的任何人都表示赞赏。

0 个答案:

没有答案