Spring Boot Actuator详解

这篇具有很好参考价值的文章主要介绍了Spring Boot Actuator详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Actuator简介

什么是Spring Boot Actuator?

  • Spring Boot Actuator 模块提供了生产级别的功能,比如健康检查审计指标收集HTTP跟踪等,帮助我们监控和管理Spring Boot应用。
  • 这个模块是一个采集应用内部信息暴露给外部的模块,上述的功能都可以通过HTTP和JMX访问。
  • 因为暴露内部信息的特性,Actuator也可以和一些外部的应用监控系统整合(Prometheus, Graphite, DataDog等)。这些监控系统提供了出色的仪表板,图形,分析和警报,可帮助你通过一个统一友好的界面,监视和管理你的应用程序。
  • Actuator使用Micrometer与这些外部应用程序监视系统集成。这样一来,只需很少的配置即可轻松集成外部的监控系统。

Micrometer为Java 平台上的性能数据收集提供了一个通用的API,应用程序只需要使用Micrometer的通用API来收集性能指标即可。Micrometer会负责完成与不同监控系统的适配工作,这就使得切换监控系统变得很容易。对比Slf4j之于Java Logger中的定位

Endpoints介绍

Endpoints简介

Spring Boot提供了所谓endpoints(下文翻译为端点)用于外部系统应用程序进行访问和交互。
例如,/health端点提供了关于应用健康情况的一些基础信息。/metrics端点提供了一些有用的应用程序指标(JVM 内存使用、系统CPU使用等)。

Endpoints类型

Actuator模块本来就有的端点我们称之为原生端点。根据端点的作用的话,我们大概可以分为三大类:

  • 应用配置类: 获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring Boot应用密切相关的配置类信息。
  • 度量指标类: 获取应用程序运行过程中用于监控的度量指标,比如:内存信息、线程池信息、HTTP请求统计等。
  • 操作控制类: 提供了对应用的关闭等操作类功能。

需要注意的就是:

  • 每一个端点都可以通过配置来单独禁用或者启动
  • 不同于Actuator 1.x,Actuator 2.x 的大多数端点默认被禁掉。 Actuator 2.x中的默认端点增加了/actuator前缀。默认暴露的两个端点为/actuator/health/actuator/info

Endpoints清单

此处使用的是SpringBoot 2.2.8版本,详情请查看Spring官方文件

Endpoint HTTP方法 描述
/actuator GET 查看有哪些Actuator endpoint是开放的
/actuator/auditevent GET 显示应用暴露的审计事件 (比如认证进入、订单失败),需要搭配Spring Security使用,示例代码
/actuator/beans GET 查看当前上下文中配置的所有的Bean
/actuator/conditions GET 该端点用来获取应用的自动化配置报告,其中包括所有自动化配置的候选项。同时还列出了每个候选项自动化配置的各个先决条件是否满足。所以,该端点可以帮助我们方便的找到一些自动化配置为什么没有生效的具体原因。
/actuator/configprops GET 查看注入带有@ConfigurationProperties类的properties配置的属性和值,prefix代表前缀
/actuator/env (常用) GET 该端点与/configprops不同,它用来获取应用所有可用的环境属性报告。包括:环境变量、JVM属性、应用的配置配置、命令行中的参数(但是会自动过滤掉带有key、password、secret等关键字的properties的值,保护信息安全)
/actuator/flyway GET 显示已应用的所有Flyway数据库迁移
/actuator/health (常用) GET 查看当前SpringBoot运行的健康指标,值由HealthIndicator的实现类提供(所以可以自定义一些健康指标信息)
/actuator/heapdump GET 会自动生成一个JVM的堆文件
/actuator/info GET 展示了关于应用的一般信息,这些信息从编译文件比如META-INF/build-info.properties或者Git文件比如git.properties或者任何环境的property中获取。
/actuator/mappings GET 查看全部的endpoint(包含 Actuator 的),即路径列表
/actuator/metrics(常用) GET 查看有哪些指标可以看(ex: jvm.memory.max、system.cpu.usage),使用/actuator/metrics/{metric.name}可以查看各指标的详细信息
/actuator/scheduledtasks GET 查看定時任务的信息
/actuator/shutdown POST 唯一一个需要POST请求的endpoint,使应用程序正常关闭

