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

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

@ -0,0 +1,43 @@ @@ -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 @@ @@ -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; @@ -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 { @@ -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,23 +127,23 @@ public class ReactiveLoadBalancerAutoConfigurationTests { @@ -116,23 +127,23 @@ public class ReactiveLoadBalancerAutoConfigurationTests {
}
@Configuration
protected static class TwoWebClientBuilders {
protected static class OneWebClientBuilder extends NoWebClientBuilder {
@Primary
@Bean
WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
@LoadBalanced
@Bean
WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
}
@Configuration
protected static class TwoWebClientBuilders extends OneWebClientBuilder {
@Primary
@Bean
LoadBalancerClient loadBalancerClient() {
return new NoopLoadBalancerClient();
WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
@Configuration

Loading…
Cancel
Save