当您部署新版本的应用程序时,您必须替换以前的版本。 您正在使用的 process manager 将首先向应用程序发送一个 SIGTERM 信号,通知它它将被杀死。 一旦应用程序得到这个信号,它就应该停止接受新的请求,完成所有正在进行的请求,清理它使用的资源,包括数据库连接和文件锁,然后退出。
const server = app.listen(port)
process.on('SIGTERM', () => {
debug('SIGTERM signal received: closing HTTP server')
server.close(() => {
debug('HTTP server closed')
})
})
负载均衡器使用健康检查来确定应用程序实例是否健康并且可以接受请求。 例如,Kubernetes 有两个健康检查:
liveness
,确定何时重新启动容器。readiness
,确定容器何时准备好开始接受流量。 当 pod 未就绪时,它会从服务负载均衡器中删除。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 会处理其余部分。
安装终端如下:
$ npm i @godaddy/terminus --save
这是一个说明如何使用总站的基本模板。 有关详细信息,请参阅 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 是一个独立的 HTTP 服务,作为一个单独的 HTTP 服务运行; 这允许拥有 health-readiness-liveness HTTP 端点,而无需将它们暴露在公共接口上。
按如下方式安装 Lightship:
$ npm install lightship
说明使用 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 文档 提供了对应Kubernetes 配置 的示例以及与Express.js 集成的完整示例。
http-terminator 实现了优雅终止 express.js 服务器的逻辑。
在 Node.js 中终止 HTTP 服务器需要跟踪所有打开的连接并向它们发出服务器正在关闭的信号。 http-terminator 实现了跟踪所有连接及其在超时时终止的逻辑。 http-terminator 还确保服务器意图关闭到当前正在从该服务器接收响应的任何客户端的正常通信。
安装 http-terminator 如下:
$ npm install http-terminator
说明使用 http-terminator 的基本模板:
const express = require('express')
const { createHttpTerminator } = require('http-terminator')
const app = express()
const server = app.listen(3000)
const httpTerminator = createHttpTerminator({ server })
app.get('/', (req, res) => {
res.send('ok')
})
// A server will terminate after invoking `httpTerminator.terminate()`.
// Note: Timeout is used for illustration of delayed termination purposes only.
setTimeout(() => {
httpTerminator.terminate()
}, 1000)
http-terminator 文档 提供 API 文档以及与其他现有第三方解决方案的比较。
express-actuator 是一个中间件,用于添加端点以帮助您监控和管理应用程序。
安装 express-actuator 如下:
$ npm install --save express-actuator
说明使用 express-actuator 的基本模板:
const express = require('express')
const actuator = require('express-actuator')
const app = express()
app.use(actuator())
app.listen(3000)
express-actuator 文档 提供了不同的定制选项。