分布式部署:服务器分布式部署,扩缩容后路由的计算

This commit is contained in:
qiaoxin
2021-07-28 21:13:51 +08:00
parent acdaa9f9ac
commit dc832da60a
13 changed files with 150 additions and 54 deletions

View File

@@ -1,7 +1,44 @@
import * as crc from 'crc';
import { ServerInfo } from 'pinus';
export function dispatch(uid: string , connectors: ServerInfo[]) {
let roleRoute = new Map<string, Map<string, string>>()
export function dispatch(uid: string, connectors: ServerInfo[], serverType: string = '') {
//测试代码
// if (serverType == 'activity') {
// if (uid == 'SPDtGqx9Gw') {
// if (connectors.length > 1) {
// connectors = connectors.filter(obj => { return obj.id != 'activity-server-1' })
// }
// } else {
// connectors = connectors.filter(obj => { return obj.id == 'activity-server-1' })
// }
// }
if (serverType) {
let roleRouteData = roleRoute.get(uid);
let serverId = '';
if (roleRouteData) {
serverId = roleRouteData.get(serverType);
if (serverId) {
let index = connectors.findIndex(obj => { return obj.id === serverId });
if (index != -1) {
// console.log('aaaaaaaaa 1', roleRouteData)
return connectors[index];
}
}
}
let index = Math.abs(crc.crc32(uid)) % connectors.length;
if (!roleRouteData) {
roleRouteData = new Map<string, string>()
}
roleRouteData.set(serverType, connectors[index].id);
roleRoute.set(uid, roleRouteData)
// console.log('aaaaaaaaa 2', roleRouteData)
return connectors[index];
}
let index = Math.abs(crc.crc32(uid)) % connectors.length;
return connectors[index];
}

View File

@@ -10,7 +10,7 @@ export function chat(session: Session, msg: any, app: Application, cb: (err: Err
return;
}
let res = dispatch(session.get('roleId'), chatServers);
let res = dispatch(session.get('roleId'), chatServers, 'chat');
cb(null, res.id);
}
@@ -38,7 +38,7 @@ export function battle(session: Session, msg: any, app: Application, cb: (err: E
}
}
let res = dispatch(rid, battleServers);
let res = dispatch(rid, battleServers, 'battle');
cb(null, res.id);
}
@@ -89,19 +89,17 @@ export function guild(session: Session, msg: any, app: Application, cb: (err: Er
}
}
let res = dispatch(rid, guildServers);
let res = dispatch(rid, guildServers, 'guild');
cb(null, res.id);
}
export function activity(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
let activityServers = app.getServersByType('activity');
if (!activityServers || activityServers.length === 0) {
cb(new Error('can not find activity servers.'));
return;
}
let res = dispatch(session.get('roleId'), activityServers);
let res = dispatch(session.get('roleId'), activityServers, 'activity');
cb(null, res.id);
}
@@ -113,7 +111,7 @@ export function gm(session: Session, msg: any, app: Application, cb: (err: Error
return;
}
let res = dispatch(session.get('roleId'), gmServers);
let res = dispatch(session.get('roleId'), gmServers, 'gm');
cb(null, res.id);
}
@@ -125,6 +123,6 @@ export function order(session: Session, msg: any, app: Application, cb: (err: Er
return;
}
let res = dispatch(session.get('roleId'), orderServers);
let res = dispatch(session.get('roleId'), orderServers, 'order');
cb(null, res.id);
}