标签:
Nodejs因其简单方便,在服务端应用市场也开始占有一席之地,其另外一个分支--socket.io(最后跟nodejs好像又要合并了),特别适合聊天室、白板(document collabration)、在线实时计算、计数器等应用,如果要支持大容量应用,就需要使用集群技术了,下面逐一讨论常见的socket.io集群方案。
在官网介绍的方案有使用ngix反向代理方案。这种方案比较简单不需要修改业务代码可以直接布署,通过iphash轮调算法保存用户分配到同一个进程。
vi /etc/nginx/conf/nginx.conf http { upstream io_nodes { ip_hash; server 127.0.0.1:6001; server 127.0.0.1:6002; server 127.0.0.1:6003; server 127.0.0.1:6004; } server { listen 3000; server_name io.yourhost.com; location / { #为支持转发WebSocket数据,加上upgrade头 proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_http_version 1.1; proxy_pass http://io_nodes; } } }
如果没有使用socket.io,则比较简单,例子如下 (原文 https://nodejs.org/api/cluster.html )
npm install cluster http
var cluster = require(‘cluster‘); var http = require(‘http‘); var numCPUs = require(‘os‘).cpus().length; if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on(‘exit‘, function(worker, code, signal) { console.log(‘worker ‘ + worker.process.pid + ‘ died‘); }); } else { // Workers can share any TCP connection // In this case it is an HTTP server http.createServer(function(req, res) { res.writeHead(200); res.end("hello world\n"); }).listen(8000); }
使用socket.io方案(原文 https://github.com/elad/node-cluster-socket.io)
yum install redis npm install cluster socket.io-redis express
var express = require(‘express‘), cluster = require(‘cluster‘), net = require(‘net‘), sio = require(‘socket.io‘), sio_redis = require(‘socket.io-redis‘); var port = 3000, num_processes = require(‘os‘).cpus().length; if (cluster.isMaster) { var workers = []; var spawn = function(i) { workers[i] = cluster.fork(); workers[i].on(‘exit‘, function(worker, code, signal) { console.log(‘respawning worker‘, i); spawn(i); }); }; for (var i = 0; i < num_processes; i++) { spawn(i); } var worker_index = function(ip, len) { if(ip==‘::1‘) return 0;//for mac os x var s = ‘‘; for (var i = 0, _len = ip.length; i < _len; i++) { if (ip[i] !== ‘.‘) { s += ip[i]; } } return Number(s) % len; }; var server = net.createServer({ pauseOnConnect: true }, function(connection) { var worker = workers[worker_index(connection.remoteAddress, num_processes)]; worker.send(‘sticky-session:connection‘, connection); }).listen(port); } else { var app = new express(); var server = app.listen(0, ‘localhost‘), io = sio(server); io.adapter(sio_redis({ host: ‘localhost‘, port: 6379 })); process.on(‘message‘, function(message, connection) { if (message !== ‘sticky-session:connection‘) { return; } server.emit(‘connection‘, connection); connection.resume(); }); //这里写你的业务代码 io.on(‘connection‘, function (socket) { socket.on(‘message‘, function (data) {}); }); }
集群后带来的主要问题就是异地服务器和多进程间的通讯问题,如果你的应用都是基于单进程颗粒的,则不需要考虑这个问题,如果你的信息在多进程则存在共享和通讯的问题,则集群后要小心处理。
官方建议的方案是将数据(事件)集中存储(可以存储在内存或redis等持久化介质里),然后各服务端从集中存储的数据池里获取数据,其已经实现了一个抽象层 Socket.io-Adapter,这个抽象层使用内存,不建议直接使用,这里有人实现的redis的例子Socket.io-Redis(地址),坏处是你需要先安装redis。这是使用demo
var io = require(‘socket.io‘)(3000); var redis = require(‘socket.io-redis‘); io.adapter(redis({ host: ‘localhost‘, port: 6379 }));
上面说的都是socket进程间的通讯,如果你要从socket.io进程发消息给非socket.io进程,如http,则需要另外一个中间件socket.io-emitter(地址)。例子:
var io = require(‘socket.io-emitter‘)({ host: ‘127.0.0.1‘, port: 6379 }); setInterval(function(){ io.emit(‘time‘, new Date); }, 5000);
本文主要讨论了socket.io与nodejs cluster的整合解决方案,让nodejs从单进程转成多进程,提高服务端程序的容量。为保证nodejs进程异常退出自动重启,建议安装第三方的forever模块,可以将nodejs应用做成service并退出时可以自动重启。当然也可以结合kabbix实现微信报警(具体实现参考我的另外一文章)。
参考
Using multiple nodes
http://socket.io/docs/using-multiple-nodes/
Real Time Chat With NodeJS, Socket.io and ExpressJS
http://code.tutsplus.com/tutorials/real-time-chat-with-nodejs-socketio-and-expressjs--net-31708
sticky-session
https://github.com/indutny/sticky-session
Create a Cluster Server with Node.js and socket.IO
http://www.html5gamedevs.com/topic/12321-create-a-cluster-server-with-nodejs-and-socketio/
Scaling Socket.IO to multiple Node.js processes using cluster
<作者 朱淦 350050183@qq.com 2015.11.8>
标签:
原文地址:http://my.oschina.net/swingcoder/blog/527648