Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

egg如何配置多个server实例 #5367

Open
laoboxie opened this issue Nov 19, 2024 · 0 comments
Open

egg如何配置多个server实例 #5367

laoboxie opened this issue Nov 19, 2024 · 0 comments

Comments

@laoboxie
Copy link

laoboxie commented Nov 19, 2024

egg如何配置多个server实例

  1. 公司内部需要同时起两个server,一个http server,一个https server
  2. 请问egg配置多个server的最佳实践是什么?
  3. 目前有两种思路:
  • 3.1 使用egg.startCluster执行两次,监听不同的端口
// server.js
egg.startCluster({
    baseDir: __dirname,
    workers: 1,
    port: 3000,
})
egg.startCluster({
    baseDir: __dirname,
    workers: 1,
    port: 4000,
})
  • 3.2 在app.js中通过createServer创建新的server
    担心这种方式可能有问题,其他地方在对server进行操作时(如优雅退出等)得关心和操作多个server。
// app.js
module.exports = app => {
  app.beforeStart(() => {
    // 配置 HTTPS 服务器
    const httpsOptions = {
      key: fs.readFileSync(path.join(app.baseDir, 'app/config/ssl/key.pem')),
      cert: fs.readFileSync(path.join(app.baseDir, 'app/config/ssl/cert.pem')),
    };
    const httpsServer = https.createServer(httpsOptions, app.callback());
    httpsServer.listen(4000, () => {

    });
  });
};
@laoboxie laoboxie reopened this Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant