Reputation: 1222
I believe followed almost all tutorials on the Internet and read many SO answers and still I'm stuck.
@Component
public class HealthCheck implements HealthIndicator {
@Override
public Health health() {
return Health.up().build();
}
}
management.endpoints.web.exposure.include: "*"
management.endpoint.health.show-details: ALWAYS
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
id 'org.springframework.boot' version '2.2.0.RELEASE'
@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?
Upvotes: 9
Views: 7204
Reputation: 11
If you have packages structure like:
com.example.health.MyHealthIndicator
com.example.myapp.MyApp (SpringApplication.run() is here)
then MyHealthIndicator will not be treated as component. In this case in MyApp you should add scanBasePackages
@SpringBootApplication(scanBasePackages="com.example.*")
public class MyApp { ... }
BUT if you put your component inside package where application is, then scanBasePackages is not needed:
com.example.myapp.health.MyHealthIndicator
com.example.myapp.MyApp
Spring will automatically find all components inside com.example.myapp.* packages. And it will not search outside of this package, unless you ask him to do so via scanBasePackages parameter.
Upvotes: 0
Reputation: 41
Instead of using
@SpringBootApplication(scanBasePackages="com.example.*")
You can instead define your packages to scan
@ComponentScan(basePackages = {"com.example", "com.other"}
I will note I tested using scanBasepackages as described in the top answer and that doesn't seem to work if you also have the @ComponentScan defined.
Upvotes: 0
Reputation: 31
This is solved by adding this code:
management:
endpoint:
health:
show-details: always
However, it creates another problem. When calling /actuator/health endpoint, you see all the details which is problematic for me. What I want to achieve is to see only statuses for both endpoints.
Upvotes: 2
Reputation: 121
I encounter issue too, what I miss is
management:
endpoint:
health:
show-details: always
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1222
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.
Upvotes: 3