springboot actuator 常用接口

概述

微服务作为一项在云中部署应用和服务的新技术是当下比较热门话题,而微服务的特点决定了功能模块的部署是分布式的,运行在不同的机器上相互通过服务调用进行交互,业务流会经过多个微服务的处理和传递,在这种框架下,微服务的监控显得尤为重要。

Actuator正是Spring Boot提供的对应用系统的监控和管理的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的Spring beans信息、系统环境变量的配置信以及Web请求的详细信息等。如果使用不当或者一些不经意的疏忽,可能造成信息泄露等严重的安全隐患。

Actuator使用

Actuator应用监控使用只需要添加spring-boot-starter-actuator依赖即可,如下:

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

可以在application.properties中指定actuator的访问路径,如指定路径为/monitor:

management.context-path=/monitor

此时,运行示例,访问/monitor/env即可查看系统环境变量的配置信息,之后再访问/monitor/trace即可查看所有Web请求的详细信息,包括请求方法、路径、时间戳以及请求和响应的头信息甚至cookie信息,如图:

Actuator监控分成两类:原生端点和用户自定义扩展端点,原生的主要有:

路径描述
/autoconfig提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过
/beans描述应用程序上下文里全部的Bean,以及它们的关系
/env获取全部环境属性
/configprops描述配置属性(包含默认值)如何注入Bean
/dump获取线程活动的快照
/health报告应用程序的健康指标,这些值由HealthIndicator的实现类提供
/info获取应用程序的定制信息,这些信息由info打头的属性提供
/mappings描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系
/metrics报告各种应用程序度量信息,比如内存用量和HTTP请求计数
/shutdown关闭应用程序,要求endpoints.shutdown.enabled设置为true
/trace提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)
安全措施

如果上述请求接口不做任何安全限制,安全隐患显而易见。实际上Spring Boot也提供了安全限制功能。比如要禁用/env接口,则可设置如下:

endpoints.env.enabled= false

如果只想打开一两个接口,那就先禁用全部接口,然后启用需要的接口:

endpoints.enabled = false
endpoints.metrics.enabled = true

另外也可以引入spring-boot-starter-security依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
 </dependency>

在application.properties中指定actuator的端口以及开启security功能,配置访问权限验证,这时再访问actuator功能时就会弹出登录窗口,需要输入账号密码验证后才允许访问。

management.port=8099
management.security.enabled=true
security.user.name=admin
security.user.password=admin
安全建议

在使用Actuator时,不正确的使用或者一些不经意的疏忽,就会造成严重的信息泄露等安全隐患。在代码审计时如果是springboot项目并且遇到actuator依赖,则有必要对安全依赖及配置进行复查。也可作为一条规则添加到黑盒扫描器中进一步把控。
安全的做法是一定要引入security依赖,打开安全限制并进行身份验证。同时设置单独的Actuator管理端口并配置不对外网开放。

参考:spring-boot-starter-actuator监控接口详解 - 简书

spring-boot-starter-actuator功能简介

根据应用依赖和配置自动创建出来的监控和管理端点。通过这些端点,我们可以实时获取系统应用的各项监控指标。

spring-boot-starter-actuator功能集成

第一步:添加相关jar包依赖

<!--srping-boot-actuator-->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

第二步:application.properties 配置Spring-Boot-Actuator配置

#spring-boot-actuator配置

#开放所有的web Endpoints

management.endpoints.web.exposure.include=*

第三步:获取监控端点详细信息和效果展示

通过访问:http://localhost:8082/actuator,获取监控端点详细信息

字符串格式化:

{
        "_links":{
        "self":{
        "href":"http://localhost:8082/actuator",
        "templated":false
        },
        "archaius":{
        "href":"http://localhost:8082/actuator/archaius",
        "templated":false
        },
        "auditevents":{
        "href":"http://localhost:8082/actuator/auditevents",
        "templated":false
        },
        "beans":{
        "href":"http://localhost:8082/actuator/beans",
        "templated":false
        },
        "caches-cache":{
        "href":"http://localhost:8082/actuator/caches/{cache}",
        "templated":true
        },
        "caches":{
        "href":"http://localhost:8082/actuator/caches",
        "templated":false
        },
        "health":{
        "href":"http://localhost:8082/actuator/health",
        "templated":false
        },
        "health-component":{

        "href":"http://localhost:8082/actuator/health/{component}",

        "templated":true

        },

        "health-component-instance":{

        "href":"http://localhost:8082/actuator/health/{component}/{instance}",

        "templated":true

        },

        "conditions":{

        "href":"http://localhost:8082/actuator/conditions",

        "templated":false

        },

        "configprops":{

        "href":"http://localhost:8082/actuator/configprops",

        "templated":false

        },

        "env":{

        "href":"http://localhost:8082/actuator/env",

        "templated":false

        },

        "env-toMatch":{

        "href":"http://localhost:8082/actuator/env/{toMatch}",

        "templated":true

        },

        "info":{

        "href":"http://localhost:8082/actuator/info",

        "templated":false

        },

        "loggers":{

        "href":"http://localhost:8082/actuator/loggers",

        "templated":false

        },

        "loggers-name":{

        "href":"http://localhost:8082/actuator/loggers/{name}",

        "templated":true

        },

        "heapdump":{

        "href":"http://localhost:8082/actuator/heapdump",

        "templated":false

        },

        "threaddump":{

        "href":"http://localhost:8082/actuator/threaddump",

        "templated":false

        },

        "metrics-requiredMetricName":{

        "href":"http://localhost:8082/actuator/metrics/{requiredMetricName}",

        "templated":true

        },

        "metrics":{

        "href":"http://localhost:8082/actuator/metrics",

        "templated":false

        },

        "scheduledtasks":{

        "href":"http://localhost:8082/actuator/scheduledtasks",

        "templated":false

        },

        "httptrace":{

        "href":"http://localhost:8082/actuator/httptrace",

        "templated":false

        },

        "mappings":{

        "href":"http://localhost:8082/actuator/mappings",

        "templated":false

        },

        "refresh":{

        "href":"http://localhost:8082/actuator/refresh",

        "templated":false

        },

        "features":{

        "href":"http://localhost:8082/actuator/features",

        "templated":false

        },

        "service-registry":{

        "href":"http://localhost:8082/actuator/service-registry",

        "templated":false

        }

   }
}

spring-boot-starter-actuator模块生成的原生监控端点,分为三大类:

  1. 应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与SpringBoot应用密切相关的配置类信息;这类端点可以帮助我们获取一系列关于Spring应用配置内容的详细报告,比如自动化配置的报告、Bean创建的报告、环境属性的报告等。
  2. 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如内存信息、线程池信息、HTTP请求统计等;
  3. 操作监控类:提供了对应用的关闭等操作类功能;

以上三类监控端点的详细信息,下面我们详细来说明。

第四步:监控端点信息描述

应用配置类:默认启用
  1. /autoconfig: 该端点用来获取应用的自动化配置报告,其中包括所有自动化配置的候选项,同时列举了每个候选项是否满足自动化配置的各个先决条件。该端点可以帮助我们方便地找到一些自动化配置为什么没有生效的具体原因。报告内容分为两部分,如下:
    (1)positiveMatches中返回的是条件匹配成功的自动化配置;
    (2)negativeMathches中返回的是条件匹配不成功的自动化配置。
  2. /beans: 该端点用来获取应用上下文中创建的所有Bean。包含的具体信息如下:
    (1)bean:Bean的名称;
    (2)scope: Bean的作用域;
    (3)type: Bean的Java类型;
    (4)resource: class文件的具体路径;
    (5)dependencies: 依赖的Bean的名称;
  3. /configprops: 该端点用来获取应用中配置的属性信息报告。我们可以通过该报告来看到各个属性的配置路径,比如我们要关闭端点,就可以使用endpoints.configprops.enabled=false来完成设置。
  4. /env: 该端点用来获取应用所有可用的环境属性报告,具体包括环境变量、JVM属性、应用的配置属性、命令行中的参数。通过该端点返回的信息,我们可以看到当前应用加载的配置信息,可以结合@ConfigurationProperties注解将它们引入到应用程序中使用。对于一些敏感属性信息,比如在属性名中包含password,secret,key这些关键词,在返回的时候会使用*来替换。
  5. /mappings: 该端点用来返回所有Spring MVC的控制器映射关系报告。返回的信息有:
    (1)bean属性: 标识该映射关系的请求处理器;
    (2)method属性:标识该映射关系的具体处理类和处理函数;
  6. /info:该端点用来返回一些应用自定义的信息。默认情况下,该端点只会返回一个空的JSON内容。我们可以在application.properties配置文件中设置一些以info为前缀的属性配置信息,就能看到效果。
