當您部署應用程式的最新版本時,您必須取代舊版本。您正在使用的處理程序管理員會先傳送 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 未準備好時,它會從服務負載平衡器中移除。警告:此資訊涉及第三方網站、產品或模組,這些網站、產品或模組並非由 Expressjs 團隊維護。在此列出並不構成 Expressjs 專案團隊的認可或推薦。
Terminus 是個開放原始碼專案,可將健康檢查和正常關機功能新增到您的應用程式,以消除撰寫樣板程式碼的需求。您只需提供正常關機的清理邏輯和健康檢查的健康檢查邏輯,Terminus 會處理其餘部分。
安裝 Terminus 的方式如下
$ npm i @godaddy/terminus --save
以下是說明如何使用 Terminus 的基本範本。如需更多資訊,請參閱 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 服務執行;這允許擁有健康、準備就緒和活性 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 文件 提供不同的自訂選項。