springBoot-Actuator

本文深入探讨SpringBoot Actuator模块,介绍其作用、配置及如何利用内置端点监控和管理应用。涵盖端点启用、暴露、缓存配置、路径设置及跨域支持,同时提供自定义端点的实现方法。

Actuator(执行器)

执行器是一个制造业术语,指的是用于移动或控制东西的一个机械装置,一个很小的改变就能让执行器产生大量的运动。
An actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change.

Spring Boot Actuator可以监控和管理Spring Boot应用,比如健康检查、审计、统计和HTTP追踪等。所有的这些特性可以通过JMX或者HTTP endpoints来获得。

JMX:是Java Management Extensions(Java管理扩展)的缩写,是一个为应用程序植入管理功能的框架;
HTTP Endpoints:请求端点形式获取对应的信息,springboot中内置对应的端点,可用于监控应用及与应用进行交互;

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

引用Actuator

springBoot-Actuator模块需要在maven模块中添加对应的spring-boot-starter-actuator依赖:

添加maven依赖

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

端点(Endpoints)

springBoot中有多内置的端点,每个端点可以启用或禁用;
访问端点的请求映射http://localhost:8080/actuator/beans(端点)
springBoot端点

ID描述默认启用
auditevents显示当前应用程序的审计事件信息Yes
info显示应用的基本信息Yes
beans显示一个应用中所有Spring Beans的完整列表Yes
conditions显示配置类和自动配置类(configuration and auto-configuration classes)的状态及它们被应用或未被应用的原因Yes
configprops显示一个所有@ConfigurationProperties的集合列表Yes
env显示来自Spring的 ConfigurableEnvironment的属性Yes
flyway显示已应用的所有Flyway数据库迁移Yes
health显示应用的健康信息(当使用一个未认证连接访问时显示一个简单的’status’,使用认证连接访问则显示全部信息详情)Yes
liquibase展示任何Liquibase数据库迁移路径Yes
metrics展示当前应用的metrics信息Yes
mappings显示一个所有@RequestMapping路径的集合列表Yes
scheduledtasks显示应用程序中的计划任务Yes
sessions允许从Spring会话支持的会话存储中检索和删除(retrieval and deletion) 用户会话。使用Spring Session对反应性Web应用程序的支持时不可用。Yes
shutdown允许应用以优雅的方式关闭(默认情况下不启用)No
threaddump执行一个线程dumpYes

端点的启用和暴露

启用

  1. 端点默认情况下,除shutdown以外的所有端点均已启用;
  2. 配置单个端点的启用,请使用management.endpoint..enabled属性;
  3. 全局默认开关,management.endpoints.enabled-by-default修改端点全局默认配置;

配置例子

--开启shutdown端点
management.endpoint.shutdown.enabled=true
--关闭全区默认开启端点,启用info端点
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true

暴露
由于端点可能包含敏感信息,因此应谨慎考虑何时公开它们。下表显示了内置端点的默认暴露

IDJMXWeb
auditeventsYesNo
beansYesNo
cachesYesNo
conditionsYesNo
configpropsYesNo
envYesNo
flywayYesNo
healthYesYes
heapdumpN/ANo
httptraceYesNo
infoYesYes
integrationgraphYesNo
jolokiaN/ANo
logfileN/ANo
loggersYesNo
liquibaseYesNo
metricsYesNo
mappingsYesNo
prometheusN/ANo
scheduledtasksYes
sessionsYesNo
shutdownYesNo
threaddumpYesNo

要更改公开哪些端点,使用include和exclude属性:

PropertyDefault
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include*
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.includeinfo, health
  • include属性列出了公开的端点的ID;
  • exclude属性列出了不应该公开的端点的ID;
  • exclude属性优先于include属性,包含和排除属性都可以使用端点ID进行配置;

注意
exclude属性优先于include属性,指同一端点ID,同时出现在include属性表和exclude属性表里,exclude属性优先于include属性,即此端点没有暴露

配置例子
http://localhost:8080/actuator

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

配置端点的缓存

端点自动缓存对不带任何参数的读取操作的响应。要配置端点缓存响应的时间,请使用其cache.time-to-live属性。以下示例将beans端点的缓存的生存时间设置为10秒:

