You need to create a custom HealthAggregator that implements the "at least one UP" logic. Here's how you can do it:
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import java.util.Map;
import java.util.stream.Collectors;
public class PgWHealthAggregator implements HealthAggregator {
@Override
public Health aggregate(Map<String, Health> healths) {
boolean atLeastOneUp = healths.values().stream()
.anyMatch(health -> health.getStatus() == Status.UP);
Status status = atLeastOneUp ? Status.UP : Status.DOWN;
return Health.status(status)
.details(healths.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
.build();
}
}
Then, modify your configuration to use this custom aggregator:
@Configuration
public class PgwHealthContributorConfig {
@Bean
public CompositeHealthContributor pgw(PgwConfigProperties pgwConfigProperties, MeterRegistry meterRegistry,
PgwHealthRestClient pgwHealthRestClient) {
Map<String, HealthContributor> contributors = new LinkedHashMap<>();
var configs = pgwConfigProperties.getConfigs();
for (int i = 0; i < configs.size(); i++) {
String name = "pgw-" + i;
contributors.put(name, new SinglePgwHealthIndicator(...));
}
CompositeHealthContributor compositeHealthContributor = CompositeHealthContributor.fromMap(contributors);
compositeHealthContributor.setAggregator(new PgWHealthAggregator()); // Set the custom aggregator
return compositeHealthContributor;
}
}
Explanation:
-
PgWHealthAggregator:
- This class implements the
HealthAggregator interface.
- The
aggregate() method takes a Map<String, Health> representing the health of each individual contributor (e.g., "pgw-0", "pgw-1").
- It uses a stream to check if any of the
Health objects have a status of Status.UP.
- Based on whether at least one is UP, it sets the overall status to
UP or DOWN.
- It includes the details of all the individual health checks in the aggregated health result. This is important so you can still see which individual PGWs are up or down.
-
Configuration Changes: