Skip to content

Commit

Permalink
Support blocking LB health-checks with RestClient. Fix javadoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek committed Nov 3, 2023
1 parent bc4082d commit cfb75fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.springframework.beans.factory.annotation.Qualifier;

/**
* Annotation to mark a RestTemplate or WebClient bean to be configured to use a
* LoadBalancerClient.
* Annotation to mark a RestTemplate, RestClient.Builder or WebClient.Builder bean to be
* configured to use a LoadBalancerClient.
* @author Spencer Gibb
*/
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
Expand Down Expand Up @@ -202,6 +203,21 @@ public ServiceInstanceListSupplierBuilder withBlockingHealthChecks() {
return this;
}

/**
* Adds a {@link HealthCheckServiceInstanceListSupplier} that uses user-provided
* {@link RestClient} instance to the {@link ServiceInstanceListSupplier} hierarchy.
* @return the {@link ServiceInstanceListSupplierBuilder} object
*/
public ServiceInstanceListSupplierBuilder withBlockingRestClientHealthChecks() {
DelegateCreator creator = (context, delegate) -> {
RestClient restClient = context.getBean(RestClient.class);
LoadBalancerClientFactory loadBalancerClientFactory = context.getBean(LoadBalancerClientFactory.class);
return blockingHealthCheckServiceInstanceListSupplier(restClient, delegate, loadBalancerClientFactory);
};
this.creators.add(creator);
return this;
}

/**
* Adds a {@link HealthCheckServiceInstanceListSupplier} that uses user-provided
* {@link RestTemplate} instance to the {@link ServiceInstanceListSupplier} hierarchy.
Expand Down Expand Up @@ -360,6 +376,22 @@ private ServiceInstanceListSupplier blockingHealthCheckServiceInstanceListSuppli
}));
}

private ServiceInstanceListSupplier blockingHealthCheckServiceInstanceListSupplier(RestClient restClient,
ServiceInstanceListSupplier delegate, LoadBalancerClientFactory loadBalancerClientFactory) {
return new HealthCheckServiceInstanceListSupplier(delegate, loadBalancerClientFactory,
(serviceInstance, healthCheckPath) -> Mono.defer(() -> {
URI uri = UriComponentsBuilder.fromUriString(getUri(serviceInstance, healthCheckPath)).build()
.toUri();
try {
return Mono.just(HttpStatus.OK
.equals(restClient.get().uri(uri).retrieve().toBodilessEntity().getStatusCode()));
}
catch (Exception ignored) {
return Mono.just(false);
}
}));
}

static String getUri(ServiceInstance serviceInstance, String healthCheckPath) {
if (StringUtils.hasText(healthCheckPath)) {
String path = healthCheckPath.startsWith("/") ? healthCheckPath : "/" + healthCheckPath;
Expand Down

0 comments on commit cfb75fa

Please sign in to comment.