Browse Source
* Add loadbalancer starter. Add property to set BlockingLoadBalancerClient as default non-reactive loadbalancer. Start working on auto-enabling caching. * Automatically enable caching. Fixes gh-585. * Add tests. * Add reference docs. * Change property name.pull/606/head
10 changed files with 209 additions and 17 deletions
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* Copyright 2012-2019 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.loadbalancer.config; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.cache.CacheManager; |
||||
import org.springframework.cache.annotation.EnableCaching; |
||||
import org.springframework.cache.interceptor.CacheAspectSupport; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
/** |
||||
* An AutoConfiguration that automatically enables caching when when Spring Boot and |
||||
* Spring Framework Cache support classes are present. |
||||
* |
||||
* @author Olga Maciaszek-Sharma |
||||
* @see CacheManager |
||||
* @see CacheAutoConfiguration |
||||
* @see CacheAspectSupport |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnClass({ CacheManager.class, CacheAutoConfiguration.class }) |
||||
@ConditionalOnMissingBean(CacheAspectSupport.class) |
||||
@EnableCaching |
||||
@AutoConfigureBefore(CacheAutoConfiguration.class) |
||||
public class LoadBalancerCacheAutoConfiguration { |
||||
|
||||
} |
@ -1,4 +1,5 @@
@@ -1,4 +1,5 @@
|
||||
# AutoConfiguration |
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ |
||||
org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration,\ |
||||
org.springframework.cloud.loadbalancer.config.BlockingLoadBalancerClientAutoConfiguration |
||||
org.springframework.cloud.loadbalancer.config.BlockingLoadBalancerClientAutoConfiguration,\ |
||||
org.springframework.cloud.loadbalancer.config.LoadBalancerCacheAutoConfiguration |
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
/* |
||||
* Copyright 2012-2019 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.loadbalancer.config; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; |
||||
import org.springframework.boot.test.util.TestPropertyValues; |
||||
import org.springframework.cache.CacheManager; |
||||
import org.springframework.cache.support.NoOpCacheManager; |
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link LoadBalancerCacheAutoConfiguration}. |
||||
* |
||||
* @author Olga Maciaszek-Sharma |
||||
*/ |
||||
|
||||
class LoadBalancerCacheAutoConfigurationTests { |
||||
|
||||
@Test |
||||
void shouldAutoEnableCaching() { |
||||
AnnotationConfigApplicationContext context = setup(""); |
||||
assertThat(context.getBeansOfType(CacheManager.class)).isNotEmpty(); |
||||
assertThat(context.getBeansOfType(CacheManager.class).get("cacheManager")) |
||||
.isNotInstanceOf(NoOpCacheManager.class); |
||||
} |
||||
|
||||
@Test |
||||
void shouldUseNoOpCacheIfCacheTypeNone() { |
||||
AnnotationConfigApplicationContext context = setup("spring.cache.type=none"); |
||||
assertThat(context.getBeansOfType(CacheManager.class)).isNotEmpty(); |
||||
assertThat(context.getBeansOfType(CacheManager.class).get("cacheManager")) |
||||
.isInstanceOf(NoOpCacheManager.class); |
||||
} |
||||
|
||||
private AnnotationConfigApplicationContext setup(String property) { |
||||
List<Class> config = new ArrayList<>(); |
||||
config.add(LoadBalancerCacheAutoConfiguration.class); |
||||
config.add(CacheAutoConfiguration.class); |
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
||||
if (StringUtils.hasText(property)) { |
||||
TestPropertyValues.of(property).applyTo(context); |
||||
} |
||||
context.register(config.toArray(new Class[0])); |
||||
context.refresh(); |
||||
return context; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,39 @@
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<parent> |
||||
<groupId>org.springframework.cloud</groupId> |
||||
<artifactId>spring-cloud-commons-parent</artifactId> |
||||
<version>2.2.0.BUILD-SNAPSHOT</version> |
||||
<relativePath>..</relativePath> |
||||
</parent> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId> |
||||
<name>spring-cloud-starter-loadbalancer</name> |
||||
<description>Spring Cloud Starter LoadBalancer</description> |
||||
<url>https://projects.spring.io/spring-cloud</url> |
||||
<organization> |
||||
<name>Pivotal Software, Inc.</name> |
||||
<url>https://www.spring.io</url> |
||||
</organization> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
</properties> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.cloud</groupId> |
||||
<artifactId>spring-cloud-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.cloud</groupId> |
||||
<artifactId>spring-cloud-loadbalancer</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-cache</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
</project> |
Loading…
Reference in new issue