健康检查和正常关机

¥Health Checks and Graceful Shutdown

正常关机

¥Graceful shutdown

当你部署新版本的应用时,你必须替换以前的版本。你正在使用的 进程管理器 将首先向应用发送一个 SIGTERM 信号,通知它它将被杀死。一旦应用收到此信号,它应该停止接受新请求,完成所有正在进行的请求,清理它使用的资源,包括数据库连接和文件锁,然后退出。

¥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 should stop accepting new requests, finish all the ongoing requests, clean up the resources it used, including database connections and file locks then exit.

正常关机示例

¥Example Graceful Shutdown

const server = app.listen(port)

process.on('SIGTERM', () => {
  debug('SIGTERM signal received: closing HTTP server')
  server.close(() => {
    debug('HTTP server closed')
  })
})

健康检查

¥Health checks

负载均衡器使用健康检查来确定应用实例是否健康并且可以接受请求。例如,Kubernetes 有两个健康检查

¥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:

第三方解决方案

¥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 是一个开源项目,可为你的应用添加健康检查和正常关闭功能,从而无需编写样板代码。你只需提供用于正常关闭的清理逻辑和用于健康检查的健康检查逻辑,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.

安装 terminus 如下:

¥Install terminus as follows:

$ npm i @godaddy/terminus --save

这是一个说明如何使用 terminus 的基本模板。有关详细信息,请参阅 https://github.com/godaddy/terminus

¥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 是一个开源项目,可为你的应用添加健康、就绪和活跃度检查。Lightship 是一个独立的 HTTP 服务,作为一个单独的 HTTP 服务运行;这允许拥有 health-readiness-liveness HTTP 端点,而无需将它们暴露在公共接口上。

¥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.

按如下方式安装 Lightship:

¥Install Lightship as follows:

$ npm install lightship

说明使用 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 文档 提供了对应 Kubernetes 配置 的示例以及与 Express.js 集成的完整示例。

¥Lightship documentation provides examples of the corresponding Kubernetes configuration and a complete example of integration with Express.js.

http-terminator

http-terminator 实现了优雅终止 express.js 服务器的逻辑。

¥http-terminator implements logic for gracefully terminating an express.js server.

在 Node.js 中终止 HTTP 服务器需要跟踪所有打开的连接并向它们发出服务器正在关闭的信号。http-terminator 实现了跟踪所有连接及其在超时时终止的逻辑。http-terminator 还确保服务器意图关闭到当前正在从该服务器接收响应的任何客户端的正常通信。

¥Terminating a HTTP server in Node.js requires keeping track of all open connections and signaling them that the server is shutting down. http-terminator implements the logic for tracking all connections and their termination upon a timeout. http-terminator also ensures graceful communication of the server intention to shutdown to any clients that are currently receiving response from this server.

安装 http-terminator 如下:

¥Install http-terminator as follows:

$ npm install http-terminator

说明使用 http-terminator 的基本模板:

¥Basic template that illustrates using 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 文档以及与其他现有第三方解决方案的比较。

¥http-terminator documentation provides API documentation and comparison to other existing third-party solutions.

express-actuator

express-actuator 是一个中间件,用于添加端点以帮助你监控和管理应用。

¥express-actuator is a middleware to add endpoints to help you monitor and manage applications.

安装 express-actuator 如下:

¥Install express-actuator as follows:

$ npm install --save express-actuator

说明使用 express-actuator 的基本模板:

¥Basic template that illustrates using express-actuator:

const express = require('express')
const actuator = require('express-actuator')

const app = express()

app.use(actuator())

app.listen(3000)

express-actuator 文档 提供了不同的定制选项。

¥The express-actuator documentation provides different options for customization.