management.endpoint.beans.cache.time-to-live=10s

端点的路径

  • 默认情况,端点通过使用端点的ID在/actuator路径下的HTTP上公开;
  • 使用management.endpoints.web.path-mapping属性将端点映射到其他路径;
  • 使用management.endpoints.web.base-path属性将基本路径可以修改;
#映射health到healthcheck
management.endpoints.web.path-mapping.health=healthcheck
#修改基本路径
management.endpoints.web.base-path=/winning

跨域支持

默认情况下,CORS支持是禁用的,并且仅management.endpoints.web.cors.allowed-origins在设置属性后才启用。以下配置允许GET和POST从example.com域调用:

management.endpoints.web.cors.allowed-origins=https://example.com
management.endpoints.web.cors.allowed-methods=GET,POST

实现自定义端点

注解形式

  • 通过在Bean上注解@Endpoint、 @WebEndpoint或 @EndpointWebExtension会将Bean通过HTTP暴露为端点
  • @ReadOperation:GET查询请求;
  • @WriteOperation:POST保存请求;
  • @DeleteOperation:DELETE删除请求;
package com.winning.acutuator.index.endpoint.autoEndpoint;

import org.springframework.boot.actuate.endpoint.annotation.*;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Endpoint(id = "autoEndpoint")
public class autoEndpoint {
    public static Map<String, Boolean> status = new HashMap<>();

    @ReadOperation
    public Map<String, Boolean> findAll() {
        return status;
    }
    @ReadOperation
    public Boolean findOne(@Selector String id) {
        return status.get(id);
    }
    @WriteOperation
    public String save(@Selector String id) {
        status.put(id, true);
        return "保存成功";
    }
    @DeleteOperation
    public String delete(@Selector String id) {
        status.remove(id);
        return "删除成功";
    }
}

  • get请求获取对应端点
  • post请求插入对应的注解
  • delete请求删除对应注解

常用端点操作

beans
http://localhost:8080/actuator/beans
bean:Bean的名称
scope:Bean的作用域
type:Bean的Java类型
reource:class文件的具体路径
dependencies:依赖的Bean名称

日志
http://localhost:8080/actuator/loggers展示了应用中可配置的loggers的列表和相关的日志等级
获取具体日志信息
http://localhost:8080/actuator/loggers/{name}来展示特定logger的细节
修改日志级别
http://localhost:8080/actuator/loggers/root
POST请求
{ “configuredLevel”: “DEBUG” }

度量metrics
http://localhost:8080/actuator/metrics展示所有可以追踪的所有度量
http://localhost:8080/actuator/metrics/{MetricName}获得每个度量的详细信息

名称代码
请求数量http.server.requests
CPU 数量system.cpu.count
系统 CPU 使用率system.cpu.usage
应用启动时间点process.start.time
应用已运行时间process.uptime
当前应用 CPU 使用率process.cpu.usage
JVM 最大内存jvm.memory.max
JVM 可用内存jvm.memory.committed
JVM 已用内存jvm.memory.used
JVM 缓冲区已用内存jvm.buffer.memory.used
当前缓冲区数量jvm.buffer.count
JVM 守护线程数量jvm.threads.daemon
JVM 当前活跃线程数量jvm.threads.live
JVM 峰值线程数量jvm.threads.peak
JVM 已加载 Class 数量jvm.classes.loaded
JVM 未加载 Class 数量jvm.classes.unloaded
GC 时, 年轻代分配的内存空间jvm.gc.memory.allocated
GC 时, 老年代分配的内存空间jvm.gc.memory.promoted
GC 时, 老年代的最大内存空间jvm.gc.max.data.size
Full GC 时, 老年代的内存空间jvm.gc.live.data.size
系统启动以来GC 次数jvm.gc.pause.count
系统启动以来GC 总耗时jvm.gc.pause.totalTime

基本信息info
info展示了应用的基本信息。通过META-INF/build-info.properties来获得编译信息,通过git.properties来获得Git信息。它同时可以展示任何其他信息,只要这个环境property中含有info 关键字即可
添加信息到properties文件
info.app.name= @project.name@
info.app.description= @project.description@
info.app.version=@project.version@
info.app.encoding:= @project.build.sourceEncoding@
info.app.java.version= @java.version@

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值