- 快速入门
- 指南
- 参考手册
- 高级主题
- 资源
- Express 词汇表
- Express 社区
- 模板引擎
- Express 中间件
- Express body-parser middleware
- Express compression middleware
- Express connect-rid middleware
- Express cookie-parser middleware
- Express cookie-session middleware
- Express cors middleware
- Express csurf middleware
- Express errorhandler middleware
- Express method-override middleware
- Express morgan middleware
- Express multer middleware
- Express response-time middleware
- Express serve-favicon middleware
- Express serve-index middleware
- Express serve-static middleware
- Express session middleware
- Express timeout middleware
- Express vhost middleware
- Express 实用模板
- 推荐框架
- 推荐书籍和博客
健康检查和优雅地关闭
Graceful shutdown
When you deploy a new version of your application, you must replace the previous version. The process manager you’re using will first send a SIGTERM signal to the application to notify it that it will be killed. Once the application gets this signal, it will stop accepting new requests, finish all the ongoing requests, and clean up the resources it used, including database connections and file locks.
Health checks
A load balancer uses health checks to determine if an application instance is healthy and can accept requests. For example, Kubernetes has two health checks:
liveness
, that determines when to restart a container.readiness
, that determines when a container is ready to start accepting traffic. When a pod is not ready, it is removed from the service load balancers.
Third-party solutions
Warning: This information refers to third-party sites, products, or modules that are not maintained by the Expressjs team. Listing here does not constitute an endorsement or recommendation from the Expressjs project team.
Terminus
Terminus is an open-source project that adds health checks and graceful shutdown to your application to eliminate the need to write boilerplate code. You just provide the cleanup logic for graceful shutdowns and the health check logic for health checks, and terminus handles the rest.
Install terminus as follows:
npm i @godaddy/terminus --save
Here’s a basic template that illustrates using terminus. For more information, see https://github.com/godaddy/terminus.
const http = require('http')
const express = require('express')
const { createTerminus } = require('@godaddy/terminus')
const app = express()
app.get('/', (req, res) => {
res.send('ok')
})
const server = http.createServer(app)
function onSignal () {
console.log('server is starting cleanup')
// start cleanup of resource, like databases or file descriptors
}
async function onHealthCheck () {
// checks if the system is healthy, like the db connection is live
// resolves, if health, rejects if not
}
createTerminus(server, {
signal: 'SIGINT',
healthChecks: { '/healthcheck': onHealthCheck },
onSignal
})
server.listen(3000)
Lightship
Lightship is an open-source project that adds health, readiness and liveness checks to your application. Lightship is a standalone HTTP-service that runs as a separate HTTP service; this allows having health-readiness-liveness HTTP endpoints without exposing them on the public interface.
Install Lightship as follows:
npm install lightship
Basic template that illustrates using Lightship:
const http = require('http')
const express = require('express')
const {
createLightship
} = require('lightship')
// Lightship will start a HTTP service on port 9000.
const lightship = createLightship()
const app = express()
app.get('/', (req, res) => {
res.send('ok')
})
app.listen(3000, () => {
lightship.signalReady()
})
// You can signal that the service is not ready using `lightship.signalNotReady()`.
Lightship documentation provides examples of the corresponding Kubernetes configuration and a complete example of integration with Express.js.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论