How to enable health in Spring Boot Actuator
Asked Answered
H

4

8

I have to check whether my service / app works or not.

I've added dependency

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

and also tried to add management.endpoint.health.show-details: always to application.yml but it didn't help.

I tried to go to http://localhost:8080/actuator/health, http://localhost:8080/health but it returned 404 error.

Hoax answered 10/1, 2022 at 13:7 Comment(0)
G
4

You can try this code on your application.yaml. This is worked for Spring boot 2.6.7.

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: health
Globigerina answered 3/6, 2022 at 14:28 Comment(0)
E
0

By default Spring boot enables security for all actuator endpoints

You can disable that feature using below property

management.security.enabled=false  

Henceforth, try to run the application and hit the endpoint

http://localhost:8080/actuator
Evangelical answered 10/1, 2022 at 13:30 Comment(3)
It didn't help.Hoax
Try removing the version from your dependency and let spring pull it based on your spring boot version and then try !Evangelical
It would help if you posted your whole pom.xml and application.properties files.Jampan
G
0

As you see, you have 404 both on

http://localhost:8080/actuator/health

and

http://localhost:8080/health

Reason for this is not because security is enabled, if security was enabled you will get 401 or 403. You probably need to expose actuator endpoints in application.yaml file.

Something like this:

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

And if you have security enabled, you need to write you own SecurityFilterChain implementation in which you will disable security on all Actuator endpoints, or in your case only on those that you exposed in your application.yaml file.

Example:

   @Configuration
   class ActuatorSecurityAutoConfiguration {

   @Bean
   SecurityFilterChain 
   surpassingActuatorSecurityFilterChain(HttpSecurity 
   httpSecurity) throws Exception {
     return httpSecurity
            .requestMatcher(EndpointRequest.toAnyEndpoint())
            .authorizeRequests()
            .anyRequest()
            .permitAll()
            .and().build();
     }
   }
Guidry answered 10/3, 2022 at 11:40 Comment(0)
V
0

In my case, I had the spring-boot-starter-actuator dependency, but did not have the spring-boot-starter-web one - in a refactor, I thought it would not be needed as my application can run with spring.main.web-application-type: none.

But that's not the case: turns out that if you set the web-application-type property to none and remove the spring-boot-starter-web depdendency (after all, your application logic doesn't need any webserver running), the health endpoints will not be initialized (if you search in the application initialization logs, you will never find anything like

[main] INFO  o.s.b.a.e.web.EndpointLinksResolver - Exposing *X* endpoints beneath base path '/actuator'

where X is the number of endpoints you defined in management.endpoints.web.exposure.include.

This is super annoying, because in my assumption the health endpoints would enable a web server automatically, if needed.

So, my solution was adding:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <version>${spring-boot.version}</version>
</dependency>

back to my pom.xml, and live with the fact that my application runs a webserver only to expose those endpoints.

Vivianaviviane answered 17/9 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.