Retry ConsulPropertySourceLocator.locateCollection()

This is the new entrypoint for property source locators.

fixes gh-627
This commit is contained in:
Spencer Gibb
2020-04-08 18:41:51 -04:00
parent 0c48f8b022
commit e71ad1ee2e
6 changed files with 98 additions and 1 deletions

View File

@@ -63,6 +63,11 @@
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.consul.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
@@ -72,6 +73,12 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator {
return this.contextIndex;
}
@Override
@Retryable(interceptor = "consulRetryInterceptor")
public Collection<PropertySource<?>> locateCollection(Environment environment) {
return PropertySourceLocator.locateCollection(this, environment);
}
@Override
@Retryable(interceptor = "consulRetryInterceptor")
public PropertySource<?> locate(Environment environment) {

View File

@@ -34,6 +34,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
properties = { "spring.application.name=testConsulPropertySourceLocatorFailFast",
"spring.cloud.consul.host=53210a7c-4809-42cb-8b30-057d2db85fcc",
"spring.cloud.consul.port=65530",
"spring.cloud.consul.retry.enabled=false",
"spring.cloud.consul.retry.maxAttempts=0",
"spring.cloud.consul.config.failFast=false" },
webEnvironment = RANDOM_PORT)

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2013-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.consul.config;
import com.ecwid.consul.transport.TransportException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.system.OutputCaptureRule;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Spencer Gibb
*/
/*
* @RunWith(SpringRunner.class)
*
* @SpringBootTest(classes = ConsulPropertySourceLocatorRetryTests.Config.class,
* properties = { "spring.application.name=testConsulPropertySourceLocatorRetry",
* "spring.cloud.consul.host=53210a7c-4809-42cb-8b30-057d2db85fcc",
* "logging.level.org.springframework.retry=TRACE", "spring.cloud.consul.port=65530",
* "spring.cloud.consul.retry.maxAttempts=1", "spring.cloud.consul.config.failFast=true"
* }, webEnvironment = RANDOM_PORT)
*/
public class ConsulPropertySourceLocatorRetryTests {
@Rule
public OutputCaptureRule output = new OutputCaptureRule();
@Test
public void testRetry() {
Assert.assertThrows(TransportException.class, () -> {
new SpringApplicationBuilder(Config.class).properties(
"spring.application.name=testConsulPropertySourceLocatorRetry",
"spring.cloud.consul.host=53210a7c-4809-42cb-8b30-057d2db85fcc",
"logging.level.org.springframework.retry=TRACE", "server.port=0",
"spring.cloud.consul.port=65530",
"spring.cloud.consul.retry.maxAttempts=1",
"spring.cloud.consul.config.failFast=true").run();
});
assertThat(output).contains("RetryContext retrieved");
}
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
static class Config {
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -93,6 +94,8 @@ public class ConsulAutoConfiguration {
@EnableRetry(proxyTargetClass = true)
@Import(AopAutoConfiguration.class)
@EnableConfigurationProperties(RetryProperties.class)
@ConditionalOnProperty(value = "spring.cloud.consul.retry.enabled",
matchIfMissing = true)
protected static class RetryConfiguration {
@Bean(name = "consulRetryInterceptor")

View File

@@ -25,6 +25,9 @@ import org.springframework.core.style.ToStringCreator;
@ConfigurationProperties("spring.cloud.consul.retry")
public class RetryProperties {
/** If consul retry is enabled. */
private boolean enabled = true;
/** Initial retry interval in milliseconds. */
private long initialInterval = 1000;
@@ -40,6 +43,14 @@ public class RetryProperties {
public RetryProperties() {
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public long getInitialInterval() {
return this.initialInterval;
}
@@ -74,7 +85,8 @@ public class RetryProperties {
@Override
public String toString() {
return new ToStringCreator(this).append("initialInterval", this.initialInterval)
return new ToStringCreator(this).append("enabled", this.enabled)
.append("initialInterval", this.initialInterval)
.append("multiplier", this.multiplier)
.append("maxInterval", this.maxInterval)
.append("maxAttempts", this.maxAttempts).toString();