Spring Boot Actuator

简介: Spring Boot Actuator

文章目录



Spring Boot Actuator


Spring Boot Actuator 在Spring Boot第一个版本发布的时候就有了,它为Spring Boot提供了一系列产品级的特性:监控应用程序,收集元数据,运行情况或者数据库状态等。


使用Spring Boot Actuator我们可以直接使用这些特性而不需要自己去实现,它是用HTTP或者JMX来和外界交互。


开始使用Spring Boot Actuator


要想使用Spring Boot Actuator,需要添加如下依赖:


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


开始使用Actuator


配好上面的依赖之后,我们使用下面的主程序入口就可以使用Actuator了:


@SpringBootApplication
public class ActuatorApp {
    public static void main(String[] args) {
        SpringApplication.run(ActuatorApp.class, args);
    }
}


启动应用程序,访问http://localhost:8080/actuator:


{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}


我们可以看到actuator默认开启了两个入口:/health和/info。


如果我们在配置文件里面这样配置,则可以开启actuator所有的入口:


management.endpoints.web.exposure.include=*


重启应用程序,再次访问http://localhost:8080/actuator:


{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}","templated":true},"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}


我们可以看到actuator暴露的所有入口。


Health Indicators


Health入口是用来监控组件的状态的,通过上面的入口,我们可以看到Health的入口如下:


"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},


有两个入口,一个是总体的health,一个是具体的health-path。


我们访问一下http://localhost:8080/actuator/health:


{"status":"UP"}


上面的结果实际上是隐藏了具体的信息,我们可以通过设置


management.endpoint.health.show-details=ALWAYS


来开启详情,开启之后访问如下:


{"status":"UP","components":{"db":{"status":"UP","details":{"database":"H2","result":1,"validationQuery":"SELECT 1"}},"diskSpace":{"status":"UP","details":{"total":250685575168,"free":12428898304,"threshold":10485760}},"ping":{"status":"UP"}}}


其中的components就是health-path,我们可以访问具体的某一个components如http://localhost:8080/actuator/health/db:


{"status":"UP","details":{"database":"H2","result":1,"validationQuery":"SELECT 1"}}


就可以看到具体某一个component的信息。


这些Health components的信息都是收集实现了HealthIndicator接口的bean来的。


我们看下怎么自定义HealthIndicator:


@Component
public class CustHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down()
                    .withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }
    public int check() {
        // Our logic to check health
        return 0;
    }
}


再次查看http://localhost:8080/actuator/health, 我们会发现多了一个Cust的组件:


"components":{"cust":{"status":"UP"} }


在Spring Boot 2.X之后,Spring添加了React的支持,我们可以添加ReactiveHealthIndicator如下:


@Component
public class DownstreamServiceHealthIndicator implements ReactiveHealthIndicator {
    @Override
    public Mono<Health> health() {
        return checkDownstreamServiceHealth().onErrorResume(
                ex -> Mono.just(new Health.Builder().down(ex).build())
        );
    }
    private Mono<Health> checkDownstreamServiceHealth() {
        // we could use WebClient to check health reactively
        return Mono.just(new Health.Builder().up().build());
    }
}


再次查看http://localhost:8080/actuator/health,可以看到又多了一个组件:


"downstreamService":{"status":"UP"}


/info 入口


info显示了App的大概信息,默认情况下是空的。我们可以这样自定义:


info.app.name=Spring Sample Application
info.app.description=This is my first spring boot application
info.app.version=1.0.0


查看:http://localhost:8080/actuator/info


{"app":{"name":"Spring Sample Application","description":"This is my first spring boot application","version":"1.0.0"}}


/metrics入口


/metrics提供了JVM和操作系统的一些信息,我们看下metrics的目录,访问:http://localhost:8080/actuator/metrics:


{"names":["jvm.memory.max","jvm.threads.states","jdbc.connections.active","process.files.max","jvm.gc.memory.promoted","system.load.average.1m","jvm.memory.used","jvm.gc.max.data.size","jdbc.connections.max","jdbc.connections.min","jvm.gc.pause","jvm.memory.committed","system.cpu.count","logback.events","http.server.requests","jvm.buffer.memory.used","tomcat.sessions.created","jvm.threads.daemon","system.cpu.usage","jvm.gc.memory.allocated","hikaricp.connections.idle","hikaricp.connections.pending","jdbc.connections.idle","tomcat.sessions.expired","hikaricp.connections","jvm.threads.live","jvm.threads.peak","hikaricp.connections.active","hikaricp.connections.creation","process.uptime","tomcat.sessions.rejected","process.cpu.usage","jvm.classes.loaded","hikaricp.connections.max","hikaricp.connections.min","jvm.classes.unloaded","tomcat.sessions.active.current","tomcat.sessions.alive.max","jvm.gc.live.data.size","hikaricp.connections.usage","hikaricp.connections.timeout","process.files.open","jvm.buffer.count","jvm.buffer.total.capacity","tomcat.sessions.active.max","hikaricp.connections.acquire","process.start.time"]}


访问其中具体的某一个组件如下

http://localhost:8080/actuator/metrics/jvm.memory.max:


{"name":"jvm.memory.max","description":"The maximum amount of memory in bytes that can be used for memory management","baseUnit":"bytes","measurements":[{"statistic":"VALUE","value":3.456106495E9}],"availableTags":[{"tag":"area","values":["heap","nonheap"]},{"tag":"id","values":["Compressed Class Space","PS Survivor Space","PS Old Gen","Metaspace","PS Eden Space","Code Cache"]}]}


