我找到了一个解决方案,使用弹簧云饲养员没有Spring引导的基础上,在这里提供的想法https://wenku.baidu.com/view/493cf9eba300a6c30d229f49.html
它应该很容易更新,以满足您的需求和使用Spring的云配置服务器(在CloudEnvironement类需要进行更新,以从服务器,而不是动物园管理员)加载文件
首先,创建一个类CloudEnvironement将创建一个动物园管理员PropertySource(前):
CloudEnvironement。java的
public class CloudEnvironment extends StandardServletEnvironment {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
super.customizePropertySources(propertySources);
try {
propertySources.addLast(initConfigServicePropertySourceLocator(this));
}
catch (Exception ex) {
logger.warn("failed to initialize cloud config environment", ex);
}
}
private PropertySource<?> initConfigServicePropertySourceLocator(Environment environment) {
ZookeeperConfigProperties configProp = new ZookeeperConfigProperties();
ZookeeperProperties props = new ZookeeperProperties();
props.setConnectString("myzookeeper:2181");
CuratorFramework fwk = curatorFramework(exponentialBackoffRetry(props), props);
ZookeeperPropertySourceLocator propertySourceLocator = new ZookeeperPropertySourceLocator(fwk, configProp);
PropertySource<?> source= propertySourceLocator.locate(environment);
return source ;
}
private CuratorFramework curatorFramework(RetryPolicy retryPolicy, ZookeeperProperties properties) {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
builder.connectString(properties.getConnectString());
CuratorFramework curator = builder.retryPolicy(retryPolicy).build();
curator.start();
try {
curator.blockUntilConnected(properties.getBlockUntilConnectedWait(), properties.getBlockUntilConnectedUnit());
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
return curator;
}
private RetryPolicy exponentialBackoffRetry(ZookeeperProperties properties) {
return new ExponentialBackoffRetry(properties.getBaseSleepTimeMs(),
properties.getMaxRetries(),
properties.getMaxSleepMs());
}
}
然后创建一个自定义的XMLWebApplicationContext类:它将使加载从动物园管理员的PropertySource当你的web应用启动并更换弹簧引导的引导魔力:
MyConfigurableWebApplicationContext.java
public class MyConfigurableWebApplicationContext extends XmlWebApplicationContext {
@Override
protected ConfigurableEnvironment createEnvironment() {
return new CloudEnvironment();
}
}
最后,在您的web.xml文件中添加以下上下文参数以使用y我们的MyConfigurableWebApplicationContext类并引导您的CloudEnvironement。
<context-param>
<param-name>contextClass</param-name>
<param-value>com.kiabi.config.MyConfigurableWebApplicationContext</param-value>
</context-param>
如果你使用一个标准的属性文件配置者,仍应被加载,所以你可以在这两个本地文件和动物园管理员属性。
对于这一切工作,你需要有弹簧云起动动物园管理员,配置和策展人框架的jar在类路径与他们的扶养,如果你使用Maven的,你可以添加以下到您的的pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-dependencies</artifactId>
<version>1.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
</dependencies>
嗨。对此有何更新?我处于相同的情况 – 2015-07-28 12:54:31
@David Geary有没有想过这个? – Selwyn 2015-10-27 12:18:26
对不起,我还没有看过这个,因为它需要一些手工努力才能在非弹簧启动应用程序中执行此操作,从弹簧启动中复制代码等。不幸的是,Spring云的东西似乎只专注于Spring Boot。 – 2015-10-28 16:23:53