woshikid / blog

Apache License 2.0
8 stars 1 forks source link

Spring Boot Actuator学习笔记 #165

Open woshikid opened 3 years ago

woshikid commented 3 years ago

POM

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

默认配置

spring.jmx.enabled: false
management:
  endpoints:
    jmx:
      exposure.include: "*"
    web:
      base-path: "/actuator"
      exposure.include: "health"
  endpoint:
    shutdown.enabled: false
    health.show-details: never # 设置为always显示components与详细信息
  server:
    port: 8080 # -1关闭http服务

自定义健康指标

@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {

    @Override
    protected void doHealthCheck(Builder builder) throws Exception {
        builder.up().withDetail("total", "100").withDetail("free", "80");
    }

}

设置响应码

management:
  endpoint:
    health:
      status:
        http-mapping:
          down: 503
          fatal: 503
          out-of-service: 503

修改日志级别

curl -H "Content-Type: application/json" -d "{\"configuredLevel\":\"DEBUG\"}" http://127.0.0.1:8080/actuator/loggers/ROOT

应用信息

# http://127.0.0.1:8080/actuator/info
#management.info.env.enabled: true # Spring Boot 2.6+
info:
  app:
    name: @project.name@ # 使用@获取Maven属性
    version: @project.version@
    java:
      version: @java.version@

自定义信息

@Component
public class MyInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("example", Map.of("key", "value"));
    }

}

生成META-INF/build-info.properties

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
    </executions>
</plugin>

生成git.properties

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
</plugin>
management.info.git.mode: "full"

自定义端点

@Component
@Endpoint(id = "custom") // @RestControllerEndpoint(id = "custom")
public class CustomEndpoint {

    @ReadOperation // @GetMapping("/")
    public Map<String, Object> getData() {
        return Map.of("total", "100", "free", "80");
    }

    @WriteOperation // @PostMapping("/") // 仅支持?传参
    public void setData(String key) {
        // 参数优先级
        // 1.http://127.0.0.1:8080/actuator/custom?key=value
        // 2.Post Body {"key":"value"}
        // 3.http://127.0.0.1:8080/actuator/custom/value // 需@Selector注解
    }

    @DeleteOperation // @DeleteMapping("/")
    public String deleteData(@Selector String key) {
        return key;
    }

}

端点缓存

management.endpoint.custom.cache.time-to-live: "10s" # 默认不缓存

CORS支持

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

开启httptrace

@Bean
@ConditionalOnMissingBean(HttpTraceRepository.class)
public HttpTraceRepository httpTraceRepository() {
    return new InMemoryHttpTraceRepository();
}