nodejs微服务健康检查方案
1. 前言
针对目前云平台方案,因为网络、主机状态等诸多因素,单台主机上的服务出现问题的几率大大增加。这就要求我们能够监控每台主机、每个微服务实例的健康状态。因此对于nodejs相关项目需要做相关的微服务健康检查接口。
在不改动原有express框架的基础上,我在express官方网站上查找到相应的健康检查的样例,做成demo供大家参考。
(链接https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html)
2. 方案实现demo
我是以agent做的demo,以下是我修改的app.js代码:红色代码为我添加的的部分。为容器提供对应的健康检查端口。
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var config = require('./config/config').getInstance().config;
var logg = config.logger;
var moment = require('moment');
var comm = require('./middlewares/comm');
var routes = require('./routes/index');
var app = express();
app.set('env', config.debug ? 'development' : 'production');
app.set('port', process.env.PORT || config.port);
app.set('trust proxy', config.proxy); // 指定子网和 IP 地址
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//session redis存储
const store = new RedisStore({
host: config.redis.host,
port: config.redis.port,
pass: config.redis.passwd,
});
//设置session
app.use(session({
store: store,
name: 'ghjhgz',
secret: 'dfgdfgfdgdfgdfgderte435sd',
resave: true,
rolling: true,
saveUninitialized: false,
cookie: {domain: config.domain}
}));
// 添加模板必需的变量
app.use(function (req, res, next) {
res.locals.user = '';
next();
});
routes(app);
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
logg.error(err);
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
if(config.debug){
res.render('error');
}else{
res.render('404');
}
});
/* istanbul ignore next */
if (!module.parent) {
app.listen(config.port, function () {
console.log('listening on port: ' + config.port);
});
}
module.exports = app;
const http = require('http');
const terminus = require('@godaddy/terminus');
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
console.log('HealthCheck is starting');
}
terminus(server, {
signal: 'SIGINT',
healthChecks: {
'/healthcheck': onHealthCheck,
},
onSignal
});
server.listen(3000);
目前,只需要修改一下app.js,onHealthCheck函数接口为健康检查接口,后续可以提供检查对应的系统健康,比如数据库或者redis链接状态等。
2.1 依赖库terminus
安装依赖
npm i @godaddy/terminus --save
Terminus是一个开放源代码项目,它将健康检查和正常关闭添加到您的应用程序中,从而无需编写样板代码。您只需提供用于正常关闭的清理逻辑和用于运行状况检查的运行状况检查逻辑,而终点则处理其余部分。
2.2 有限的Windows支持
由于固有的平台限制,terminus对Windows的支持有限。你可以期望SIGINT工作,以及在SIGBREAK某种程度上SIGHUP。但是,SIGTERM在Windows上永远不会工作,因为在任务管理器中查杀进程是无条件的,也就是说,应用程序无法检测或阻止进程。
2.3 Terminus源码GitHub地址
https://github.com/godaddy/terminus
3. Kubernetes对应的接口
使用livenessProbe探针对开放的端口进行检测。
livenessProbe:
httpGet:
path: /healthcheck #对应应用的健康路径
port: 3000 #统一的健康检查端口,在云平台内部不会出现端口冲突
initialDelaySeconds: 15
periodSeconds: 5
timeoutSeconds: 1
nodejs微服务健康检查方案的更多相关文章
- hydra nodejs 微服务框架简单试用
hydra 是一个以来redis 的nodejs 微服务框架 安装 需要redis,使用docker 进行运行 redis docker run -d -p 6379:6379 redis 安装yo ...
- nodejs微服务
近来公司增加了nodejs微服务 它的主要任务是接收来自于现场的采集数据:作业记录和流转记录,动态构建一个基地的全景实时数据 暂时不涉及数据库. 如果要进行数据库操作,不建议使用本模块, ...
- 【Spring Cloud】微服务架构选型方案
1.技术架构 2.组件介绍 1.服务注册与发现——Eureka 服务注册与发现中心采用Eureka,以AP为核心的高可用注册中心,保证高可用性和最终一致性,server之间互相注册的replicate ...
- Nginx实战|Nginx健康检查
开源Linux 长按二维码加关注~ 上一篇:盘点提高国内访问Github的速度的9种方案 服务治理的一个重要任务是感知服务节点变更,完成服务自动注册及异常节点的自动摘除.这就需要服务治理平台能够:及时 ...
- SpringCloud微服务实战——搭建企业级开发框架(四十五):【微服务监控告警实现方式二】使用Actuator(Micrometer)+Prometheus+Grafana实现完整的微服务监控
无论是使用SpringBootAdmin还是使用Prometheus+Grafana都离不开SpringBoot提供的核心组件Actuator.提到Actuator,又不得不提Micrometer ...
- 微服务与SpringCloud简介
A.官网 https://spring.io/projects/spring-cloud B.简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用 ...
- Spring Cloud与微服务构建:Spring Cloud简介
Spring Cloud简介 微服务因该具备的功能 微服务可以拆分为"微"和"服务"二字."微"即小的意思,那到底多小才算"微&q ...
- 微服务时代之网关相关技术选型及部署(nacos+gateway)
1.场景描述 因要用到微服务,关于注册中心这块,与同事在技术原型上做了讨论,初步定的方案是使用:阿里巴巴的nacos+springcloud gateway,下面表格是同事整理的注册中心对比,以前用的 ...
- 构建安全可靠的微服务 | Nacos 在颜铺 SaaS 平台的应用实践
作者 | 殷铭 颜铺科技架构师 本文整理自架构师成长系列 3 月 19 日直播课程. 关注"阿里巴巴云原生"公众号,回复 "319",即可获取对应直播回放链接 ...
随机推荐
- Java代码实现MySQL数据库的备份与还原
通常在MySQL数据库的备份和恢复的时候,多是采用在cmd中执行mysql命令来实现. 例如: mysqldump -h127.0.0.1 -uroot -ppass test > d:/tes ...
- ASP.NET-ActionFilter过滤器用法实例
ActionFilter可以对每一个传过来的action请求进行过滤,非常有用,但是如果在这里判断过多,那么网站的性能和速度会不会变慢,这个问题值得思考,现在先放在这里. public class A ...
- flex 通过htmlservices链接moss的rest(rest 的get post方式)
一:flex debug(调试)--trace() --moss导入 flex学习:1.flex出现不能使用trace调试语句的问题,控制台无信息输出.这个问题不须要改动安装文件的參量. 仅仅须要下载 ...
- 2014百度之星资格赛—— Xor Sum(01字典树)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- [Gatsby] Install Gatsby and Scaffold a Blog
In this lesson, you’ll install Gatsby and the plugins that give the default starter the ability to t ...
- 手势跟踪论文学习:Realtime and Robust Hand Tracking from Depth(三)Cost Function
iker原创.转载请标明出处:http://blog.csdn.net/ikerpeng/article/details/39050619 Realtime and Robust Hand Track ...
- 宿主机mac os无法连接到虚拟机centos
宿主机: Mac OS 10.9.2 虚拟机: [root@localhost ~]# cat /etc/redhat-release CentOS release 6.4 (Final) [root ...
- 英特尔深度学习框架BigDL——a distributed deep learning library for Apache Spark
BigDL: Distributed Deep Learning on Apache Spark What is BigDL? BigDL is a distributed deep learning ...
- nyoj--1237--最大岛屿(dfs+数据处理)
最大岛屿 时间限制:1000 ms | 内存限制:65535 KB 难度: 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战 ...
- sublime配置python运行环境
1.sublime下载与插件管理 1.1 下载 官网地址:https://www.sublimetext.com/3 1.2 安装Package Control管理插件 使用ctrl + ` (感叹后 ...