From 2212b9e31a6922c4e913531520fff588dca9c4cf Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Thu, 25 Feb 2016 10:30:34 -0700 Subject: [PATCH] If failFast=false only log warnings for config loading. fixes gh-137 --- .../main/asciidoc/spring-cloud-consul.adoc | 7 ++- .../consul/config/ConsulConfigProperties.java | 5 ++ .../config/ConsulPropertySourceLocator.java | 18 +++++-- ...sulPropertySourceLocatorFailFastTests.java | 48 +++++++++++++++++++ 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorFailFastTests.java diff --git a/docs/src/main/asciidoc/spring-cloud-consul.adoc b/docs/src/main/asciidoc/spring-cloud-consul.adoc index 7abe180d..9e9743d4 100644 --- a/docs/src/main/asciidoc/spring-cloud-consul.adoc +++ b/docs/src/main/asciidoc/spring-cloud-consul.adoc @@ -185,6 +185,11 @@ You could store a YAML document in any of the keys listed above. You can change the data key using `spring.cloud.consul.config.data-key`. +[[spring-cloud-consul-failfast]] +== Fail Fast + +It may be convenient in certain circumstances (like local development or certain test scenarios) to not fail if consul isn't available for configuration. Setting `spring.cloud.consul.config.failFast=false` will cause the configuration module to log a warning rather than throw an exception. This will allow the application to continue startup normally. + [[spring-cloud-consul-retry]] == Consul Retry @@ -203,7 +208,7 @@ Retry has a `RetryInterceptorBuilder` that makes it easy to create one. [[spring-cloud-consul-bus]] == Spring Cloud Bus with Consul -TODO: document Spring Cloud Consul Bus +Coming in a later release. [[spring-cloud-consul-hystrix]] == Circuit Breaker with Hystrix diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java index 4d382ebb..47de704d 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java @@ -56,6 +56,11 @@ public class ConsulConfigProperties { private Watch watch = new Watch(); + /** + * Throw exceptions during config lookup if true, otherwise, log warnings. + */ + private boolean failFast = true; + @Data public class Watch { private int waitTime = 2; 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 3b386fdb..dfb07cc2 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 @@ -28,13 +28,17 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.retry.annotation.Retryable; +import org.springframework.util.ReflectionUtils; import com.ecwid.consul.v1.ConsulClient; +import lombok.extern.apachecommons.CommonsLog; + /** * @author Spencer Gibb */ @Order(0) +@CommonsLog public class ConsulPropertySourceLocator implements PropertySourceLocator { private ConsulClient consul; @@ -75,9 +79,17 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator { Collections.reverse(this.contexts); for (String propertySourceContext : this.contexts) { - ConsulPropertySource propertySource = create(propertySourceContext); - propertySource.init(); - composite.addPropertySource(propertySource); + try { + ConsulPropertySource propertySource = create(propertySourceContext); + propertySource.init(); + composite.addPropertySource(propertySource); + } catch (Exception e) { + if (this.properties.isFailFast()) { + ReflectionUtils.rethrowRuntimeException(e); + } else { + log.warn("Unable to load consul config from "+ propertySourceContext, e); + } + } } return composite; 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 new file mode 100644 index 00000000..43578047 --- /dev/null +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorFailFastTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2013-2016 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.consul.config; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Spencer Gibb + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = ConsulPropertySourceLocatorFailFastTests.Config.class) +@WebIntegrationTest(value = {"spring.application.name=testConsulPropertySourceLocatorFailFast", + "spring.cloud.consul.host=53210a7c-4809-42cb-8b30-057d2db85fcc", + "spring.cloud.consul.port=65530", + "spring.cloud.consul.retry.maxAttempts=0", + "spring.cloud.consul.config.failFast=false"}, randomPort = true) +public class ConsulPropertySourceLocatorFailFastTests { + + @Configuration + @EnableAutoConfiguration + static class Config { + } + + @Test + public void testFailFastFalse() { + } + +}