If failFast=false only log warnings for config loading.

fixes gh-137
This commit is contained in:
Spencer Gibb
2016-02-25 10:30:34 -07:00
parent 3f3fccd01a
commit 2212b9e31a
4 changed files with 74 additions and 4 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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() {
}
}