Browse Source

Merge remote-tracking branch 'origin/3.1.x'

pull/1194/head
Olga Maciaszek-Sharma 2 years ago
parent
commit
9a06eb4a05
  1. 2
      spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/config/BlockingLoadBalancerClientAutoConfiguration.java
  2. 27
      spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/config/LoadBalancerCacheAutoConfiguration.java
  3. 8
      spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/config/BlockingLoadBalancerClientAutoConfigurationTests.java
  4. 10
      spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/config/LoadBalancerCacheAutoConfigurationTests.java

2
spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/config/BlockingLoadBalancerClientAutoConfiguration.java

@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore; @@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
@ -50,6 +51,7 @@ import org.springframework.web.client.RestTemplate; @@ -50,6 +51,7 @@ import org.springframework.web.client.RestTemplate;
@AutoConfigureAfter(LoadBalancerAutoConfiguration.class)
@AutoConfigureBefore({ org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration.class })
@ConditionalOnClass(RestTemplate.class)
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.enabled", havingValue = "true", matchIfMissing = true)
public class BlockingLoadBalancerClientAutoConfiguration {
@Bean

27
spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/config/LoadBalancerCacheAutoConfiguration.java

@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory; @@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -42,9 +43,9 @@ import org.springframework.context.annotation.Conditional; @@ -42,9 +43,9 @@ import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
/**
* An AutoConfiguration that automatically enables caching when when Spring Boot, and
* Spring Framework Cache support are present. If Caffeine is present in the classpath, it
* will be used for loadbalancer caching. If not, a default cache will be used.
* An AutoConfiguration that automatically enables caching when Spring Boot, and Spring
* Framework Cache support are present. If Caffeine is present in the classpath, it will
* be used for loadbalancer caching. If not, a default cache will be used.
*
* @author Olga Maciaszek-Sharma
* @since 2.2.0
@ -56,8 +57,8 @@ import org.springframework.context.annotation.Configuration; @@ -56,8 +57,8 @@ import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ CacheManager.class, CacheAutoConfiguration.class })
@AutoConfigureAfter(CacheAutoConfiguration.class)
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.cache.enabled", matchIfMissing = true)
@EnableConfigurationProperties(LoadBalancerCacheProperties.class)
@Conditional(LoadBalancerCacheAutoConfiguration.OnLoadBalancerCachingEnabledCondition.class)
public class LoadBalancerCacheAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ -129,4 +130,22 @@ public class LoadBalancerCacheAutoConfiguration { @@ -129,4 +130,22 @@ public class LoadBalancerCacheAutoConfiguration {
}
static final class OnLoadBalancerCachingEnabledCondition extends AllNestedConditions {
OnLoadBalancerCachingEnabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.enabled", havingValue = "true", matchIfMissing = true)
static class LoadBalancerEnabled {
}
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.cache.enabled", matchIfMissing = true)
static class LoadBalancerCacheEnabled {
}
}
}

8
spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/config/BlockingLoadBalancerClientAutoConfigurationTests.java

@ -52,4 +52,12 @@ class BlockingLoadBalancerClientAutoConfigurationTests { @@ -52,4 +52,12 @@ class BlockingLoadBalancerClientAutoConfigurationTests {
.run(context -> assertThat(context).doesNotHaveBean(BlockingLoadBalancerClient.class));
}
@Test
void shouldNotFailOnRetryFactoryWhenLoadBalancingDisabled() {
applicationContextRunner.withPropertyValues("spring.cloud.loadbalancer.enabled=false").run(context -> {
assertThat(context).doesNotHaveBean(BlockingLoadBalancerClient.class);
assertThat(context).doesNotHaveBean(LoadBalancedRetryFactory.class);
});
}
}

10
spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/config/LoadBalancerCacheAutoConfigurationTests.java

@ -171,6 +171,16 @@ class LoadBalancerCacheAutoConfigurationTests { @@ -171,6 +171,16 @@ class LoadBalancerCacheAutoConfigurationTests {
});
}
@Test
void shouldNotInstantiateDefaultLoadBalancerCacheIfLoadBalancingDisabled() {
noCaffeineRunner().withPropertyValues("spring.cloud.loadbalancer.enabled=false")
.withUserConfiguration(TestConfiguration.class).run(context -> {
assertThat(context.getBeansOfType(CacheManager.class)).hasSize(1);
assertThat(context.getBean("cacheManager")).isInstanceOf(ConcurrentMapCacheManager.class);
assertThat(((CacheManager) context.getBean("cacheManager")).getCacheNames()).isEmpty();
});
}
private ApplicationContextRunner baseApplicationRunner() {
return new ApplicationContextRunner().withConfiguration(
AutoConfigurations.of(CacheAutoConfiguration.class, LoadBalancerCacheAutoConfiguration.class));

Loading…
Cancel
Save