Skip to content

Commit

Permalink
Use SmartInitializingSingleton instead of BeanPostProcessor to set up @…
Browse files Browse the repository at this point in the history
…LoadBalanced WebClient.Builder. (#1319)
  • Loading branch information
OlgaMaciaszek authored Jan 16, 2024
1 parent 9908098 commit c8fe5d3
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -32,12 +33,12 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;

/**
Expand All @@ -49,7 +50,7 @@
* @author Gang Li
* @author Olga Maciaszek-Sharma
*/
@Configuration(proxyBeanMethods = false)
@AutoConfiguration
@Conditional(BlockingRestClassesPresentCondition.class)
@ConditionalOnBean(LoadBalancerClient.class)
@EnableConfigurationProperties(LoadBalancerClientsProperties.class)
Expand All @@ -59,6 +60,10 @@ public class LoadBalancerAutoConfiguration {
@Autowired(required = false)
private List<RestTemplate> restTemplates = Collections.emptyList();

@LoadBalanced
@Autowired(required = false)
private List<RestClient.Builder> restClientBuilders = Collections.emptyList();

@Autowired(required = false)
private List<LoadBalancerRequestTransformer> transformers = Collections.emptyList();

Expand All @@ -74,6 +79,19 @@ public SmartInitializingSingleton loadBalancedRestTemplateInitializerDeprecated(
});
}

@Bean
public SmartInitializingSingleton loadBalancedRestClientBuilderInitializer(
final ObjectProvider<List<RestClientBuilderCustomizer>> restClientBuilderCustomizers) {
return () -> restClientBuilderCustomizers.ifAvailable(customizers -> {
for (RestClient.Builder restClientBuilder : LoadBalancerAutoConfiguration.this.restClientBuilders) {
for (RestClientBuilderCustomizer customizer : customizers) {
customizer.customize(restClientBuilder);
}

}
});
}

@Bean
@ConditionalOnMissingBean
public LoadBalancerRequestFactory loadBalancerRequestFactory(LoadBalancerClient loadBalancerClient) {
Expand Down Expand Up @@ -101,11 +119,10 @@ public RestTemplateCustomizer restTemplateCustomizer(final LoadBalancerIntercept
}

@Bean
@ConditionalOnBean(LoadBalancerInterceptor.class)
@ConditionalOnMissingBean
LoadBalancerRestClientBuilderBeanPostProcessor lbRestClientPostProcessor(
final LoadBalancerInterceptor loadBalancerInterceptor, ApplicationContext context) {
return new LoadBalancerRestClientBuilderBeanPostProcessor(loadBalancerInterceptor, context);
public RestClientBuilderCustomizer restClientBuilderCustomizer(
final LoadBalancerInterceptor loadBalancerInterceptor) {
return restClientBuilder -> restClientBuilder.requestInterceptor(loadBalancerInterceptor);
}

}
Expand Down Expand Up @@ -174,11 +191,10 @@ public RestTemplateCustomizer restTemplateCustomizer(
}

@Bean
@ConditionalOnBean(RetryLoadBalancerInterceptor.class)
@ConditionalOnMissingBean
LoadBalancerRestClientBuilderBeanPostProcessor lbRestClientPostProcessor(
final RetryLoadBalancerInterceptor loadBalancerInterceptor, ApplicationContext context) {
return new LoadBalancerRestClientBuilderBeanPostProcessor(loadBalancerInterceptor, context);
public RestClientBuilderCustomizer restClientBuilderCustomizer(
final RetryLoadBalancerInterceptor loadBalancerInterceptor) {
return restClientBuilder -> restClientBuilder.requestInterceptor(loadBalancerInterceptor);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,9 @@
*
* @author Olga Maciaszek-Sharma
* @since 4.1.0
* @deprecated to be removed in the next release.
*/
@Deprecated
public class LoadBalancerRestClientBuilderBeanPostProcessor implements BeanPostProcessor {

private final ClientHttpRequestInterceptor loadBalancerInterceptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.client.loadbalancer;

import org.springframework.web.client.RestClient;

/**
* A customizer interface for {@link RestClient.Builder}. Used to set
* {@link LoadBalancerInterceptor} on the builder at the end of the singleton
* pre-instantiation phase.
*
* @author Olga Maciaszek-Sharma
* @since 4.1.1
*/
public interface RestClientBuilderCustomizer {

void customize(RestClient.Builder restClientBuilder);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -117,6 +117,43 @@ void multipleRestClientBuilders() {
});
}

@Test
void restTemplatesAndRestClientsFromUsersAutoConfiguration() {
applicationContextRunner
.withConfiguration(AutoConfigurations.of(TwoRestTemplatesAndTwoRestClientBuilders.class))
.run(context -> {
final Map<String, RestClient.Builder> restClientBuilders = context
.getBeansOfType(RestClient.Builder.class);
final Map<String, RestTemplate> restTemplates = context.getBeansOfType(RestTemplate.class);

assertThat(restClientBuilders).isNotNull();
assertThat(restClientBuilders.values()).hasSize(2);

TwoRestTemplatesAndTwoRestClientBuilders.Two two = context
.getBean(TwoRestTemplatesAndTwoRestClientBuilders.Two.class);

assertThat(two.loadBalancedRestClientBuilder).isNotNull();
assertLoadBalanced(two.loadBalancedRestClientBuilder);

assertThat(two.nonLoadBalancedRestClientBuilder).isNotNull();
two.nonLoadBalancedRestClientBuilder
.requestInterceptors(interceptors -> assertThat(interceptors).isEmpty());

assertThat(restTemplates).isNotNull();
Collection<RestTemplate> templates = restTemplates.values();
assertThat(templates).hasSize(2);

TwoRestTemplatesAndTwoRestClientBuilders.Two twoRestTemplate = context
.getBean(TwoRestTemplatesAndTwoRestClientBuilders.Two.class);

assertThat(twoRestTemplate.loadBalanced).isNotNull();
assertLoadBalanced(twoRestTemplate.loadBalanced);

assertThat(twoRestTemplate.nonLoadBalanced).isNotNull();
assertThat(twoRestTemplate.nonLoadBalanced.getInterceptors()).isEmpty();
});
}

protected abstract void assertLoadBalanced(RestClient.Builder restClientBuilder);

protected abstract void assertLoadBalanced(RestTemplate restTemplate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public final class LoadBalancerEnvironmentPropertyUtils {

private LoadBalancerEnvironmentPropertyUtils() {
throw new IllegalStateException("Should not instantiate a utility class");
throw new UnsupportedOperationException("Cannot instantiate a utility class");
}

public static boolean trueForClientOrDefault(Environment environment, String propertySuffix) {
Expand Down
2 changes: 2 additions & 0 deletions src/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
<suppress files=".*Tests.*" checks="JavadocVariable"/>
<suppress files=".*Tests.*" checks="JavadocMethod"/>
<suppress files=".*Tests.*" checks="HideUtilityClassConstructor"/>
<suppress files=".*AutoConfiguration.*" checks="HideUtilityClassConstructor"/>
<suppress files=".*AutoConfiguration.*" checks="FinalClass"/>
<suppress files=".*ReactiveDiscoveryClient.*" checks="JavadocVariable"/>
</suppressions>

0 comments on commit c8fe5d3

Please sign in to comment.