SpringBoot如何实现项目健康检查与监控

发布时间:2021-05-24 11:43:46 作者:小新
来源:亿速云 阅读:180

这篇文章主要介绍SpringBoot如何实现项目健康检查与监控,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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"
}

我们在为Spring Boot应用添加actuator后,期望的health接口返回结果应该是类似下面的结果:

{
 status: "UP",
 diskSpace: 
 {
 status: "UP",
 total: 250182889472,
 free: 31169568768,
 threshold: 10485760
 },
 db: 
 {
 status: "UP",
 database: "H2",
 hello: 1
 }
}

如果只是返回了status

{
 status: "UP"
}

则需要为应用新增配置,以yml配置文件为例,需要添加如下配置:

management:
 security:
 enabled: false
endpoints:
 health:
 sensitive: false
management.endpoint.health.show-details=always

以上是“SpringBoot如何实现项目健康检查与监控”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Kong 的健康检查和监控
  2. SpringBoot:强大的 Actuator 服务监控与管理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot

上一篇:SpringBoot如何实现JPA的save方法不更新null属性

下一篇:基于SpringBoot如何实现图片上传与显示

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》