若项目中不想使用spring-boot-starter-actuator依赖,也可以使用以下代码手动配置Actuator:
@Configuration
public class ActuatorConfig {
@Bean
public CommandLineRunner commandLineRunner(MetricsEndpoint metricsEndpoint) {
return (args) -> {
System.out.println(metricsEndpoint.metrics());
};
}
@Bean
public HealthEndpoint healthEndpoint() {
HealthIndicator healthIndicator = () -> Health.status("I <3 Java!").build();
return new HealthEndpoint(healthIndicator);
}
@Bean
public InfoEndpoint infoEndpoint() {
Map infoMap = new HashMap<>();
infoMap.put("name", "My Application");
infoMap.put("version", "1.0");
infoMap.put("description", "Description of my application");
return new InfoEndpoint(infoMap);
}
}
以上代码实现了MetricsEndpoint、HealthEndpoint和InfoEndpoint的手动配置,可以根据需要自行添加其他的Endpoint,如EnvEndpoint等。