更多详细的EndPoint可以查看:Spring Boot Actuator [监控与管理]

集成 Actuator

创建示例项目

我们先创建一个demo应用,可以通过Spring Initializr创建:
spring-boot-starter-actuator,# SpringBoot,spring boot,java,后端

引入依赖

Maven

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

Gradle

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
}

端点配置

默认暴露端点

我们可以通过以下配置,来配置通过JMX 和 HTTP 暴露的端点。

Property Default
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include info, health

考虑到安全因素,Actuator默认只开放了/actuator/health/actuator/info这两个endpoint,如果要开放其他endpoint,需要额外在application.properties中进行设置。

暴露配置

① 打开所有的监控点

management:
  endpoints:
    enabled-by-default: true #暴露所有端点信息
    web:
      exposure:
        include: '*'  #以web方式暴露(不包含shutdown)

② 开放指定的endpoint

management:
  endpoints:
    enabled-by-default: true #暴露所有端点信息
    web:
      exposure:
        include: beans,mappings  # 如果指定多个端点,用","分开

③ 关闭指定的endpoint

management:
  endpoints:
    web:
      exposure:
        exclude: beans# 如果指定多个端点,用","分开
        include: *

exclude通常会跟include一起用,就是先include了全部,然后再exclude/actuator/beans这个endpoint。
④ 开放shutdown,需额外配置

management:
  endpoints:
    shutdown:
      enabled: true

路径配置

① 默认情况下所有端点都暴露在/actuator路径下,也可以改变/actuator的路径,可以自定义:

management:
  endpoints:
    web:
      base-path: /monitor

设置完重启后,原本內建的/actuator/xxx路径,都会变成/monitor/xxx,可以用来防止被其他人猜到。
② 支持单个endpoint路径定义,比如将health修改成healthcheck

management:
  endpoints:
    web:
      path-mapping:
        health: healthcheck

管理端口调整

management:
  server:
    port: 11011

自定义端口,默认跟server.port一样,可以防止被其他人猜到

端点响应缓存

对于一些不带参数的端点请求会自动进行缓存,我们可以通过如下方式配置缓存时间,下面配置表示 beans端点的缓存时间为100s

management:
  endpoint:
    beans:
      cache:
        time-to-live: 100s

端点保护

如果开启了Actuator默认不开放的endpoints,建议一定要加上Spring Security用于endpoint保护,避免重要信息泄露,必须防止未经授权的外部访问。

这里我们使用Spring Security保护,首先添加相关依赖:

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

添加之后,我们需要定义安全校验规则,来覆盖Spring Security的默认配置。
这里给出两个版本的模板配置:

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.boot.actuate.context.ShutdownEndpoint;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {

    /*
     * version1:
     * 1. 限制 '/shutdown'端点的访问,只允许ACTUATOR访问
     * 2. 允许外部访问其他的端点
     * 3. 允许外部访问静态资源
     * 4. 允许外部访问 '/'
     * 5. 其他的访问需要被校验
     * version2:
     * 1. 限制所有端点的访问,只允许ACTUATOR访问
     * 2. 允许外部访问静态资源
     * 3. 允许外部访问 '/'
     * 4. 其他的访问需要被校验
     */

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // version1
//        http
//                .authorizeRequests()
//                    .requestMatchers(EndpointRequest.to(ShutdownEndpoint.class))
//                        .hasRole("ADMIN")
//                .requestMatchers(EndpointRequest.toAnyEndpoint())
//                    .permitAll()
//                .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
//                    .permitAll()
//                .antMatchers("/")
//                    .permitAll()
//                .antMatchers("/**")
//                    .authenticated()
//                .and()
//                .httpBasic();

        // version2
        http
                .authorizeRequests()
                .requestMatchers(EndpointRequest.toAnyEndpoint())
                    .hasRole("ADMIN")
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
                    .permitAll()
                .antMatchers("/")
                    .permitAll()
                .antMatchers("/**")
                    .authenticated()
                .and()
                .httpBasic();
    }
}