度量指标类:默认启用
  1. /metrics: 该端点用来返回当前应用的各类重要度量指标,比如内存信息、线程信息、垃圾回收信息等。具体信息包含如下:
    (1)系统信息:包括处理器数量processors、运行时间uptime和instance.uptime、系统平均负载systemload.average;
    (2)mem.*: 内存概要信息,包括分配给应用的总内存数量以及当前空闲的内存数量,这些信息来自java.lang.Runtime;
    (3)heap.*: 堆内存使用情况。这些信息来自java.lang.management.MemoryMXBean接口中getHeapMemoryUsage方法获取的java.lang.management.MemoryUsage;
    (4)nonheap.*: 非堆内存使用情况。这些信息来自java.lang.management.MemoryMXBean接口中getNonHeapMemoryUsage方法获取java.lang.management.MemoryUsage;
    (5)threads.*: 线程使用情况,包括线程数、守护线程(daemon)、线程峰值(peak)等,这些信息来自java.lang.management.ThreadMXBean;
    (6)classes.*: 应用加载和卸载的类统计,这些信息来自java.lang.managemeng.ClassLoadingMXBean;
    (7)gc.*: 垃圾收集器的详细信息,包括垃圾回收次数gc.ps_scavenge.count、垃圾回收消耗时间gc.ps_scavenge.time、标记-清除算法的次数gc.ps_marksweep.count、标记-清除算法的消耗时间gc.ps_marksweep.time。这些信息来自java.lang.management.GarbageCollectorMXBean;
    (8)httpsessions.*: Tomcat容器的会话使用情况。包括最大会话数httpsessions.max和活跃会话数httpsessions.active。该度量指标信息仅在引入嵌入式Tomcat作为应用容器的时候才会提供;
    (9)gauge.*: HTTP请求的性能指标之一,它主要用来反映一个绝对值。
    (10)counter.*: HTTP请求的性能指标之一,它主要作为计数器来使用,记录了增加量和减少量。
    报告的具体内容如下:
  2. /health: 该端点用来获取应用的各类健康指标信息。在spring-boot-starter-actuator模块中自带实现了一些常用资源的健康指标检测器,这些健康指标检测器都是通过HealthIndicator接口实现,并且会根据依赖关系的引入实现自动化配置。常见的一些健康指标检测器如下:
    (1)DiskSpaceHealthIndicator: 低磁盘空间检测;
    (2)DataSourceHealthIndicator: 检测DataSource的链接是否成功;
    (3)MongoHealthIndicator: 检测Mongo数据库是否可用;
    (4)RabbitHealthIndicator: 检测Rabbit服务器是否可用;
    (5)RedisHealthIndicator: 检测Redis服务器是否可用;
    (6)SolrHealthIndicator: 检测Solr服务器是否可用;
    我们也可用通过实现HealthIndicator接口来自定义自己的健康指标检测器;
    报告的具体内容如下:
  3. /dump: 该端点用来暴露程序运行中的线程信息。它使用java.lang.management.ThreadMXBean的dumpAllThreads方法来返回所有含有同步信息的活动线程详情;
  4. /trace: 该端点用来返回基本的HTTP跟踪信息。默认情况下,跟踪信息的存储采用org.springframework.boot.actuate.trace.InMemoryTraceRepository实现的内存方式,始终保留最近的100条请求记录。
