spring Boot Actuator使用

spring Boot Actuator使用

Spring Boot 的 Actuator 提供了很多生产级的特性,比如监控和度量Spring Boot 应用程序。

官网参考文档Spring Boot Actuator: Production-ready Features

maven依赖

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

暴露的endpoints

Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information.

根据官网的提示,我们可以得知Actuator endpoints让我们能跟我们的application进行监控和互动。Spring Boot Actuator本身已经提供的若干个定义好的endpoints。更加这些endpoints,我们对我们的application进行监控。如:health endpoint (监控application的健康信息)

Most applications choose exposure via HTTP, where the ID of the endpoint along with a prefix of /actuator is mapped to a URL. For example, by default, the health endpoint is mapped to /actuator/health.

根据官网的提示,这些endpoint都是以HTTP进行暴露的,并且是以/actuator为前缀的URL,如:/actuator/health是健康监控HTTP端口。

表格链接:https://www.jianshu.com/p/d5943e303a1f

Endpoint IDDescription
auditevents显示应用暴露的审计事件 (比如认证进入、订单失败)
info显示应用的基本信息
health显示应用的健康状态
metrics显示应用多样的度量信息
loggers显示和修改配置的loggers
logfile返回log file中的内容(如果logging.file或者logging.path被设置)
httptrace显示HTTP足迹,最近100个HTTP request/repsponse
env显示当前的环境特性
flyway显示数据库迁移路径的详细信息
liquidbase显示Liquibase 数据库迁移的纤细信息
shutdown让你逐步关闭应用
mappings显示所有的@RequestMapping路径
scheduledtasks显示应用中的调度任务
threaddump执行一个线程dump
heapdump返回一个GZip压缩的JVM堆dump

暴露方式

根据官网的提示这些端口都有默认的暴露方式,分别是JMX和Web。这个是官网的表格:

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

如果想更改这些默认的暴露方式,可以从配置文件中修改如下四个配置属性:

management.endpoints.jmx.exposure.exclude

management.endpoints.jmx.exposure.include=*   #默认

management.endpoints.web.exposure.exclude

management.endpoints.web.exposure.include=health	#默认

保护 HTTP endpoin

You should take care to secure HTTP endpoints in the same way that you would any other sensitive URL. If Spring Security is present, endpoints are secured by default using Spring Security’s content-negotiation strategy. If you wish to configure custom security for HTTP endpoints, for example, only allow users with a certain role to access them, Spring Boot provides some convenient RequestMatcher objects that can be used in combination with Spring Security.

关于HTTP endpoint的保护,如果你有整合Spring Security,那么endpoints会使用Spring Security默认的安全策略,你也可以自定义一个安全策略。

自定义的SecurityFilterChain(安全过滤链),可以帮助我们对Endpoint的请求进行过滤,如下面安全过滤链中,对Endpoint进行的请求都必须有ENDPOINT_ADMIN role。

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
        http.httpBasic();
        return http.build();
    }
}

开启CORS支持

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

/actuator/health

Spring Boot Actuator有很多预定义的健康指示器,下面列出几个是:

DataSourceHealthIndicator

DiskSpaceHealthIndicator

MongoHealthIndicator

RedisHealthIndicator

CassandraHealthIndicator
请添加图片描述

官网API地址:Overview (Spring Boot 2.5.3 API)

除了上面的还有其他支持的指示器,可以去官网API文档看,我们使用也十分简单,Spring Boot Actuator已经把这些装配到上下文中了,默认是开启监控的。如果想关闭某个健康检测,在配置文件进行配置即可

management.health.mongo.enabled=false

如果需要详细的健康检测数据,只要在配置文件中加

management.endpoint.health.show-details=always

还有两个Kubernetes常用的健康检测端口:

请添加图片描述

/actuator/metrics

/actuator/metrics是度量或者检测端口,我们可以通过一些方式检测指定的metrics

http://localhost:8080/actuator/metrics/{MetricName}

可以通过端口/actuator来获取MetricName

{
    "names": [
        "jvm.memory.max",
        "http.server.requests",
        "process.files.max",
        ...
    ]
}

/actuator/loggers

该端口用于获取loggers的等级或者进行修改

http://localhost:8080/actuator/loggers/{name}
//如:
http://localhost:8080/actuator/loggers/root

/actuator/bus-refres

这个端口一般是在spring cloud config中使用,通过这个端口可以刷新配置文件信息。

curl -v -X POST “http://localhost:8081/actuator/bus-refresh”
curl -X POST “http://localhost:8081/actuator/bus-refresh”

/actuator/bus-refres

这个端口一般是在spring cloud config中使用,通过这个端口可以刷新配置文件信息。

curl -v -X POST “http://localhost:8081/actuator/bus-refresh”
curl -X POST “http://localhost:8081/actuator/bus-refresh”

posted @   鸭梨的药丸哥  阅读(7)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示