简体   繁体   English

如何在不重新启动服务器的情况下将expressjs静态模板部署到AWS?

[英]How to deploy expressjs static templates to AWS without restarting the server?

I have express.js server server that I deploy to AWS elastic beanstalk. 我有部署到AWS Elastic beantalk的express.js服务器服务器。 I use handlebars engine for templating. 我使用车把引擎进行模板制作。 Templates structure changes more often than server code so I want to have a fast way to deploy them. 模板的结构更改比服务器代码更频繁,因此我希望有一种快速的方法来部署它们。 I have experimented with hosting templates on separate S3 static bucket and then loading them from nodejs code. 我已经尝试过在单独的S3静态存储桶上托管模板,然后从nodejs代码加载它们。 This way I can deploy changes to templates really fast by using S3 static deployment and not eb deploy that restarts all server nodes. 这样,我可以使用S3静态部署真正快速地将更改部署到模板,而不是通过eb deploy重新启动所有服务器节点。

Here is my node.js server code for template loading: 这是我用于模板加载的node.js服务器代码:

const s3 = new aws.S3();
s3.getObject({Bucket: 'bucketname.com', Key: 'views/' + path}, (err, data) => {
  const template = handlebars.compile(data.Body.toString('utf-8'));
  resolve(template);
});

This solved template deployment speed problem, but I'm not sure if it does not introduce additional performance hit. 这解决了模板部署速度的问题,但是我不确定它是否不会带来额外的性能损失。 I thought since S3 and elastic beanstalk are both amazon services the impact should be minimal, but website benchmarks make me think otherwise. 我以为,由于S3和Elastic beantalk都是亚马逊服务,因此影响应该很小,但是网站基准使我不这么认为。

Maybe there is a better way to solve the problem of templates deployment speed? 也许有更好的方法来解决模板部署速度的问题?

Regarding the benchmarking numbers, I believe that's because your code will try to fetch the view template from s3 each time it needs to render the view. 关于基准测试编号,我认为这是因为您的代码每次需要渲染视图时,都会尝试从s3获取视图模板。 These network calls will lead to overhead. 这些网络呼叫将导致开销。

I once faced a similar challenge in one of my previous projects. 在我以前的一个项目中,我曾经面临过类似的挑战。 However, in our case, the size and number of these frequently changing templates were within the acceptable range and it was possible to have them in-memory. 但是,在我们的情况下,这些频繁更改的模板的大小和数量都在可接受的范围内,并且可以将它们存储在内存中。

We ended up in creating a different GIT repo for storing these static templates. 我们最终创建了一个不同的GIT存储库来存储这些静态模板。 Our express server would load these templates and cache them in-memory when it is started. express服务器将在启动时加载这些模板并将其缓存在内存中。

We configured a webhook to ping our express server every time the templates changed (new commit in the git repo). 每次模板更改(git repo中的新提交)时,我们都配置了一个Webhook来ping express服务器。 The server then invalidates its cache and loads new templates from the same repo. 然后,服务器使其缓存无效,并从同一存储库加载新模板。 That way we didn't need to restart the server for every change in the static template. 这样,我们无需为静态模板中的每个更改重新启动服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM