文章目录
用于服务启动之后,健康检查完成才会给服务流量
java
/**
* http://localhost:8080/actuator/health
* 服务启动后,服务健康状态为DOWN,等待执行完成warmup()之后变为UP
*
* @author batman
*/
@Component
public class MyHealthIndicator implements HealthIndicator {
public static final AtomicBoolean preheat = new AtomicBoolean(false);
public static final AtomicBoolean preheatEnd = new AtomicBoolean(false);
public static final AtomicInteger count = new AtomicInteger(0);
@Override
public Health health() {
if (preheat.compareAndSet(false, true)) {
warmup();
preheatEnd.compareAndSet(false, true);
}
if (!preheatEnd.get()) {
return Health.down().build();
}
return Health.up().build();
}
private void warmup() {
try {
Thread.sleep(5000L
);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}