From ce44a0403016d0aeaf2ef332b501834cdd5501b4 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 25 Jan 2016 11:02:17 +0000 Subject: [PATCH] Move generic config server discovery to spring-cloud-config Fixes gh-302 (spring-cloud-consul and -zookeeper my still need to add some bootstrap config to ensure that the DiscoveryClient is available in bootstrap context). --- ...ntConfigServiceBootstrapConfiguration.java | 86 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/DiscoveryClientConfigServiceBootstrapConfiguration.java diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/DiscoveryClientConfigServiceBootstrapConfiguration.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/DiscoveryClientConfigServiceBootstrapConfiguration.java new file mode 100644 index 00000000..25568379 --- /dev/null +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/DiscoveryClientConfigServiceBootstrapConfiguration.java @@ -0,0 +1,86 @@ +/* + * Copyright 2013-2014 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.config.client; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.util.UtilAutoConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; + +/** + * Bootstrap configuration for a config client that wants to lookup the config server via + * discovery. + * + * @author Dave Syer + */ +@ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled", matchIfMissing = false) +@Configuration +@Import({ UtilAutoConfiguration.class }) +public class DiscoveryClientConfigServiceBootstrapConfiguration { + + private static Log logger = LogFactory.getLog(DiscoveryClientConfigServiceBootstrapConfiguration.class); + + @Autowired + private ConfigClientProperties config; + + @Autowired + private DiscoveryClient client; + + @EventListener(ContextRefreshedEvent.class) + public void onApplicationEvent(ContextRefreshedEvent event) { + refresh(); + } + + private void refresh() { + try { + logger.debug("Locating configserver via discovery"); + ServiceInstance server = this.client + .getInstances(this.config.getDiscovery().getServiceId()).get(0); + String url = getHomePage(server); + if (server.getMetadata().containsKey("password")) { + String user = server.getMetadata().get("user"); + user = user == null ? "user" : user; + this.config.setUsername(user); + String password = server.getMetadata().get("password"); + this.config.setPassword(password); + } + if (server.getMetadata().containsKey("configPath")) { + String path = server.getMetadata().get("configPath"); + if (url.endsWith("/") && path.startsWith("/")) { + url = url.substring(0, url.length() - 1); + } + url = url + path; + } + this.config.setUri(url); + } + catch (Exception ex) { + logger.warn("Could not locate configserver via discovery", ex); + } + } + + private String getHomePage(ServiceInstance server) { + return server.getUri().toString() + "/"; + } + +} diff --git a/spring-cloud-config-client/src/main/resources/META-INF/spring.factories b/spring-cloud-config-client/src/main/resources/META-INF/spring.factories index 8f2ccf02..4efe1764 100644 --- a/spring-cloud-config-client/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-config-client/src/main/resources/META-INF/spring.factories @@ -4,4 +4,5 @@ org.springframework.cloud.config.client.ConfigClientAutoConfiguration # Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\ -org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration \ No newline at end of file +org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration,\ +org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration \ No newline at end of file