操作控制类:需要通过属性配置来开启操作

     1./shutdown: 该端点用来实现关闭应用的远程操作,需要配置属性endpoints.shutdown.enable=true才能实现。该操作非常危险,不建议使用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 1. spring-boot-starter-web 2. spring-boot-starter-data-jpa 3. spring-boot-starter-test 4. spring-boot-starter-security 5. spring-boot-starter-aop 6. spring-boot-starter-thymeleaf 7. spring-boot-starter-freemarker 8. spring-boot-starter-actuator 9. spring-boot-starter-logging 10. spring-boot-starter-validation ### 回答2: Spring Boot是一个开源的Java开发框架,它的设计目标是简化Spring应用程序的初始化和配置过程。为了进一步简化开发过程,Spring Boot推出了一系列的starter(启动器)来提供一些常用功能的自动化配置。下面是几个常用的Spring Boot starter: 1. spring-boot-starter-web:这是Spring Boot最常用的starter之一,它提供了构建基于Spring MVC的Web应用程序所需的依赖和自动配置。包括Tomcat、Spring Web MVC、Jackson等。 2. spring-boot-starter-data-jpa:这个starter为使用Spring Data JPA访问数据库提供了便利。它包括了Hibernate、Spring Data JPA以及对不同数据库的自动配置。 3. spring-boot-starter-security:这个starter提供了基于Spring Security的安全功能。它包括了Spring Security的依赖以及一些常用的安全配置,如认证、授权等。 4. spring-boot-starter-test:这个starter提供了编写单元测试和集成测试所需的依赖和自动配置。包括JUnit、Mockito、Hamcrest等。 5. spring-boot-starter-actuator:这个starter为监控和管理Spring Boot应用程序提供了便利。它包括了一系列监控和管理功能的依赖和自动配置,如健康检查、性能指标、配置管理等。 除了上述提到的几个starter,Spring Boot还有很多其他的starter可以选择,如spring-boot-starter-redis、spring-boot-starter-mail等,用于集成其他常用的功能或服务。使用starter可以极大地简化配置和集成的过程,提高开发效率。 ### 回答3: Spring Boot是一个开源的Java框架,它简化了Java应用程序的开发并提供了大量的开箱即用的功能。Spring Boot提供了许多常用的starter来帮助开发人员快速构建应用程序。 常用的Spring Boot starter有以下几种: 1. Spring Boot Starter Web:这是用于构建Web应用程序的starter,包含了Spring MVC、Tomcat和其他与Web开发相关的依赖。它简化了Web应用程序的开发,使开发人员可以更快速地搭建起一个可运行的Web应用。 2. Spring Boot Starter Data JPA:这是用于访问数据库的starter,使用了Spring Data JPA技术。它提供了常用的CRUD操作方法和一些常见的查询功能,大大简化了对数据库的操作。 3. Spring Boot Starter Security:这是用于实现应用程序安全的starter。它提供了认证(Authentication)和授权(Authorization)功能,可以方便地实现用户登录和权限控制。 4. Spring Boot Starter Test:这是用于编写单元测试和集成测试的starter。它提供了一些常用的测试注解和工具类,使开发人员可以更方便地进行测试。 5. Spring Boot Starter Actuator:这是用于监控和管理应用程序的starter。它提供了一些监控接口和端点,可以通过HTTP请求获取应用程序的性能指标、健康状况等信息。 这些是Spring Boot常用的starter,它们提供了不同方面的功能和依赖,可以根据应用程序的需要选择和使用。使用这些starter可以极大地提高开发效率,减少重复工作,让开发人员可以更专注于业务逻辑的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micrle_007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值