Add "application" to config search path in server
This is incredibly useful in a distributed system: all components get the shared config in "application.yml" (for instance) as well as their specific values per spring.application.name.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-platform-config-server</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-platform-config-server</name>
|
||||
<description>spring-platform-config-server</description>
|
||||
@@ -16,11 +16,27 @@
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.platform</groupId>
|
||||
<artifactId>spring-platform-netflix</artifactId>
|
||||
<version>${spring-platform.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.platform</groupId>
|
||||
<artifactId>spring-platform-config-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.platform</groupId>
|
||||
<artifactId>spring-platform-netflix-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
@@ -34,6 +50,10 @@
|
||||
<artifactId>org.eclipse.jgit</artifactId>
|
||||
<version>2.3.1.201302201838-r</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.eureka</groupId>
|
||||
<artifactId>eureka-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
@@ -43,6 +63,7 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring-platform.version>${project.version}</spring-platform.version>
|
||||
<start-class>org.springframework.platform.config.server.Application</start-class>
|
||||
<java.version>1.7</java.version>
|
||||
</properties>
|
||||
@@ -52,6 +73,9 @@
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<classifier>exec</classifier>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -53,10 +53,13 @@ public class Application {
|
||||
@Configuration
|
||||
@Profile("!native")
|
||||
protected static class GitRepositoryConfiguration {
|
||||
@Autowired
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.platform.config.server")
|
||||
public JGitEnvironmentRepository repository() {
|
||||
return new JGitEnvironmentRepository();
|
||||
return new JGitEnvironmentRepository(environment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.util.FileUtils;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.platform.config.Environment;
|
||||
import org.springframework.platform.config.PropertySource;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -35,14 +36,17 @@ import org.springframework.util.Assert;
|
||||
public class JGitEnvironmentRepository implements EnvironmentRepository {
|
||||
|
||||
public static final String DEFAULT_URI = "https://github.com/scratches/config-repo";
|
||||
|
||||
|
||||
private static Log logger = LogFactory.getLog(JGitEnvironmentRepository.class);
|
||||
|
||||
private File basedir;
|
||||
|
||||
private String uri = DEFAULT_URI;
|
||||
|
||||
public JGitEnvironmentRepository() {
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
public JGitEnvironmentRepository(ConfigurableEnvironment environment) {
|
||||
this.environment = environment;
|
||||
try {
|
||||
final File basedir = Files.createTempDirectory("config-repo-").toFile();
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
@@ -50,13 +54,15 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
|
||||
public void run() {
|
||||
try {
|
||||
FileUtils.delete(basedir, FileUtils.RECURSIVE);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.warn("Failed to delete temporary directory on exit: " + e);
|
||||
}
|
||||
}
|
||||
});
|
||||
this.basedir = basedir;
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Cannot create temp dir", e);
|
||||
}
|
||||
}
|
||||
@@ -67,8 +73,7 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
|
||||
}
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setBasedir(File basedir) {
|
||||
this.basedir = basedir;
|
||||
}
|
||||
@@ -80,12 +85,15 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
|
||||
if (new File(basedir, ".git").exists()) {
|
||||
git = Git.open(basedir);
|
||||
git.fetch().call();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (basedir.exists()) {
|
||||
try {
|
||||
FileUtils.delete(basedir, FileUtils.RECURSIVE);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to initialize base directory", e);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(
|
||||
"Failed to initialize base directory", e);
|
||||
}
|
||||
}
|
||||
Assert.state(basedir.mkdirs(), "Could not create basedir: " + basedir);
|
||||
@@ -100,7 +108,8 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
|
||||
result = clean(environment.findOne(application, name, label));
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Cannot clone repository", e);
|
||||
}
|
||||
}
|
||||
@@ -109,9 +118,14 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
|
||||
Environment result = new Environment(value.getName(), value.getLabel());
|
||||
for (PropertySource source : value.getPropertySources()) {
|
||||
String name = source.getName().replace(basedir.toURI().toString(), "");
|
||||
if (name.contains(("classpath:/"))) {
|
||||
continue;
|
||||
}
|
||||
if (environment.getPropertySources().contains(name)) {
|
||||
continue;
|
||||
}
|
||||
name = name.replace("applicationConfig: [", "");
|
||||
name = uri + "/"
|
||||
+ name.substring(0, name.contains("]") ? name.lastIndexOf("]") : name.length());
|
||||
name = uri + "/" + name.replace("]", "");
|
||||
result.add(new PropertySource(name, source.getSource()));
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.web.context.support.StandardServletEnvironment;
|
||||
public class NativeEnvironmentRepository implements EnvironmentRepository {
|
||||
|
||||
private Set<String> standardSources = new HashSet<String>(Arrays.asList(
|
||||
"vcap",
|
||||
StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
|
||||
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
|
||||
StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME,
|
||||
|
||||
@@ -55,6 +55,9 @@ public class SpringApplicationEnvironmentRepository implements EnvironmentReposi
|
||||
|
||||
private String[] getArgs(String config) {
|
||||
List<String> list = new ArrayList<String>();
|
||||
if (!config.startsWith("application")) {
|
||||
config = "application," + config;
|
||||
}
|
||||
list.add("--spring.config.name=" + config);
|
||||
list.add("--spring.platform.bootstrap.enabled=false");
|
||||
if (locations != null) {
|
||||
|
||||
@@ -8,4 +8,4 @@ spring:
|
||||
server:
|
||||
port: 8888
|
||||
management:
|
||||
context_path: /admin
|
||||
context_path: /admin
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.io.File;
|
||||
import org.eclipse.jgit.util.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.platform.config.Environment;
|
||||
|
||||
/**
|
||||
@@ -32,7 +33,7 @@ import org.springframework.platform.config.Environment;
|
||||
*/
|
||||
public class JGitEnvironmentRepositoryTests {
|
||||
|
||||
private JGitEnvironmentRepository repository = new JGitEnvironmentRepository();
|
||||
private JGitEnvironmentRepository repository = new JGitEnvironmentRepository(new StandardEnvironment());
|
||||
|
||||
private File basedir = new File("target/config-repo");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user