SpringBoot之旅 -- SpringBoot 项目健康检查与监控
相关文章
前言
You build it,You run it, 当我们编写的项目上线后,为了能第一时间知晓该项目是否出现问题,常常对项目进行健康检查及一些指标进行监控。
Spring Boot-Actuator 就是帮助我们监控我们的Spring Boot 项目的。
使用
Spring Boot 最主要的特性就是AutoConfig(自动配置),而对于我们这些使用者来说也就是各种starter,
Spring Boot-Actuator 也提供了starter,为我们自动配置,在使用上我们只需要添加starter到我们的依赖中,然后启动项目即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
常用Endpoint
Spring Boot-actuator,提供了许多有用的EndPoint,对Spring Boot应用提供各种监控,下面说一下我常用的EndPoint:
/health 应用的健康状态
/configprops 获取应用的配置信息,因为Spring Boot 可能发布时是单独的Jar包,配置文件可能包含其中, 当我们需要检查配置文件时可以使用 ConfigpropsEndPoint 进行查看一些配置是否正确。
/trace 最近几次的http请求信息
HealthEndPoint
当我们访问 http://localhost:8088/health 时,可以看到 HealthEndPoint 给我们提供默认的监控结果,包含 磁盘检测和数据库检测。
{
"status": "UP",
"diskSpace": {
"status": "UP",
"total": 398458875904,
"free": 315106918400,
"threshold": 10485760
},
"db": {
"status": "UP",
"database": "MySQL",
"hello": 1
}
}
其实看 Spring Boot-actuator 源码,你会发现 HealthEndPoint 提供的信息不仅限于此,org.springframework.boot.actuate.health 包下 你会发现 ElasticsearchHealthIndicator、RedisHealthIndicator、RabbitHealthIndicator 等
也就是 HealthEndPoint 也提供 ES, Redis 等组件的健康信息。
自定义Indicator 扩展 HealthEndPoint
看源码 其实 磁盘和数据库健康信息就是 DiskSpaceHealthIndicator、DataSourceHealthIndicator 来实现的,当我们对一些我们自定义的组件进行监控时, 我们也可以实现个Indicator :
@Component
public class User implements HealthIndicator {
/**
* user监控 访问: http://localhost:8088/health
*
* @return 自定义Health监控
*/
@Override
public Health health() {
return new Health.Builder().withDetail("usercount", 10) //自定义监控内容
.withDetail("userstatus", "up").up().build();
}
}
这时我们再次访问: http://localhost:8088/health 这时返回的结果如下,包含了我们自定义的 User 健康信息。
{
"status": "UP",
"user": {
"status": "UP",
"usercount": 10,
"userstatus": "up"
},
"diskSpace": {
"status": "UP",
"total": 398458875904,
"free": 315097989120,
"threshold": 10485760
},
"db": {
"status": "UP",
"database": "MySQL",
"hello": 1
}
}
自定义EndPoint
其实除了扩展 HealthEndPoint 来添加一些健康检查, 我们也可以自定定义一些EndPoint 来提供程序运行时一些信息的展示:
@Configuration
public class EndPointAutoConfig {
@Bean
public Endpoint<Map<String, Object>> customEndPoint() {
return new SystemEndPoint();
}
}
@ConfigurationProperties(prefix="endpoints.customsystem")
public class SystemEndPoint extends AbstractEndpoint<Map<String, Object>> {
public SystemEndPoint(){
super("customsystem");
}
@Override
public Map<String, Object> invoke() {
Map<String,Object> result= new HashMap<>();
Map<String, String> map = System.getenv();
result.put("username",map.get("USERNAME"));
result.put("computername",map.get("COMPUTERNAME"));
result.put("userdomain",map.get("USERDOMAIN"));
return result;
}
}
访问 http://localhost:8088/customsystem 来查看我们自定义的EndPoint ,返回结果如下:
{
"username": "xxx",
"userdomain": "DESKTOP-6EAN1H4",
"computername": "DESKTOP-6EAN1H4"
}
SpringBoot之旅 -- SpringBoot 项目健康检查与监控的更多相关文章
- SpringBoot 项目健康检查与监控(转)
前言 You build it,You run it, 当我们编写的项目上线后,为了能第一时间知晓该项目是否出现问题,常常对项目进行健康检查及一些指标进行监控.Spring Boot-Actuator ...
- Kubernetes应用健康检查
目录贴:Kubernetes学习系列 在实际生产环境中,想要使得开发的应用程序完全没有bug,在任何时候都运行正常,几乎 是不可能的任务.因此,我们需要一套管理系统,来对用户的应用程序执行周期性的健康 ...
- springboot Actuator健康检查
通过情况下,如我们想在系统中添加一个健康检查的接口,我们怎么做呢? 我们会新建一个类,或在已存在类的基础上添加检测接口. package com.crhms.medicareopinion; impo ...
- SpringBoot健康检查实现原理
相信看完之前文章的同学都知道了SpringBoot自动装配的套路了,直接看spring.factories文件,当我们使用的时候只需要引入如下依赖 <dependency> <gro ...
- Springboot监控之一:SpringBoot四大神器之Actuator之2--覆盖修改spring cloud的默认的consul健康检查规则
微服务网关是socket长连接与支付公司对接,该网关需要提供http接口给内部系统调用,当socket没有建立连接时(网关服务的高可用是haProxy搭建的,有些服务的socket可能未连上支付公司) ...
- Springboot监控之一:SpringBoot四大神器之Actuator之2--springboot健康检查
Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring Boot 内置了一些 HealthIndicator. ...
- SpringBoot之旅第四篇-web开发
一.引言 有了自动配置,springboot使web开发变得简单,这个在springboot之旅中的第一篇中就有体现,实际的开发中当然不会这么简单,很多时候我们都需要自己去定制一些东西.web开发的东 ...
- springboot +mybatis 搭建完整项目
springboot + mybatis搭建完整项目 1.springboot整合mybatis注解版 转:https://blog.csdn.net/u013187139/article/detai ...
- 使用idea+springboot+Mybatis搭建web项目
使用idea+springboot+Mybatis搭建web项目 springboot的优势之一就是快速搭建项目,省去了自己导入jar包和配置xml的时间,使用非常方便. 1.创建项目project, ...
随机推荐
- 拖动条(SeekBar)的功能和用法
拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程序,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因而拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...
- HDU-1995-汉诺塔V
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1995 这题不知道该说水还是不水,对于这题我看到题目数据,就有了想法,因为题目数据给的好 所以我直接 假 ...
- JavaScript的核心
对象 ECMAScript做为一个高度抽象的面向对象语言,是通过对象来交互的.即使ECMAScript里边也有基本类型,但是,当需要的时候,它们也会被转换成对象. 一个对象就是一个属性集合,并拥有一个 ...
- linux redis 和 windows redis 的安装
Linux 下安装 下载地址:http://redis.io/download,下载最新文档版本. 本教程使用的最新文档版本为 2.8.17,下载并安装: $ wget http://download ...
- [转载] 几张非常有意义的JavaScript基础学习思维图
原文:http://www.w3cfuns.com/forum.php?mod=viewthread&tid=5598364&extra=page%3D1%26filter%3Ddig ...
- PLSQL程序流程
IF语句结构: if(条件表达式)- -then- -执行语句;- -end; IF-THEN-ELSE语句结构: if(条件表达式)- -then- -执行语句;- -else- -执行语句;- e ...
- ubuntu12.04下安装pptp_vpn服务器
1.下载安装apt-get install pptpd 2.配置/etc/pptpd.confvim /etc/pptpd.conf添加下面两行(在配置文件的最后取消注释修改IP即可)localip ...
- 在Windows Server2016里通过Hyper-V安装Liunx Container
Windows Server2016正式发布已经有一段时间了.无可厚非,原生Docker的支持是这个版本一个非常大的亮点. Windows Container 的安装在网上有很多教程,我这里不过多的描 ...
- fiddler介绍
先看fiddler 的使用界面和各模块的功能介绍 1请求列表:请求列表中包含了许多信息,从左至右依次为,#(序列号),Result(结果状态码),Prottocol(请求的协议),Host(请求的主机 ...
- 【转】对于HttpClient和HtmlUnit的理解
原文地址:http://www.haohaoblog.com/?p=1327&utm_source=tuicool 做Java编程的人其实,很多不懂SEO,也不知道如何让百度收录等等,当然,对 ...