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

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

@@ -12,4 +12,11 @@
`cd game-server && node tsrun.js`
## 运行 web-server
`cd web-server && node app`
`cd web-server && node app`
## 扩容事项
1.master.ts对应环境的主机host填写内网ip地址新服务器host填写主机host地址
2.config/database.ts填写对应环境下的配置
3.新服务器执行命令(host为主机内网ip)
`npm run build`
`pm2 start ./dist/app.js --name="activity-server-2" -x -- env=dev id=activity-server-2 host=172.26.145.171 port=9061 serverType=activity`

View File

@@ -43,7 +43,7 @@ filePath.CONFIG_DIR = '/config';
const adminfilePath = _pinus.DEFAULT_ADMIN_PATH;
adminfilePath.ADMIN_FILENAME = 'adminUser';
adminfilePath.ADMIN_USER = 'config/adminUser';
const ALL_ENVS = 'production|development|alpha|dev|isbn|monitor';
const ALL_ENVS = 'production|development|alpha|dev|isbn|monitor|distribute';
/**
* 替换全局Promise
* 自动解析sourcemap
@@ -151,8 +151,8 @@ app.configure(ALL_ENVS, 'systimer', function () {
function errorHandler(err: Error, msg: any, resp: any,
session: FrontendOrBackendSession, cb: HandlerCallback) {
const errCode = genCode(10);
errlogger.error(`${pinus.app.serverId} error handler \n msg[${JSON.stringify(msg)}] \n resp[${JSON.stringify(resp)}] \n sessionId:${JSON.stringify(session.export())} \n error 【${errCode}】 stack: ${err.stack}`);
const errCode = genCode(10);
errlogger.error(`${pinus.app.serverId} error handler \n msg[${JSON.stringify(msg)}] \n resp[${JSON.stringify(resp)}] \n sessionId:${JSON.stringify(session.export())} \n error 【${errCode}】 stack: ${err.stack}`);
if (!resp) {
resp = resResult(STATUS.GLOBAL_ERR, { errCode });
@@ -162,8 +162,8 @@ function errorHandler(err: Error, msg: any, resp: any,
export function globalErrorHandler(err: Error, msg: any, resp: any,
session: FrontendOrBackendSession, cb: HandlerCallback) {
const errCode = genCode(10);
errlogger.error(`${pinus.app.serverId} globalErrorHandler handler \n msg[${JSON.stringify(msg)}] \n resp[${JSON.stringify(resp)}] \n sessionId:${JSON.stringify(session.export())} \n error 【${errCode}】 stack: ${err.stack}`);
const errCode = genCode(10);
errlogger.error(`${pinus.app.serverId} globalErrorHandler handler \n msg[${JSON.stringify(msg)}] \n resp[${JSON.stringify(resp)}] \n sessionId:${JSON.stringify(session.export())} \n error 【${errCode}】 stack: ${err.stack}`);
if (cb) {
cb(err, resp ? resp : { code: 503 });
@@ -176,7 +176,7 @@ app.configure(ALL_ENVS, function () {
app.set(RESERVED.GLOBAL_ERROR_HANDLER, globalErrorHandler);
app.globalAfter((err: Error, routeRecord: RouteRecord, msg: any, session: FrontendOrBackendSession, resp: any, cb: HandlerCallback) => {
infologger.debug('global after ', msg && msg.route, msg && JSON.stringify(msg.body), session && session.get('roleId'));
if(err) infologger.debug(err);
if (err) infologger.debug(err);
})
app.globalBefore((routeRecord: RouteRecord, msg: any, session: FrontendOrBackendSession, cb: HandlerCallback) => {

View File

@@ -31,7 +31,7 @@ export class GateHandler {
return resResult(STATUS.CONNECTOR_ERR);
}
// select connector
let res = dispatch(userCode, connectors);
let res = dispatch(userCode, connectors, 'connector');
return resResult(STATUS.SUCCESS, { host: res.clientHost, port: res.clientPort });
}
}

View File

@@ -1,4 +1,4 @@
import {Application, BackendSession, pinus} from 'pinus';
import { Application, BackendSession, pinus } from 'pinus';
import { RoleModel } from '../../../db/Role';
import { EventRecordModel } from '../../../db/EventRecord';
import { getEvent } from '../../../services/eventSercive';
@@ -7,7 +7,6 @@ import { STATUS } from '../../../consts/statusCode';
import { GMMailModel } from '../../../db/GMMail';
import { setGmMails } from '../../../pubUtils/gmData/gmDataUtil';
import { getRoleOnlineInfo } from '../../../services/redisService';
import { dispatch } from '../../../util/dispatcher';
import { SendMailFun } from '../../../services/mailService';
import { GM_MAIL_TYPE } from '../../../consts';
import { RewardInter } from '../../../pubUtils/interface';
@@ -16,7 +15,7 @@ import { ServerlistModel } from '../../../db/Serverlist';
import { getWorldChannelSid } from '../../../services/chatService';
import { MaintenanceModel } from '../../../db/Maintenance';
import { initMaintenance, stopMaintenance } from '../../../services/gmService';
export default function(app: Application) {
export default function (app: Application) {
return new GmHandler(app);
}
@@ -29,8 +28,8 @@ export class GmHandler {
let { uid, serverId } = msg;
let role = await RoleModel.findByUid(uid, serverId);
if(!role) return resResult(STATUS.GM_ROLE_NOT_FOUND);
let {roleId, roleName, eventStatus } = role;
if (!role) return resResult(STATUS.GM_ROLE_NOT_FOUND);
let { roleId, roleName, eventStatus } = role;
let channelService = this.app.get('channelService');
let channel = channelService.getChannel(roleId, true);
@@ -49,9 +48,9 @@ export class GmHandler {
return resResult(STATUS.SUCCESS, { list: event });
}
async clearEvent(msg: { }, session: BackendSession) {
async clearEvent(msg: {}, session: BackendSession) {
let roleId = session.get('roleId');
await EventRecordModel.deleteAccount(roleId);
return resResult(STATUS.SUCCESS);
}
@@ -64,19 +63,19 @@ export class GmHandler {
let content = {};
try {
content = JSON.parse(message);
} catch(e) {
} catch (e) {
return resResult(STATUS.GM_JSON_FORMAT_ERR);
}
console.log(uid, serverId)
let role = await RoleModel.findByUid(uid, serverId);
if(!role) return resResult(STATUS.GM_ROLE_NOT_FOUND);
if (!role) return resResult(STATUS.GM_ROLE_NOT_FOUND);
let { roleId } = role;
let hisOnlineInfo = await getRoleOnlineInfo(role.roleId);
if(hisOnlineInfo.isOnline) {
if (hisOnlineInfo.isOnline) {
let sid = hisOnlineInfo.sid;
if (!!sid) {//下发邮件,对应前端红点提示
pinus.app.channelService.pushMessageByUids(eventName, resResult(STATUS.SUCCESS, content), [{uid: roleId, sid}]);
pinus.app.channelService.pushMessageByUids(eventName, resResult(STATUS.SUCCESS, content), [{ uid: roleId, sid }]);
}
}
return resResult(STATUS.SUCCESS, { msg: content });
@@ -84,8 +83,8 @@ export class GmHandler {
//对接gm后台下发邮件
async addMail(msg: { endTime: number, sendName: string, content: string, goods: RewardInter[], sendTime: number }) {
let {endTime, content, sendName, sendTime, goods } = msg;
let mail = await GMMailModel.addMail({endTime, content, sendName, sendTime, goods});
let { endTime, content, sendName, sendTime, goods } = msg;
let mail = await GMMailModel.addMail({ endTime, content, sendName, sendTime, goods });
return resResult(STATUS.SUCCESS, { mail });
}
@@ -94,7 +93,7 @@ export class GmHandler {
let { id, mailType, roleIds, serverIds } = msg;
let f = new SendMailFun();
await f.setWithGmMail(id);
if(mailType == GM_MAIL_TYPE.SINGLE || mailType == GM_MAIL_TYPE.GROUP) {
if (mailType == GM_MAIL_TYPE.SINGLE || mailType == GM_MAIL_TYPE.GROUP) {
await f.sendToUsers(mailType, roleIds);
} else {
await f.sendToServer(serverIds);
@@ -103,11 +102,11 @@ export class GmHandler {
return resResult(STATUS.SUCCESS);
}
async sendWithContent(msg: { contentId: number, mailType: number, params: { params?: string[], sendName?: string, goods?: RewardInter[], endTime?: number}, roleIds?: string[], serverIds?: number[] }, session: BackendSession) {
async sendWithContent(msg: { contentId: number, mailType: number, params: { params?: string[], sendName?: string, goods?: RewardInter[], endTime?: number }, roleIds?: string[], serverIds?: number[] }, session: BackendSession) {
let { contentId, mailType, roleIds, serverIds, params } = msg;
let f = new SendMailFun();
f.setWithContentId(contentId, params);
if(mailType == GM_MAIL_TYPE.SINGLE || mailType == GM_MAIL_TYPE.GROUP) {
if (mailType == GM_MAIL_TYPE.SINGLE || mailType == GM_MAIL_TYPE.GROUP) {
await f.sendToUsers(mailType, roleIds);
} else {
await f.sendToServer(serverIds);
@@ -118,41 +117,41 @@ export class GmHandler {
async reloadResource(msg: {}, session: BackendSession) {
try {
let activityServers = this.app.getServersByType('activity');
for(let { id } of activityServers) {
for (let { id } of activityServers) {
await this.app.rpc.activity.activityRemote.reloadResources.toServer(id);
}
let battleServers = this.app.getServersByType('battle');
for(let { id } of battleServers) {
for (let { id } of battleServers) {
await this.app.rpc.battle.battleRemote.reloadResources.toServer(id);
}
let chatServers = this.app.getServersByType('chat');
for(let { id } of chatServers) {
for (let { id } of chatServers) {
await this.app.rpc.chat.chatRemote.reloadResources.toServer(id);
}
let connectServers = this.app.getServersByType('connector');
for(let { id } of connectServers) {
for (let { id } of connectServers) {
await this.app.rpc.connector.connectorRemote.reloadResources.toServer(id);
}
let guildServers = this.app.getServersByType('guild');
for(let { id } of guildServers) {
for (let { id } of guildServers) {
await this.app.rpc.guild.guildRemote.reloadResources.toServer(id);
}
let roleServers = this.app.getServersByType('role');
for(let { id } of roleServers) {
for (let { id } of roleServers) {
await this.app.rpc.role.roleRemote.reloadResources.toServer(id);
}
let systimerServers = this.app.getServersByType('systimer');
for(let { id } of systimerServers) {
for (let { id } of systimerServers) {
await this.app.rpc.systimer.systimerRemote.reloadResources.toServer(id);
}
let orderServers = this.app.getServersByType('order');
for(let { id } of orderServers) {
for (let { id } of orderServers) {
await this.app.rpc.order.orderRemote.reloadResources.toServer(id);
}
return resResult(STATUS.SUCCESS, {
isOK: true
});
} catch(e) {
} catch (e) {
return resResult(STATUS.SUCCESS, {
isOK: false,
err: e.stack
@@ -171,9 +170,9 @@ export class GmHandler {
async sendMarquee(msg: { code: string }, session: BackendSession) {
const { code } = msg;
let systimerServers = this.app.getServersByType('systimer');
for(let { id } of systimerServers) {
for (let { id } of systimerServers) {
let result = await this.app.rpc.systimer.systimerRemote.setMarquee.toServer(id, code);
if(!result) return resResult(STATUS.GM_MARQUEE_ERR);
if (!result) return resResult(STATUS.GM_MARQUEE_ERR);
}
return resResult(STATUS.SUCCESS);
}
@@ -181,9 +180,9 @@ export class GmHandler {
async cancelMarquee(msg: { code: string }, session: BackendSession) {
const { code } = msg;
let systimerServers = this.app.getServersByType('systimer');
for(let { id } of systimerServers) {
for (let { id } of systimerServers) {
let result = await this.app.rpc.systimer.systimerRemote.cancelMarquee.toServer(id, code);
if(!result) return resResult(STATUS.GM_MARQUEE_CANCEL_ERR);
if (!result) return resResult(STATUS.GM_MARQUEE_CANCEL_ERR);
}
return resResult(STATUS.SUCCESS);
}
@@ -192,7 +191,7 @@ export class GmHandler {
const { code } = msg;
const uid = session.get('uid');
const maintenance = await MaintenanceModel.updateStatusByCode(code, true, uid);
if(!maintenance) return resResult(STATUS.WRONG_PARMS);
if (!maintenance) return resResult(STATUS.WRONG_PARMS);
await initMaintenance(maintenance);
return resResult(STATUS.SUCCESS,);
@@ -202,15 +201,15 @@ export class GmHandler {
const { code } = msg;
const uid = session.get('uid');
const maintenance = await MaintenanceModel.updateStatusByCode(code, false, uid);
if(!maintenance) return resResult(STATUS.WRONG_PARMS);
if (!maintenance) return resResult(STATUS.WRONG_PARMS);
await stopMaintenance(maintenance, uid);
return resResult(STATUS.SUCCESS,);
}
async setMaintenance() {
let connectorServers = pinus.app.getServersByType('connector');
for(let { id } of connectorServers) {
for (let { id } of connectorServers) {
pinus.app.rpc.connector.connectorRemote.setServerMainten.toServer(id, [1]);
}
return resResult(STATUS.SUCCESS);

View File

@@ -453,7 +453,7 @@ export async function guildActivityStart(dicGuildActivity?: DicGuildActivity) {
// 开始活动
let guildServers = pinus.app.getServersByType('guild');
for (let serverId of servers) {
let sid = dispatch(serverId.toString(), guildServers);
let sid = dispatch(serverId.toString(), guildServers, 'guild');
await pinus.app.rpc.guild.guildActivityRemote.raceActivityStart.toServer(sid.id, serverId);
}
@@ -560,7 +560,7 @@ export async function raceActivitySeconds() {
let servers = await getAllServers(); // 玩家serverId列表
let guildServers = pinus.app.getServersByType('guild');
for (let serverId of servers) {
let sid = dispatch(serverId.toString(), guildServers);
let sid = dispatch(serverId.toString(), guildServers, 'guild');
await pinus.app.rpc.guild.guildActivityRemote.calWoodenHorseAndSend.toServer(sid.id, serverId);
}
}

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);
}

View File

@@ -29,5 +29,10 @@ module.exports = {
'mongo': 'mongodb://dbop:zyzMon2021@dds-8vb7474e31ba7ed41.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vb7474e31ba7ed42.mongodb.zhangbei.rds.aliyuncs.com:3717/zyz?replicaSet=mgset-505529944',
'redis': 'r-8vb130185rp2ir3lqn.redis.zhangbei.rds.aliyuncs.com',
'redispw': 'zyz_monitor_2021'
},
'distribute': {
'mongo': 'mongodb://dbop:zyzDev2021@dds-8vb5c74ba4263da41.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vb5c74ba4263da42.mongodb.zhangbei.rds.aliyuncs.com:3717/zyz?replicaSet=mgset-506991391',
'redis': 'r-8vb130185rp2ir3lqn.redis.zhangbei.rds.aliyuncs.com',
'redispw': 'zyz_monitor_2021'
}
};

View File

@@ -16,7 +16,7 @@ module.exports = {
},
'dev': {
'id': 'master-server-1',
'host': '127.0.0.1',
'host': '172.26.145.161',
'port': 3005
},
'isbn': {
@@ -28,5 +28,10 @@ module.exports = {
'id': 'master-server-1',
'host': '127.0.0.1',
'port': 3005
},
'distribute': {
'id': 'master-server-1',
'host': '172.26.145.161',
'port': 3005
}
};

View File

@@ -22,5 +22,9 @@ module.exports = {
'monitor': {
'type': 'monitor',
'name': '监控服'
},
'distribute': {
'type': 'distribute',
'name': '分布式测试'
}
};

View File

@@ -263,5 +263,42 @@ module.exports = {
'order': [
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
]
},
'distribute': {
'connector': [
{ 'id': 'connector-server-1', 'port': 4050, 'clientHost': '121.89.211.172', 'host': '127.0.0.1', 'clientPort': 3050, 'frontend': true, "args": " --inspect=9237" },
{ 'id': 'connector-server-2', 'port': 4051, 'clientHost': '121.89.211.172', 'host': '127.0.0.1', 'clientPort': 3051, 'frontend': true, "args": " --inspect=9229" },
{ 'id': 'connector-server-3', 'port': 4052, 'clientHost': '121.89.211.172', 'host': '127.0.0.1', 'clientPort': 3052, 'frontend': true, "args": " --inspect=9238" }
],
'chat': [
{ 'id': 'chat-server-1', 'host': '127.0.0.1', 'port': 6050, "args": " --inspect=9234" },
{ 'id': 'chat-server-2', 'host': '127.0.0.1', 'port': 6051, "args": " --inspect=9235" },
{ 'id': 'chat-server-3', 'host': '127.0.0.1', 'port': 6052, "args": " --inspect=9236" }
],
'role': [
{ 'id': 'role-server-1', 'host': '127.0.0.1', 'port': 6053, "args": " --inspect=9231" }
],
'battle': [
{ 'id': 'battle-server-1', 'host': '127.0.0.1', 'port': 6054, "args": " --inspect=9230" }
],
'gate': [
{ 'id': 'gate-server-1', 'host': '127.0.0.1', 'clientHost': '121.89.211.172', 'clientPort': 3014, 'frontend': true, "args": " --inspect=9227" },
],
'gm': [
{ 'id': 'gm-server-1', 'host': '127.0.0.1', 'port': 6055, "args": " --inspect=9232" }
],
'systimer': [
{ 'id': 'systimer-server-1', 'host': '127.0.0.1', 'port': 6056, "args": " --inspect=9233" }
],
'guild': [
{ 'id': 'guild-server-1', 'host': '127.0.0.1', 'port': 6057, "args": " --inspect=9239" },
{ 'id': 'guild-server-2', 'host': '127.0.0.1', 'port': 6058, "args": " --inspect=9240" }
],
'activity': [
{ 'id': 'activity-server-2', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" },
],
'order': [
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
]
}
};

View File

@@ -5,6 +5,7 @@ if [ $# != 1 ] ; then
echo "alpha: 同步代码到 alpha 服务器上,作为测试服供服务开发使用"
echo "dev: 同步代码到 dev 服务器上,作为测试服供服务开发使用"
echo "monitor: 同步代码到 monitor 服务器上,用做监控和压力测试"
echo "distribute: 同步代码到 distribute 服务器上,用做监控和压力测试"
exit 1;
fi
@@ -18,6 +19,8 @@ elif [ ${1} == 'isbn' ] ; then
destUrl="root@zyzisbn:/root/zyz/"
elif [ ${1} == 'monitor' ] ; then
destUrl="root@zyzmon:/root/zyz/"
elif [ ${1} == 'distribute' ] ; then
destUrl="root@zyzdistribute:/root/zyz/"
else
echo "需要一个参数指明服务器"
exit 1;

View File

@@ -23,6 +23,7 @@
"local": "cross-env EGG_SERVER_ENV=local npm run dev",
"isbn": "cross-env EGG_SERVER_ENV=isbn npm run dev",
"monitor": "cross-env EGG_SERVER_ENV=monitor npm run dev",
"distribute": "cross-env EGG_SERVER_ENV=distribute npm run dev",
"lylocal": "cross-env EGG_SERVER_ENV=lylocal npm run dev"
},
"dependencies": {
@@ -71,4 +72,4 @@
],
"author": "liangtongchuan",
"license": "MIT"
}
}