augejs
is a progressive Node.js framework for building applications.
🌟 Star us on GitHub — it helps! 👏
https://github.com/augejs/augejs.github.io
The Nodejs official solution provided by Node.js is Cluster module, and there's an introduction:
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows you to easily create child processes that all share server ports.
-
the process that forks other processes is called Master process, it seems like a contractor that does nothing except forking other processes.
-
other forked processes are called Worker processes, as the name suggests, they are workers that work actually. They accept requests and provide services.
-
usually the number of Worker processes depends on the CPU core number, only in this way can we take full advantage of multi-core resources.
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}
import {
ILogger,
boot,
GetLogger,
Module,
IScanNode,
Cluster,
} from '@augejs/core';
import {
WebServer,
RequestMapping,
} from '@augejs/koa';
@Cluster({
workers: 2
})
@WebServer()
@Module()
class AppModule {
@GetLogger()
logger!:ILogger;
@RequestMapping.Get()
async api() {
this.logger.info('api');
return '----';
}
async onInit(scanNode: IScanNode) {
this.logger.info('app on onInit');
}
async onAppWillReady() {
this.logger.info('app on onAppWillReady');
}
async onAppDidReady() {
this.logger.info('app on onAppDidReady');
}
}
async function main() {
await boot(AppModule);
}
main();