集群环境的启动代码

This commit is contained in:
Xu Chang 2023-12-13 11:47:19 +08:00
parent 3fe8ed6c42
commit 348e41c64a
2 changed files with 15 additions and 7 deletions

View File

@ -29,13 +29,17 @@ async function startup(path, contextBuilder, connector, omitWatchers, omitTimers
const io = new socket_io_1.Server(httpServer, socketOption); const io = new socket_io_1.Server(httpServer, socketOption);
const clusterInfo = (0, oak_backend_base_1.getClusterInfo)(); const clusterInfo = (0, oak_backend_base_1.getClusterInfo)();
if (clusterInfo.usingCluster) { if (clusterInfo.usingCluster) {
// 目前只pm2模式 // 目前只支持单物理结点的pm2模式
// pm2环境下要接入clusterAdapter // pm2环境下要接入clusterAdapter
// https://socket.io/zh-CN/docs/v4/pm2/ // https://socket.io/zh-CN/docs/v4/pm2/
io.adapter((0, cluster_adapter_1.createAdapter)()); io.adapter((0, cluster_adapter_1.createAdapter)());
(0, sticky_1.setupWorker)(io); (0, sticky_1.setupWorker)(io);
console.log(`以集群模式启动,实例总数『${clusterInfo.instanceCount}』,当前实例号『${clusterInfo.instanceId}`);
} }
const appLoader = new oak_backend_base_1.AppLoader(path, contextBuilder, io.of(DATA_SUBSCRIBER_NAMESPACE)); else {
console.log('以单实例模式启动');
}
const appLoader = clusterInfo.usingCluster ? new oak_backend_base_1.ClusterAppLoader(path, contextBuilder, io.of(DATA_SUBSCRIBER_NAMESPACE)) : new oak_backend_base_1.AppLoader(path, contextBuilder, io.of(DATA_SUBSCRIBER_NAMESPACE));
await appLoader.mount(); await appLoader.mount();
await appLoader.execStartRoutines(); await appLoader.execStartRoutines();
if (routine) { if (routine) {

View File

@ -5,7 +5,7 @@ import PathLib from 'path';
import Koa from 'koa'; import Koa from 'koa';
import KoaRouter from 'koa-router'; import KoaRouter from 'koa-router';
import KoaBody from 'koa-body'; import KoaBody from 'koa-body';
import { AppLoader, getClusterInfo } from 'oak-backend-base'; import { AppLoader, getClusterInfo, ClusterAppLoader } from 'oak-backend-base';
import { OakException, Connector, EntityDict, ClusterInfo } from 'oak-domain/lib/types'; import { OakException, Connector, EntityDict, ClusterInfo } from 'oak-domain/lib/types';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain'; import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore'; import { AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
@ -37,14 +37,18 @@ export async function startup<ED extends EntityDict & BaseEntityDict, Cxt extend
const io = new Server(httpServer, socketOption); const io = new Server(httpServer, socketOption);
const clusterInfo = getClusterInfo(); const clusterInfo = getClusterInfo();
if (clusterInfo.usingCluster) { if (clusterInfo.usingCluster) {
// 目前只pm2模式 // 目前只支持单物理结点的pm2模式
// pm2环境下要接入clusterAdapter // pm2环境下要接入clusterAdapter
// https://socket.io/zh-CN/docs/v4/pm2/ // https://socket.io/zh-CN/docs/v4/pm2/
io.adapter(createAdapter()); io.adapter(createAdapter());
setupWorker(io); setupWorker(io);
console.log(`以集群模式启动,实例总数『${clusterInfo.instanceCount}』,当前实例号『${clusterInfo.instanceId}`);
} }
else {
const appLoader = new AppLoader(path, contextBuilder, io.of(DATA_SUBSCRIBER_NAMESPACE)); console.log('以单实例模式启动');
}
const appLoader = clusterInfo.usingCluster ? new ClusterAppLoader(path, contextBuilder, io.of(DATA_SUBSCRIBER_NAMESPACE)) : new AppLoader(path, contextBuilder, io.of(DATA_SUBSCRIBER_NAMESPACE));
await appLoader.mount(); await appLoader.mount();
await appLoader.execStartRoutines(); await appLoader.execStartRoutines();
if (routine) { if (routine) {