Skip to content

Commit

Permalink
Check DeferringLoadBalancerInterceptor exist
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLiu1123 committed Mar 30, 2024
1 parent c5b156b commit dbe9fd3
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.cloud.client.loadbalancer.DeferringLoadBalancerInterceptor;
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancedExchangeFilterFunction;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.AnnotationUtils;
Expand Down Expand Up @@ -71,6 +72,8 @@ class ExchangeClientCreator {
ClassUtils.isPresent("org.springframework.web.reactive.function.client.WebClient", null);
private static final boolean LOADBALANCER_PRESENT =
ClassUtils.isPresent("org.springframework.cloud.client.loadbalancer.LoadBalancerClient", null);
private static final boolean DEFERRING_LOADBALANCER_INTERCEPTOR_PRESENT = ClassUtils.isPresent(
"org.springframework.cloud.client.loadbalancer.DeferringLoadBalancerInterceptor", null);

private static final Field exchangeAdapterField;
private static final Field customArgumentResolversField;
Expand Down Expand Up @@ -236,11 +239,15 @@ private RestTemplate buildRestTemplate(HttpExchangeProperties.Channel channelCon
builder = builder.requestFactory(() -> getRequestFactory(channelConfig));

if (isLoadBalancerEnabled(channelConfig)) {
List<ClientHttpRequestInterceptor> interceptors = beanFactory
.getBeanProvider(ClientHttpRequestInterceptor.class)
.orderedStream()
.toList();
builder = builder.additionalInterceptors(interceptors);
Set<ClientHttpRequestInterceptor> allInterceptors = new LinkedHashSet<>();
if (DEFERRING_LOADBALANCER_INTERCEPTOR_PRESENT) {
beanFactory
.getBeanProvider(DeferringLoadBalancerInterceptor.class)
.forEach(allInterceptors::add);
} else {
beanFactory.getBeanProvider(ClientHttpRequestInterceptor.class).forEach(allInterceptors::add);
}
builder = builder.additionalInterceptors(allInterceptors);
}

// Default request factory will be replaced by user's RestTemplateCustomizer bean here
Expand Down Expand Up @@ -330,12 +337,16 @@ private RestClient buildRestClient(HttpExchangeProperties.Channel channelConfig)

if (isLoadBalancerEnabled(channelConfig)) {
builder.requestInterceptors(interceptors -> {
List<ClientHttpRequestInterceptor> newInterceptors =
beanFactory.getBeanProvider(ClientHttpRequestInterceptor.class).stream()
.toList();

Set<ClientHttpRequestInterceptor> allInterceptors = new LinkedHashSet<>(interceptors);
allInterceptors.addAll(newInterceptors);
if (DEFERRING_LOADBALANCER_INTERCEPTOR_PRESENT) {
beanFactory
.getBeanProvider(DeferringLoadBalancerInterceptor.class)
.forEach(allInterceptors::add);
} else {
beanFactory
.getBeanProvider(ClientHttpRequestInterceptor.class)
.forEach(allInterceptors::add);
}

interceptors.clear();
interceptors.addAll(allInterceptors);
Expand Down

0 comments on commit dbe9fd3

Please sign in to comment.