解析Spring Boot中的Actuator端点

大家好,我是微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们来解析一下Spring Boot中的Actuator端点。Spring Boot Actuator提供了一系列内建的端点,帮助开发者监控和管理Spring Boot应用程序。通过这些端点,可以获取应用的各种运行时信息,极大地方便了开发、运维和故障排查工作。

1. 引入Spring Boot Actuator

首先,我们需要在Spring Boot项目中引入Actuator依赖。在pom.xml文件中添加以下内容:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.

2. 配置Actuator

application.properties文件中进行一些基本配置。可以启用或禁用特定的端点,以及设置端点的访问权限。例如:

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
  • 1.
  • 2.

上述配置表示暴露所有端点,并且在/actuator/health端点中显示详细信息。

3. 常用Actuator端点解析

3.1 /actuator

/actuator端点提供了所有可用端点的列表。通过访问http://localhost:8080/actuator可以查看所有启用的Actuator端点。

3.2 /actuator/health

/actuator/health端点显示应用程序的健康状况。默认情况下,返回的状态是UPDOWN,可以根据需要自定义健康检查。

package cn.juwatech.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        // 自定义健康检查逻辑
        int errorCode = check(); 
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

    private int check() {
        // 模拟健康检查
        return 0;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

3.3 /actuator/info

/actuator/info端点显示应用程序的定制信息。可以在application.properties中添加信息,或者在application.yml中配置。

info.app.name=MyApp
info.app.version=1.0.0
info.company.name=Juwatech
  • 1.
  • 2.
  • 3.

访问http://localhost:8080/actuator/info时,会显示上述配置信息。

3.4 /actuator/metrics

/actuator/metrics端点提供了应用程序的各项指标,如JVM内存使用情况、GC活动、线程信息等。通过访问http://localhost:8080/actuator/metrics可以查看所有可用的指标。

package cn.juwatech.metrics;

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class CustomMetrics {

    @Autowired
    private MeterRegistry meterRegistry;

    @PostConstruct
    public void init() {
        meterRegistry.gauge("custom.metric", Math.random() * 100);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

3.5 /actuator/loggers

/actuator/loggers端点显示并可以动态修改日志记录级别。通过访问http://localhost:8080/actuator/loggers可以查看所有的日志记录器及其级别。

package cn.juwatech.logging;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoggerController {

    @PostMapping("/change-log-level")
    public void changeLogLevel(@RequestParam String loggerName, @RequestParam String level) {
        org.apache.logging.log4j.core.config.Configurator.setLevel(loggerName, org.apache.logging.log4j.Level.valueOf(level));
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

4. 自定义Actuator端点

除了内置的端点,Spring Boot还允许我们创建自定义的Actuator端点。

4.1 创建自定义端点

package cn.juwatech.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;

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

    @ReadOperation
    public String customRead() {
        return "Custom read operation";
    }

    @WriteOperation
    public void customWrite(String data) {
        // 自定义写操作逻辑
        System.out.println("Custom write operation with data: " + data);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

4.2 暴露自定义端点

application.properties中配置暴露自定义端点:

management.endpoints.web.exposure.include=custom
  • 1.

访问http://localhost:8080/actuator/custom可以查看自定义的读操作结果。

5. 安全性配置

为了确保Actuator端点的安全,可以配置Spring Security来保护这些端点。

5.1 引入Spring Security依赖

pom.xml中添加Spring Security依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.

5.2 配置安全规则

package cn.juwatech.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.stereotype.Component;

@Component
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/actuator/**").authenticated()
            .and()
            .httpBasic();
        return http.build();
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

这样配置后,所有的Actuator端点都需要进行身份验证才能访问。

6. 总结

Spring Boot Actuator通过提供一系列内建的监控和管理端点,使开发者能够方便地获取应用的运行时信息,并进行管理和调试。通过自定义端点和安全配置,可以进一步增强Actuator的功能和安全性。