SCC简介:SCC是一组服务,可以轻松地将SocketCluster部署和扩展到任意数量的机器
SCC由以下服务组成:
socketcluster https://github.com/SocketCluster/socketcluster
scc-broker https://github.com/SocketCluster/scc-broker
scc-state https://github.com/SocketCluster/scc-state
1.下载代码
git clone https://github.com/SocketCluster/scc-state
git clone https://github.com/SocketCluster/scc-broker
git clone https://github.com/SocketCluster/socketcluster
2.切换到每个代码中执行 npm install
3.启动状态服务器scc-state
cd scc-state
node server
4.启动代理服务器
1.cd scc-broker
2.SCC_INSTANCE_IP='127.0.0.1' 3.SCC_STATE_SERVER_HOST='127.00.1' 4.SOCKETCLUSTER_SERVER_PORT='8888' node server
注意:2、3、4放在一行执行
SCC_INSTANCE_IP 运行是sockercluster服务的ip(官网的大坑,并没有要求),不然会提示
Server xxxxxx at address undefinedon port 8888 joined the cluster
SCC_STATE_SERVER_HOST 运行scc-state服务的ip
5.启动sockercluster
cd socketcluster
npm install -g socketcluster
socketcluster create myapp
cd myapp
SCC_STATE_SERVER_HOST='127.0.0.1'SOCKETCLUSTER_PORT='8000' node server
SCC_STATE_SERVER_HOST='127.0.0.1'SOCKETCLUSTER_PORT='8001' node server
这是启动两个sockercluster实例 这时的这两个实例会共享通道
6.搭建负载均衡
git clone https://github.com/SocketCluster/loadbalancer
npm install -g loadbalancer
修改config文件
vi config.json
{
"sourcePort": 80,
"balancerCount": 1,
"targetDeactivationDuration": 60000,
"sessionExpiry": 30000,
"downgradeToUser": "someuser",
"stickiness": true,
"targets": [
{
"host": "localhost",
"port": 8000
},
{
"host": "localhost",
"port": 8001
}
]
}
启动loadblancer
loadbalancer start --config my/path/config.json
loadbalancer stop 停止
当然服务在均衡也可以使用第三方的负载均衡软件,nginx,haproxy都可以实现。
此时访问http://localhost:80 就可以实现soketclusterde 负载均衡,共享通道了,如果需要增加机器只需要在新的机器上开启sockercluster个进程,注意开启时要指定scc-state的ip。