Skip to content

Commit

Permalink
网关服务器从redis中取其它服务器列表
Browse files Browse the repository at this point in the history
  • Loading branch information
shihuaping committed Aug 15, 2017
1 parent 99dded2 commit 3f5f1da
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions center_svr/center-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const packet = require('../protocol/packet');
// type
// ip
// port
// activeTime
// ext
// }
function registerServer(serverInfo) {
Expand Down Expand Up @@ -46,6 +47,8 @@ function process(socket, data) {
switch (subCmd) {
case cmdDefine.SUB_CENTER_UPDATE:
let serverInfo = jObj.body;
serverInfo.ip = socket.remoteAddress;
serverInfo.activeTime = Math.floor(new Date().getTime()/1000);
registerServer(serverInfo);
break;
case cmdDefine.SUB_CENTER_GET:
Expand Down
2 changes: 2 additions & 0 deletions gate_svr/gate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const wsServer = require('./ws-server');
const sysConfig = require('./config/sys-config.json');
const logger = require('./logger');
const registerCenter = require('./register-center');
const sysCache = require('./sys-cache');

logger.debug(sysConfig);

sysCache.readCached();
setInterval(registerCenter.registerSelf, 5*1000);

wsServer.startServer(sysConfig);
12 changes: 9 additions & 3 deletions gate_svr/redis-oper.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@

const sysConfig = require('./config/sys-config.json');
const redis = require('redis');
//const redisPool = require('redis-pool-connection');
const redisKey = require('../lib/rds-key');

var host = sysConfig.redisHost;
var port = sysConfig.redisPort;
var password = sysConfig.redisPassword;

function readCachedServers() {
function readCachedServers(cb) {
var rds = null;
if (!!password && password.length > 0) {
rds = redis.createClient({host:host,port:port,password:password});
} else {
rds = redis.createClient({host:host,port:port});
}

conn.hget
for (let v of redisKey.SERVER_TYPE_LIST) {
let key = redisKey.KEY_SERVER_TYPE + v;
rds.get(key, function (error,reply) {
cb (v, reply);
});
}
}

exports.readCachedServers = readCachedServers;
1 change: 1 addition & 0 deletions gate_svr/register-center.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function registerSelf() {
let serverInfo = {};
serverInfo.type = constDefine.SERVER_TYPE_GATE;
serverInfo.port = sysConfig.svrPort;
serverInfo.name = "gate_svr";

jObj.body = serverInfo;

Expand Down
18 changes: 16 additions & 2 deletions gate_svr/sys-cache.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@

const redisOper = require('./redis-oper');

var cached = {}
var cached = {};

function saveServerInfo(type,serverList) {
let jArray = JSON.parse(serverList);
cached[type] = jArray;
}

// read servers info from redis
function readCached() {

redisOper.readCachedServers(saveServerInfo);
}

function getServerList(type) {

return cached[type];
}

setInterval(readCached, 60*1000);

setInterval(readCached, 60*1000);
exports.getServerList = getServerList;
exports.readCached = readCached;
3 changes: 2 additions & 1 deletion login_svr/register-center.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ function registerSelf() {
let jObj = packet.getPacket(cmdDefine.CENTER, cmdDefine.SUB_CENTER_UPDATE);

let serverInfo = {};
serverInfo.type = constDefine.SERVER_TYPE_GATE;
serverInfo.type = constDefine.SERVER_TYPE_LOGIN;
serverInfo.port = sysConfig.svrPort;
serverInfo.name = "login_svr";

jObj.body = serverInfo;

Expand Down

0 comments on commit 3f5f1da

Please sign in to comment.