From e71ad1ee2e64a8ba20b37491848b6ea533543374 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 8 Apr 2020 18:41:51 -0400 Subject: [PATCH] Retry ConsulPropertySourceLocator.locateCollection() This is the new entrypoint for property source locators. fixes gh-627 --- spring-cloud-consul-config/pom.xml | 5 ++ .../config/ConsulPropertySourceLocator.java | 7 ++ ...sulPropertySourceLocatorFailFastTests.java | 1 + ...ConsulPropertySourceLocatorRetryTests.java | 69 +++++++++++++++++++ .../cloud/consul/ConsulAutoConfiguration.java | 3 + .../cloud/consul/RetryProperties.java | 14 +++- 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorRetryTests.java diff --git a/spring-cloud-consul-config/pom.xml b/spring-cloud-consul-config/pom.xml index eadbbe82..2b9fc167 100644 --- a/spring-cloud-consul-config/pom.xml +++ b/spring-cloud-consul-config/pom.xml @@ -63,6 +63,11 @@ spring-boot-autoconfigure-processor true + + org.springframework.boot + spring-boot-starter-aop + test + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java index 6bb6fa89..0b92beaa 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java @@ -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> locateCollection(Environment environment) { + return PropertySourceLocator.locateCollection(this, environment); + } + @Override @Retryable(interceptor = "consulRetryInterceptor") public PropertySource locate(Environment environment) { diff --git a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorFailFastTests.java b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorFailFastTests.java index c2c37586..14b1e1dc 100644 --- a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorFailFastTests.java +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorFailFastTests.java @@ -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) diff --git a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorRetryTests.java b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorRetryTests.java new file mode 100644 index 00000000..232c6931 --- /dev/null +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorRetryTests.java @@ -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 { + + } + +} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java index ed388c70..b2cb0ceb 100644 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java +++ b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java @@ -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") diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/RetryProperties.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/RetryProperties.java index e8e323b0..a13b00a8 100644 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/RetryProperties.java +++ b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/RetryProperties.java @@ -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();