application.properties的相关配置如下:

# Spring Security Default user name and password
spring.security.user.name=actuator
spring.security.user.password=actuator
spring.security.user.roles=ADMIN

我们使用浏览器访问http://localhost:8080/actuator/health端点接口,会先弹出个登录框,只有登录后才能访问。

定制Endpoint

定制化health信息

① 配置Health显示情况,详细界面显示配置

management:
    health:
      enabled: true
      show-details: always #总是显示详细信息。可显示每个模块的状态信息

② 定制Health Endpoint
添加信息:
1)继承 AbstractHealthIndicator 类
2)定义参数 Health.Builder builder
3)向参数 builder 中添加状态及相关信息

@Component
//类名截去HealthIndicator后,为显示时的组件名
public class MyComHealthIndicator extends AbstractHealthIndicator {
 
    //真实的检查方法
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        // 获取链接进行测试
        Map<String, Object> map = new HashMap<>();
 
        //检查完成
        if (1 == 1) {
            //健康
            builder.status(Status.UP);
            map.put("count", 1);
        } else {
            //不健康
            builder.status(Status.OUT_OF_SERVICE);
            map.put("err", "连接超时");
            map.put("time", 3000);
        }
 
        builder.withDetail("code", 100)
               .withDetails(map);
    }
}

定制info信息

编写InfoContributor
1)继承 public class ExampleInfoContributor implements InfoContributor
2)定义 Info.Builder builder 参数
3)向builder中添加参数信息

@Component
public class ExampleInfoContributor implements InfoContributor {
 
    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("msg", "你好")
               .withDetail("hello", "uclass")
               .withDetails(Collections.singletonMap("key", "value"));
 
    }
}

定制Metrics信息

① 利用service层构造器,在注册中心中添加cityService.saveCity.count指标

@Service
public class CityServiceImpl implements CityServices {
 
    @Autowired
    CityMapper cityMapper;
 
    Counter counter;
 
    //利用构造器,在注册中心中添加cityService.saveCity.count指标
    public CityServiceImpl(MeterRegistry meterRegistry) {
        //指标中心注册新的指标项
        counter = meterRegistry.counter("cityService.saveCity.count");
    }
 
    public void saveCity(City city) {
        counter.increment();
        cityMapper.insertCity(city);
    }
}

② 在配置类中新建配置,添加指标

@ServletComponentScan(basePackages = "com.uclass.thymeleaf")
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
    @Bean
    MeterBinder queueSize(Queue queue) {
        return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
    }
}
    //利用构造器,在注册中心中添加cityService.saveCity.count指标
    public CityServiceImpl(MeterRegistry meterRegistry) {
        //指标中心注册新的指标项
        counter = meterRegistry.counter("cityService.saveCity.count");
    }

自定义新端点

利用 @Endpoint(id = “myservice”) 注解

  • id属性代表新增的端点名称
  • 利用@ReadOperation、@WritOperation注解,在端点中添加信息
@Component
@Endpoint(id = "myservice")
public class MyServiceEndPoint {
 
    @ReadOperation
    public Map getDockerInfo () {
        //端点的读操作
        return Collections.singletonMap("dockerInfo", "docker start...");
    }
 
    @WriteOperation
    private void restartDocker(){
        System.out.println("docker restarted....");
    }
}

参考资料

SpringBoot 指标监控——Actuator
Spring boot——Actuator 详解文章来源地址https://www.toymoban.com/news/detail-732879.html

