我找到了一個解決方案,使用彈簧雲飼養員沒有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