3
votes

I believe followed almost all tutorials on the Internet and read many SO answers and still I'm stuck.

1. A simple health check

@Component
public class HealthCheck implements HealthIndicator {

    @Override
    public Health health() {
        return Health.up().build();
    }

}

2. Application.yaml is configured to show all details:

management.endpoints.web.exposure.include: "*"
management.endpoint.health.show-details: ALWAYS

3. Spring-boot-actuator included as dependency:

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'

4. Spring boot of recent release:

id 'org.springframework.boot' version '2.2.0.RELEASE'

5. Main application class is annotated with @SpringBootApplication (which implicitly brings @ComponentScan).

@SpringBootApplication
public class PaymentServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(PaymentServiceApplication.class, args);
    }

}

My custom health check has to test for Apache Kafka, but I skipped the details for brevity. Still, invoking /actuator/health endpoint I get the same default result:

{
    "status": "UP",
    "components": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 250685575168,
                "free": 99168997376,
                "threshold": 10485760
            }
        },
        "ping": {
            "status": "UP"
        }
    }
}

Is there anything I may have missed?

2
i tried it and the health endpoint is used. Could you make a system.out. there and try it again... if there is something in the console.pL4Gu33
Verify the component is actually configured as a spring bean. Just annotating as @Component does not guarantee it becomes a bean if there's no component scan for it.Compass
@Compass, I've updated the question. The main class is annotated with @SpringBootApplication, which enables component scan.Tomek Cejner
Your HealthIndicator class doesn't add any extra information.jordiburgos

2 Answers

2
votes

I found the solution, but am not sure about the cause. The class was indeed not registered as bean, and surprisingly, adding explicitly base packages attribute helped:

@SpringBootApplication(scanBasePackages="com.example.*")

Interesting that there was no need to do the above in a different project.

0
votes

Interestingly the base package for the HealthIndicator is not recognized by the application container instead of declaring the class as @component Stereotype.

Fix for the issue- declare base package of HealthIndicator in Main Spring boot application class: @SpringBootApplication (scanBasePackages = "basepackage for your class HealthCheck") public class PaymentServiceApplication {

public static void main(String[] args) {
    SpringApplication.run(PaymentServiceApplication.class, args);
}

}

EDIT : NOTE- The above declaration of base package="" is not main spring boot class is not necessary.

You must stop the SERVER and then clean build the maven application and RESTART the server and re-run the application

You can now run find your custom health endpoint.