前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot核心技术:探究Actuator的默认开放节点 & 详细健康状态

SpringBoot核心技术:探究Actuator的默认开放节点 & 详细健康状态

作者头像
恒宇少年
发布2018-11-22 10:30:30
2.1K0
发布2018-11-22 10:30:30
举报

系统的监控在分布式的设计中显得尤为重要,因为分开部署的缘故,并不能及时的了解到程序运行的实时状况,之所以重要所以SpringBoot也给我提供了一套自动监控的API,可以无缝整合spring-boot-admin来完成图形化的展示,我们本章先来介绍下actuator系统监控相关内容。

本章目标

通过spring-boot-actuator完成系统运行监控,实时了解程序运行的环境是否健康。

构建项目

使用idea开发工具创建SpringBoot项目并添加spring-boot-starter-actuator以及spring-boot-starter-web(如果缺少web依赖会导致本章项目无法启动(没有内部集成的web容器无法运行.),pom.xml文件部分内容如下所示:

代码语言:javascript
复制
.....
<description>
    SpringBoot核心技术:使用Actuator监控你的应用程序
    博客文章地址:http://blog.yuqiyu.com/spring-boot-actuator.html
</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <!--Web-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--Actuator-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
.....

配置绑定映射类

有关本章开放节点的配置都被映射到属性配置类org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties中,通过@ConfigurationProperties注解自动映射application.yml配置文件内以前缀management.endpoints.web的属性配置信息。

默认开放的节点

Actuator默认开放了两个节点信息,分别是:

  • health:健康监测节点 健康节点我们在访问时默认只可以查看当前系统的运行状态,如下所示:
代码语言:javascript
复制
{
    "status": "UP"
}

如果不开放相关的配置无法查看详细的运行健康信息,比如:硬盘等,具体的开放方法在本章查看详细健康状态

  • info:基本信息查看节点

我们在属性类WebEndpointProperties内也并没有看到healthinfo作为初始化的值赋值给exposure.include,那么是怎么进行赋值的呢?

元数据配置文件

spring-configuration-metadata.json(元数据配置文件)位于spring-boot-actuator-autoconfigure-2.0.6.jar依赖的META-INF目录下,主要作用是对应WebEndpointProperties等属性配置类的字段类型描述默认值对应目标字段的定义,具体的实现我在自定义starter的文章内有讲到,我们找到namemanagement.endpoints.web.exposure.include的配置如下:

代码语言:javascript
复制
.....
{
  "sourceType": "org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties$Exposure",
  "defaultValue": [
    "health",
    "info"
  ],
  "name": "management.endpoints.web.exposure.include",
  "description": "Endpoint IDs that should be included or '*' for all.",
  "type": "java.util.Set<java.lang.String>"
},
.....

通过配置的defaultValue来自动映射到WebEndpointProperties属性配置类的Exposure#include字段,下面简单介绍上面的字段:

  • sourceType:该配置字段所关联配置类的类型(全限定名)
  • defaultValue:默认值,根据type来设置,如上java.util.Set<java.lang.String>类型的默认值就可以通过["health","info"]设置
  • name:字段的名称,对应配置类内的field名称
  • description:该配置字段的描述信息,可以是中文,填写后idea工具会自动识别并提示
  • type:该字段的类型的全限定名,如:java.lang.String

查看详细健康状态

开启查看详细健康状态比较简单,通过配置参数management.endpoint.health.show-details来进行修改,该参数的值由org.springframework.boot.actuate.health.ShowDetails枚举提供配置值,ShowDetails源码如下所示:

代码语言:javascript
复制
/**
 * Options for showing details in responses from the {@link HealthEndpoint} web
 * extensions.
 *
 * @author Andy Wilkinson
 * @since 2.0.0
 */
public enum ShowDetails {

    /**
     * Never show details in the response.
     */
    NEVER,

    /**
     * Show details in the response when accessed by an authorized user.
     */
    WHEN_AUTHORIZED,

    /**
     * Always show details in the response.
     */
    ALWAYS

}

spring-configuration-metadata.json元数据文件内,配置的showDetails的默认值为never,也就是不显示详细信息,配置如下所示:

代码语言:javascript
复制
.....
{
  "sourceType": "org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties",
  "defaultValue": "never",
  "name": "management.endpoint.health.show-details",
  "description": "When to show full health details.",
  "type": "org.springframework.boot.actuate.health.ShowDetails"
},
.....

我们修改management.endpoint.health.show-details参数为always后重新项目再次访问/actuator/health就可以获取如下的信息:

代码语言:javascript
复制
{
    "status": "UP",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 250790436864,
                "free": 77088698368,
                "threshold": 10485760
            }
        }
    }
}

自定义节点访问前缀

actuator默认的所有节点的访问前缀都是/actuator,在application.yml配置文件内设置management.endpoints.web.basePath参数进行修改,如下所示:

代码语言:javascript
复制
# 管理节点配置
management:
  endpoints:
    web:
      # actuator的前缀地址
      base-path: /

basePath字段位于WebEndpointProperties属性配置类内,修改完成重启项目就可以使用修改后的路径进行访问,我们上述直接映射到了/下。

总结

通过本章的讲解我们明白了spring-boot-actuator默认开放了healthinfo两个节点,通过配置健康检查详细信息可以查看硬盘相关的运行健康状态。

源码位置

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.10.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本章目标
  • 构建项目
  • 配置绑定映射类
  • 默认开放的节点
    • 元数据配置文件
    • 查看详细健康状态
    • 自定义节点访问前缀
    • 总结
    • 源码位置
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档