php disconf,未主/disconf

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值