Disconf
修改点
把ReloadingPropertyPlaceholderConfigurer类改为继承PropertySourcesPlaceholderConfigurer
注意事项
不要再使用@PropertySource注解来引入本地配置文件
不要再使用其他PropertyPlaceholderConfigurer来引入本地配置文件
不要再使用其他PropertySourcesPlaceholderConfigurer来引入本地配置文件
demo
springboot1、2按如下进行注解式配置:
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ApplicationConfig {
@Bean(destroyMethod = "destroy")
public BeanDefinitionRegistryPostProcessor disconfMgrBean() {
return new DisconfMgrBean();
}
@Bean(initMethod = "init", destroyMethod = "destroy")
public DisconfMgrBeanSecond disconfMgrBeanSecond() {
return new DisconfMgrBeanSecond();
}
@Bean
@Primary
public PropertiesFactoryBean propertiesFactory() throws IOException {
ReloadablePropertiesFactoryBean factory = new ReloadablePropertiesFactoryBean();
/*
* 本地文件路径("classpath:/test.properties")放前面,远程文件名放后;这样远程文件的内容会覆盖本地文件的内容。
* 需要配置-Ddisconf.ignore=test.properties,以避免disconf尝试从远程下载test.properties文件
*/
factory.setLocations(Arrays.asList("classpath:/test.properties", "db.properties", "dubbo.properties"));
factory.setFileEncoding(StandardCharsets.UTF_8.name());
return factory;
}
@Bean
public static PropertySourcesPlaceholderConfigurer properties(PropertiesFactoryBean factory) throws IOException {
ReloadingPropertyPlaceholderConfigurer ppc = new ReloadingPropertyPlaceholderConfigurer();
// 如果需要远程内容覆盖本地application.properties里的内容,需设置localOverride为true
// ppc.setLocalOverride(true);
ppc.setIgnoreUnresolvablePlaceholders(true);
ppc.setIgnoreResourceNotFound(true);
ppc.setProperties(factory.getObject());
return ppc;
}
}
disconf-client 2.6.37 版本修改如下
ConfigLoaderUtils 修改类中的方法,在没有disconf.properties 的情况下,可以在环境变量中获取配置信息,这样便于做通用的镜像,便于管理。
/**
* @param propertyFilePath
* @return Properties
* @throws Exception
* @Description: 配置文件载入器助手
* @author liaoqiqi
* @date 2013-6-19
*/
public static Properties loadConfig(final String propertyFilePath)
throws Exception {
try {
// 用TOMCAT模式 来载入试试
return ConfigLoaderUtils.loadWithTomcatMode(propertyFilePath);
} catch (Exception e1) {
try {
// 用普通模式进行载入
return loadWithNormalMode(propertyFilePath);
} catch (Exception e2) {
// throw new Exception("cannot load config file: "
// + propertyFilePath);
Map map = System.getenv();
Properties properties = new Properties();
for (Iterator itr = map.keySet().iterator(); itr.hasNext(); ) {
String key = itr.next();
System.out.println("环境变量" + key + "=" + map.get(key));
properties.setProperty(key, map.get(key));
}
return properties;
}
}
}