Browse Source

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
pull/436/head
Spencer Gibb 6 years ago
parent
commit
ca80e3ac78
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 1
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfiguration.java
  2. 43
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/WebClientCustomizer.java
  3. 57
      spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java

1
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfiguration.java

@ -4,7 +4,6 @@ import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;

43
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/WebClientCustomizer.java

@ -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);
}

57
spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java

@ -16,9 +16,17 @@
package org.springframework.cloud.client.loadbalancer.reactive; 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.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance; 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.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient; 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; import static org.assertj.core.api.Assertions.assertThat;
@ -90,20 +92,29 @@ public class ReactiveLoadBalancerAutoConfigurationTests {
assertThat(getFilters(two.nonLoadBalanced)).isNullOrEmpty(); 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) { protected ConfigurableApplicationContext init(Class<?> config) {
return new SpringApplicationBuilder().web(WebApplicationType.NONE) return new SpringApplicationBuilder().web(WebApplicationType.NONE)
// .properties("spring.aop.proxyTargetClass=true") // .properties("spring.aop.proxyTargetClass=true")
.sources(config, ReactiveLoadBalancerAutoConfiguration.class).run(); .sources(config, WebClientAutoConfiguration.class,
ReactiveLoadBalancerAutoConfiguration.class).run();
} }
@Configuration @Configuration
protected static class OneWebClientBuilder { protected static class NoWebClientBuilder {
@Bean
@LoadBalanced
WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
@Bean @Bean
LoadBalancerClient loadBalancerClient() { LoadBalancerClient loadBalancerClient() {
@ -116,23 +127,23 @@ public class ReactiveLoadBalancerAutoConfigurationTests {
} }
@Configuration @Configuration
protected static class TwoWebClientBuilders { protected static class OneWebClientBuilder extends NoWebClientBuilder {
@Primary
@Bean @Bean
WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
@LoadBalanced @LoadBalanced
@Bean
WebClient.Builder loadBalancedWebClientBuilder() { WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder(); return WebClient.builder();
} }
}
@Configuration
protected static class TwoWebClientBuilders extends OneWebClientBuilder {
@Primary
@Bean @Bean
LoadBalancerClient loadBalancerClient() { WebClient.Builder webClientBuilder() {
return new NoopLoadBalancerClient(); return WebClient.builder();
} }
@Configuration @Configuration

Loading…
Cancel
Save