Uses custom WebClientCustomizer rather than one from Boot.
Using the boot WebClientCustomizer caused the auto configured WebClient.Builder to use the loadbalancer filter function even though it wasn't annotated with `@LoadBalanced`. Fixes gh-434
This commit is contained in:
@@ -4,7 +4,6 @@ import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2013-2018 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
|
||||
*
|
||||
* http://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.reactive;
|
||||
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
* Callback interface that can be used to customize a
|
||||
* {@link org.springframework.web.reactive.function.client.WebClient.Builder
|
||||
* WebClient.Builder}.
|
||||
*
|
||||
* See original {@link org.springframework.boot.web.reactive.function.client.WebClientCustomizer}
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface WebClientCustomizer {
|
||||
|
||||
/**
|
||||
* Callback to customize a
|
||||
* {@link org.springframework.web.reactive.function.client.WebClient.Builder
|
||||
* WebClient.Builder} instance.
|
||||
* @param webClientBuilder the client builder to customize
|
||||
*/
|
||||
void customize(WebClient.Builder webClientBuilder);
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,17 @@
|
||||
|
||||
package org.springframework.cloud.client.loadbalancer.reactive;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
@@ -34,12 +42,6 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
@@ -90,20 +92,29 @@ public class ReactiveLoadBalancerAutoConfigurationTests {
|
||||
assertThat(getFilters(two.nonLoadBalanced)).isNullOrEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noCustomWebClientBuilders() {
|
||||
ConfigurableApplicationContext context = init(NoWebClientBuilder.class);
|
||||
final Map<String, WebClient.Builder> webClientBuilders = context
|
||||
.getBeansOfType(WebClient.Builder.class);
|
||||
|
||||
assertThat(webClientBuilders).hasSize(1);
|
||||
|
||||
WebClient.Builder builder = context.getBean(WebClient.Builder.class);
|
||||
|
||||
assertThat(builder).isNotNull();
|
||||
assertThat(getFilters(builder)).isNullOrEmpty();
|
||||
}
|
||||
|
||||
protected ConfigurableApplicationContext init(Class<?> config) {
|
||||
return new SpringApplicationBuilder().web(WebApplicationType.NONE)
|
||||
// .properties("spring.aop.proxyTargetClass=true")
|
||||
.sources(config, ReactiveLoadBalancerAutoConfiguration.class).run();
|
||||
.sources(config, WebClientAutoConfiguration.class,
|
||||
ReactiveLoadBalancerAutoConfiguration.class).run();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class OneWebClientBuilder {
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
WebClient.Builder loadBalancedWebClientBuilder() {
|
||||
return WebClient.builder();
|
||||
}
|
||||
protected static class NoWebClientBuilder {
|
||||
|
||||
@Bean
|
||||
LoadBalancerClient loadBalancerClient() {
|
||||
@@ -116,7 +127,18 @@ public class ReactiveLoadBalancerAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class TwoWebClientBuilders {
|
||||
protected static class OneWebClientBuilder extends NoWebClientBuilder {
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
WebClient.Builder loadBalancedWebClientBuilder() {
|
||||
return WebClient.builder();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class TwoWebClientBuilders extends OneWebClientBuilder {
|
||||
|
||||
@Primary
|
||||
@Bean
|
||||
@@ -124,17 +146,6 @@ public class ReactiveLoadBalancerAutoConfigurationTests {
|
||||
return WebClient.builder();
|
||||
}
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
WebClient.Builder loadBalancedWebClientBuilder() {
|
||||
return WebClient.builder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
LoadBalancerClient loadBalancerClient() {
|
||||
return new NoopLoadBalancerClient();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class Two {
|
||||
@Autowired
|
||||
|
||||
Reference in New Issue
Block a user