如何在Spring Boot Actuator中启用健康



我必须检查我的服务/应用程序是否工作。

我添加了依赖

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

并尝试将CCD_ 1添加到CCD_。

我试图转到http://localhost:8080/actuator/healthhttp://localhost:8080/health,但返回404错误。

您可以在application.yaml上尝试此代码。这适用于Spring boot 2.6.7。

management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health

如您所见,上有404

http://localhost:8080/actuator/health

http://localhost:8080/health

原因并不是因为启用了安全性,如果启用了安全,您将获得401或403。您可能需要在application.yaml文件中公开执行器端点。

类似这样的东西:

management:
endpoints:
web:
exposure:
include: "health,info"

如果您启用了安全性,则需要编写自己的SecurityFilterChain实现,在该实现中,您将禁用所有执行器端点上的安全性,或者在您的情况下,仅禁用application.yaml文件中公开的端点的安全性。

示例:

@Configuration
class ActuatorSecurityAutoConfiguration {
@Bean
SecurityFilterChain 
surpassingActuatorSecurityFilterChain(HttpSecurity 
httpSecurity) throws Exception {
return httpSecurity
.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests()
.anyRequest()
.permitAll()
.and().build();
}
}

默认情况下,Spring引导为所有执行器端点启用安全性

您可以使用以下属性禁用该功能

management.security.enabled=false  

此后,尝试运行应用程序并到达终点

http://localhost:8080/actuator

最新更新