Spring Boot 2.X 的metrics是通过Micrometer来实现的,Spring Boot会自动注册MeterRegistry。 有关Micrometer和Spring Boot的结合使用我们会在后面的文章中详细讲解。


自定义Endpoint


Spring Boot的Endpoint也是可以自定义的:


@Component
@Endpoint(id = "features")
public class FeaturesEndpoint {
    private Map<String, String> features = new ConcurrentHashMap<>();
    @ReadOperation
    public Map<String, String> features() {
        return features;
    }
    @ReadOperation
    public String feature(@Selector String name) {
        return features.get(name);
    }
    @WriteOperation
    public void configureFeature(@Selector String name, String value) {
        features.put(name, value);
    }
    @DeleteOperation
    public void deleteFeature(@Selector String name) {
        features.remove(name);
    }
}


访问http://localhost:8080/actuator/, 我们会发现多了一个入口:

http://localhost:8080/actuator/features/ 。

上面的代码中@ReadOperation对应的是GET, @WriteOperation对应的是PUT,

@DeleteOperation对应的是DELETE。


@Selector后面对应的是路径参数, 比如我们可以这样调用configureFeature方法:


POST /actuator/features/abc HTTP/1.1
Host: localhost:8080
Content-Type: application/json
User-Agent: PostmanRuntime/7.18.0
Accept: */*
Cache-Control: no-cache
Postman-Token: dbb46150-9652-4a4a-95cb-3a68c9aa8544,8a033af4-c199-4232-953b-d22dad78c804
Host: localhost:8080
Accept-Encoding: gzip, deflate
Content-Length: 15
Connection: keep-alive
cache-control: no-cache
{"value":true}


注意,这里的请求BODY是以JSON形式提供的:


{"value":true}


请求URL:/actuator/features/abc 中的abc就是@Selector 中的 name。

我们再看一下GET请求:


http://localhost:8080/actuator/features/


{"abc":"true"}


这个就是我们之前PUT进去的值。


扩展现有的Endpoints


我们可以使用@EndpointExtension (@EndpointWebExtension或者@EndpointJmxExtension)来实现对现有EndPoint的扩展:


@Component
@EndpointWebExtension(endpoint = InfoEndpoint.class)
public class InfoWebEndpointExtension {
    private InfoEndpoint delegate;
    // standard constructor
    @ReadOperation
    public WebEndpointResponse<Map> info() {
        Map<String, Object> info = this.delegate.info();
        Integer status = getStatus(info);
        return new WebEndpointResponse<>(info, status);
    }
    private Integer getStatus(Map<String, Object> info) {
        // return 5xx if this is a snapshot
        return 200;
    }
}
相关文章
|
XML Java 应用服务中间件
Spring Boot 两种部署到服务器的方式
本文介绍了Spring Boot项目的两种部署方式:jar包和war包。Jar包方式使用内置Tomcat,只需配置JDK 1.8及以上环境,通过`nohup java -jar`命令后台运行,并开放服务器端口即可访问。War包则需将项目打包后放入外部Tomcat的webapps目录,修改启动类继承`SpringBootServletInitializer`并调整pom.xml中的打包类型为war,最后启动Tomcat访问应用。两者各有优劣,jar包更简单便捷,而war包适合传统部署场景。需要注意的是,war包部署时,内置Tomcat的端口配置不会生效。
3056 17
Spring Boot 两种部署到服务器的方式
|
Java 数据库 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
572 0
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
817 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
监控 安全 Java
Spring Boot 中的 Actuator 是什么?
Spring Boot 中的 Actuator 是什么?
3005 6
|
安全 Java 测试技术
如何在 Spring Boot 中禁用 Actuator 端点安全?
如何在 Spring Boot 中禁用 Actuator 端点安全?
3267 1
|
存储 运维 安全
Spring运维之boot项目多环境(yaml 多文件 proerties)及分组管理与开发控制
通过以上措施,可以保证Spring Boot项目的配置管理在专业水准上,并且易于维护和管理,符合搜索引擎收录标准。
1057 2
|
缓存 NoSQL Java
Springboot自定义注解+aop实现redis自动清除缓存功能
通过上述步骤,我们不仅实现了一个高度灵活的缓存管理机制,还保证了代码的整洁与可维护性。自定义注解与AOP的结合,让缓存清除逻辑与业务逻辑分离,便于未来的扩展和修改。这种设计模式非常适合需要频繁更新缓存的应用场景,大大提高了开发效率和系统的响应速度。
686 2
|
Java Spring 监控
Spring Boot Actuator:守护你的应用心跳,让监控变得触手可及!
【8月更文挑战第31天】Spring Boot Actuator 是 Spring Boot 框架的核心模块之一,提供了生产就绪的特性,用于监控和管理 Spring Boot 应用程序。通过 Actuator,开发者可以轻松访问应用内部状态、执行健康检查、收集度量指标等。启用 Actuator 需在 `pom.xml` 中添加 `spring-boot-starter-actuator` 依赖,并通过配置文件调整端点暴露和安全性。Actuator 还支持与外部监控工具(如 Prometheus)集成,实现全面的应用性能监控。正确配置 Actuator 可显著提升应用的稳定性和安全性。
868 1
|
监控 数据可视化 Java
springBoot:actuator&admin 图形可视化&spring 打包 (七)
本文介绍了Spring Boot Actuator及其图形化管理界面Spring Boot Admin的使用方法,包括依赖导入、服务端与客户端配置、以及如何打包为JAR和WAR文件并部署。通过这些步骤,可以实现应用的监控和管理功能。
739 0
|
监控 NoSQL Java
Spring Boot Actuator 使用和常用配置
Spring Boot Actuator 使用和常用配置
2032 5