分布式部署:服务器分布式部署,扩缩容后路由的计算
This commit is contained in:
@@ -13,3 +13,10 @@
|
|||||||
|
|
||||||
## 运行 web-server
|
## 运行 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`
|
||||||
|
|||||||
+1
-1
@@ -43,7 +43,7 @@ filePath.CONFIG_DIR = '/config';
|
|||||||
const adminfilePath = _pinus.DEFAULT_ADMIN_PATH;
|
const adminfilePath = _pinus.DEFAULT_ADMIN_PATH;
|
||||||
adminfilePath.ADMIN_FILENAME = 'adminUser';
|
adminfilePath.ADMIN_FILENAME = 'adminUser';
|
||||||
adminfilePath.ADMIN_USER = 'config/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
|
* 替换全局Promise
|
||||||
* 自动解析sourcemap
|
* 自动解析sourcemap
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export class GateHandler {
|
|||||||
return resResult(STATUS.CONNECTOR_ERR);
|
return resResult(STATUS.CONNECTOR_ERR);
|
||||||
}
|
}
|
||||||
// select connector
|
// select connector
|
||||||
let res = dispatch(userCode, connectors);
|
let res = dispatch(userCode, connectors, 'connector');
|
||||||
return resResult(STATUS.SUCCESS, { host: res.clientHost, port: res.clientPort });
|
return resResult(STATUS.SUCCESS, { host: res.clientHost, port: res.clientPort });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,6 @@ import { STATUS } from '../../../consts/statusCode';
|
|||||||
import { GMMailModel } from '../../../db/GMMail';
|
import { GMMailModel } from '../../../db/GMMail';
|
||||||
import { setGmMails } from '../../../pubUtils/gmData/gmDataUtil';
|
import { setGmMails } from '../../../pubUtils/gmData/gmDataUtil';
|
||||||
import { getRoleOnlineInfo } from '../../../services/redisService';
|
import { getRoleOnlineInfo } from '../../../services/redisService';
|
||||||
import { dispatch } from '../../../util/dispatcher';
|
|
||||||
import { SendMailFun } from '../../../services/mailService';
|
import { SendMailFun } from '../../../services/mailService';
|
||||||
import { GM_MAIL_TYPE } from '../../../consts';
|
import { GM_MAIL_TYPE } from '../../../consts';
|
||||||
import { RewardInter } from '../../../pubUtils/interface';
|
import { RewardInter } from '../../../pubUtils/interface';
|
||||||
|
|||||||
@@ -453,7 +453,7 @@ export async function guildActivityStart(dicGuildActivity?: DicGuildActivity) {
|
|||||||
// 开始活动
|
// 开始活动
|
||||||
let guildServers = pinus.app.getServersByType('guild');
|
let guildServers = pinus.app.getServersByType('guild');
|
||||||
for (let serverId of servers) {
|
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);
|
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 servers = await getAllServers(); // 玩家serverId列表
|
||||||
let guildServers = pinus.app.getServersByType('guild');
|
let guildServers = pinus.app.getServersByType('guild');
|
||||||
for (let serverId of servers) {
|
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);
|
await pinus.app.rpc.guild.guildActivityRemote.calWoodenHorseAndSend.toServer(sid.id, serverId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,44 @@
|
|||||||
import * as crc from 'crc';
|
import * as crc from 'crc';
|
||||||
import { ServerInfo } from 'pinus';
|
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;
|
let index = Math.abs(crc.crc32(uid)) % connectors.length;
|
||||||
return connectors[index];
|
return connectors[index];
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,7 @@ export function chat(session: Session, msg: any, app: Application, cb: (err: Err
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = dispatch(session.get('roleId'), chatServers);
|
let res = dispatch(session.get('roleId'), chatServers, 'chat');
|
||||||
cb(null, res.id);
|
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);
|
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);
|
cb(null, res.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function activity(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
export function activity(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||||
let activityServers = app.getServersByType('activity');
|
let activityServers = app.getServersByType('activity');
|
||||||
|
|
||||||
if (!activityServers || activityServers.length === 0) {
|
if (!activityServers || activityServers.length === 0) {
|
||||||
cb(new Error('can not find activity servers.'));
|
cb(new Error('can not find activity servers.'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let res = dispatch(session.get('roleId'), activityServers, 'activity');
|
||||||
let res = dispatch(session.get('roleId'), activityServers);
|
|
||||||
cb(null, res.id);
|
cb(null, res.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +111,7 @@ export function gm(session: Session, msg: any, app: Application, cb: (err: Error
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = dispatch(session.get('roleId'), gmServers);
|
let res = dispatch(session.get('roleId'), gmServers, 'gm');
|
||||||
cb(null, res.id);
|
cb(null, res.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,6 +123,6 @@ export function order(session: Session, msg: any, app: Application, cb: (err: Er
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = dispatch(session.get('roleId'), orderServers);
|
let res = dispatch(session.get('roleId'), orderServers, 'order');
|
||||||
cb(null, res.id);
|
cb(null, res.id);
|
||||||
}
|
}
|
||||||
@@ -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',
|
'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',
|
'redis': 'r-8vb130185rp2ir3lqn.redis.zhangbei.rds.aliyuncs.com',
|
||||||
'redispw': 'zyz_monitor_2021'
|
'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'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -16,7 +16,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
'dev': {
|
'dev': {
|
||||||
'id': 'master-server-1',
|
'id': 'master-server-1',
|
||||||
'host': '127.0.0.1',
|
'host': '172.26.145.161',
|
||||||
'port': 3005
|
'port': 3005
|
||||||
},
|
},
|
||||||
'isbn': {
|
'isbn': {
|
||||||
@@ -28,5 +28,10 @@ module.exports = {
|
|||||||
'id': 'master-server-1',
|
'id': 'master-server-1',
|
||||||
'host': '127.0.0.1',
|
'host': '127.0.0.1',
|
||||||
'port': 3005
|
'port': 3005
|
||||||
|
},
|
||||||
|
'distribute': {
|
||||||
|
'id': 'master-server-1',
|
||||||
|
'host': '172.26.145.161',
|
||||||
|
'port': 3005
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -22,5 +22,9 @@ module.exports = {
|
|||||||
'monitor': {
|
'monitor': {
|
||||||
'type': 'monitor',
|
'type': 'monitor',
|
||||||
'name': '监控服'
|
'name': '监控服'
|
||||||
|
},
|
||||||
|
'distribute': {
|
||||||
|
'type': 'distribute',
|
||||||
|
'name': '分布式测试'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -263,5 +263,42 @@ module.exports = {
|
|||||||
'order': [
|
'order': [
|
||||||
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
|
{ '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" },
|
||||||
|
]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ if [ $# != 1 ] ; then
|
|||||||
echo "alpha: 同步代码到 alpha 服务器上,作为测试服供服务开发使用"
|
echo "alpha: 同步代码到 alpha 服务器上,作为测试服供服务开发使用"
|
||||||
echo "dev: 同步代码到 dev 服务器上,作为测试服供服务开发使用"
|
echo "dev: 同步代码到 dev 服务器上,作为测试服供服务开发使用"
|
||||||
echo "monitor: 同步代码到 monitor 服务器上,用做监控和压力测试"
|
echo "monitor: 同步代码到 monitor 服务器上,用做监控和压力测试"
|
||||||
|
echo "distribute: 同步代码到 distribute 服务器上,用做监控和压力测试"
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -18,6 +19,8 @@ elif [ ${1} == 'isbn' ] ; then
|
|||||||
destUrl="root@zyzisbn:/root/zyz/"
|
destUrl="root@zyzisbn:/root/zyz/"
|
||||||
elif [ ${1} == 'monitor' ] ; then
|
elif [ ${1} == 'monitor' ] ; then
|
||||||
destUrl="root@zyzmon:/root/zyz/"
|
destUrl="root@zyzmon:/root/zyz/"
|
||||||
|
elif [ ${1} == 'distribute' ] ; then
|
||||||
|
destUrl="root@zyzdistribute:/root/zyz/"
|
||||||
else
|
else
|
||||||
echo "需要一个参数指明服务器"
|
echo "需要一个参数指明服务器"
|
||||||
exit 1;
|
exit 1;
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
"local": "cross-env EGG_SERVER_ENV=local npm run dev",
|
"local": "cross-env EGG_SERVER_ENV=local npm run dev",
|
||||||
"isbn": "cross-env EGG_SERVER_ENV=isbn npm run dev",
|
"isbn": "cross-env EGG_SERVER_ENV=isbn npm run dev",
|
||||||
"monitor": "cross-env EGG_SERVER_ENV=monitor 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"
|
"lylocal": "cross-env EGG_SERVER_ENV=lylocal npm run dev"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
Reference in New Issue
Block a user