到了这里,关于Spring Boot Actuator详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 关于Spring Boot Actuator漏洞补救方案

    在浏览器中范围于http://192.168.0.119:81/dev-api/actuator(http://IP:端口/actuator),如下图 几个漏洞属于配置不当引起路由暴露。 1.读取用户的认证字段获取敏感信息 可以直接尝试访问网站目录下的/trace 路径,读取用户认证字段信息,比如在trace 路径下,会有用户的敏感信息,可能

    2024年02月04日
    浏览(30)
  • Spring Boot2.xx开启监控 Actuator

                            docker实战(一):centos7 yum安装docker docker实战(二):基础命令篇 docker实战(三):docker网络模式(超详细) docker实战(四):docker架构原理 docker实战(五):docker镜像及仓库配置 docker实战(六):docker 网络及数据卷设置 docker实战(七):docker 性质及版本选择 认知升维: 道、法

    2024年02月14日
    浏览(35)
  • 如何解决 Spring Boot Actuator 的未授权访问漏洞

    Spring Boot Actuator  的作用是提供了一组管理和监控端点,允许你查看应用程序的运行时信息,例如健康状态、应用程序信息、性能指标等。这些端点对于开发、 测试  和运维团队来说都非常有用,可以帮助快速诊断问题、监控应用程序的性能,并采取必要的措施来维护和管理

    2024年02月07日
    浏览(25)
  • 如何在Spring Boot中禁用Actuator端点安全性?

    在Spring Boot中,禁用Actuator端点的安全性可以通过配置来实现。Actuator端点是Spring Boot应用程序的管理和监控端点,它们默认受到Spring Security的保护。如果希望完全禁用Actuator端点的安全性,我们可以按照以下步骤进行操作: 确保我们的pom.xml文件中包含了Spring Boot Starter依赖项。

    2024年02月04日
    浏览(27)
  • java Spring Boot 2 /actuator/health 返回 HTTP 404

    spring-boot-starter-actuator官方文档 Spring Boot 包含许多附加功能,可帮助您在将应用程序投入生产时监控和管理应用程序。您可以选择使用 HTTP 端点或 JMX 来管理和监控您的应用程序。审核、运行状况和指标收集也可以自动应用于您的应用程序。 该spring-boot-actuator模块提供了 Spri

    2024年01月18日
    浏览(32)
  • spring boot actuator 未授权访问(env泄露redis密码)

    Actuator Actuator是Spring Boot提供的服务监控和管理中间件,默认配置会出现接口未授权访问,部分接口会泄露网站流量信息和内存信息等,使用Jolokia库特性甚至可以远程执行任意代码,获取服务器权限。 渗透过程 访问浏览器访问actuator 访问env获取到敏感信息redis服务器地址以及

    2024年02月11日
    浏览(27)
  • Spring Boot 3.x 系列【34】Actuator入门案例及端点配置

    有道无术,术尚可求,有术无道,止于术。 本系列Spring Boot版本3.0.5 源码地址:https://gitee.com/pearl-organization/study-spring-boot3 在生产环境很有必要对应用进行 监控、追踪、审计、控制 ,假如数据库突然挂了,直到客户打电话骂娘时~ 我们才知道这回事,就很尴尬了🤣🤣🤣 Sp

    2024年02月05日
    浏览(31)
  • Spring Boot应用集成Actuator组件以后怎么自定义端点暴露信息

    在平时业务开发中,我们往往会在spring Boot项目中集成Actuator组件进行系统监控,虽然Actuator组件暴露的端点信息已经足够丰富了,但是特殊场景下,我们也需要自己暴露端点信息,此时应该怎么操作呢? 1. 创建一个spring Boot项目,导入相关依赖 3. 暴露端点 注意: 自定义的端

    2024年02月21日
    浏览(29)
  • 【Spring Boot系列】- Spring Boot事务应用详解

    事务(Transaction)是数据库操作最基本单元,逻辑上一组操作,要么都成功。如果有一个操作失败。则事务操作都失败(回滚(Rollback))。 事务的四个特性(ACID): 1. 原子性(Atomicity) 一个事务(Transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间

    2024年02月08日
    浏览(38)
  • Spring Boot banner详解

    Spring Boot 3.x系列文章 Spring Boot 2.7.8 中文参考指南(一) Spring Boot 2.7.8 中文参考指南(二)-Web Spring Boot 源码阅读初始化环境搭建 Spring Boot 框架整体启动流程详解 Spring Boot 系统初始化器详解 Spring Boot 监听器详解 Spring Boot banner详解 Spring Boot 默认打印的banner是这样的,Java工程师看都

    2024年02月08日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包