package de.twomartens.templateservice.actuator; import de.twomartens.templateservice.entity.Greeting; import de.twomartens.templateservice.interceptors.RequestTypeInterceptor; import de.twomartens.templateservice.interceptors.TraceIdInterceptor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import java.util.Optional; /** * A Health check which checks if the rest services are working.] */ @Slf4j @Component public class RestHealthCheck extends AbstractHealthCheck implements HealthIndicator { private final RestTemplate restTemplate; public RestHealthCheck(ServerProperties serverProperties, TraceIdInterceptor traceIdInterceptor, RequestTypeInterceptor requestTypeInterceptor, RestTemplate restTemplate) { super(Optional.ofNullable(serverProperties.getPort()).orElse(8080), traceIdInterceptor, requestTypeInterceptor); this.restTemplate = restTemplate; } @Override boolean isEndpointAvailable() { try { ResponseEntity result = restTemplate.getForEntity(getEndpoint(), Greeting.class); Greeting body = result.getBody(); if (body == null) { return false; } return !body.getMessage().isEmpty(); } catch (RestClientException e) { return false; } } @Override String getMethodName() { return "template-service-rest"; } String mkEndpoint() { return String.format("http://%s:%d/greeting?name=RestHealthCheck", getHost(), getPort()); } }