Custom Health Indicators in Spring Boot

This commit is contained in:
Mona Mohamadinia 2020-08-14 03:02:52 +04:30
parent cd34bc8454
commit 850ace5f5e
3 changed files with 45 additions and 1 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.health;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HealthApplication {
public static void main(String[] args) {
SpringApplication.run(HealthApplication.class, args);
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.health;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
@Component
public class RandomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
double chance = ThreadLocalRandom.current().nextDouble();
Health.Builder status = Health.up();
if (chance > 0.9) {
status = Health.down(new RuntimeException("Bad Luck"));
}
Map<String, Object> details = new HashMap<>();
details.put("chance", chance);
details.put("strategy", "thread-local");
return status.withDetails(details).build();
}
}

View File

@ -1 +1,5 @@
management.health.probes.enabled=true
management.health.probes.enabled=true
management.endpoint.health.show-details=always
management.health.random.enabled=false
management.endpoint.health.status.http-mapping.down=500
management.endpoint.health.status.http-mapping.out_of_service=503