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

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
@@ -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 });
}
}
+32 -33
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);
+2 -2
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);
}
}
+38 -1
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];
}
+6 -8
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);
}