1.加入依赖
<dependency> <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> <version>2.6.36</version> </dependency>
2.添加配置文件spring-disconf.xml
这里将application.properties交给disconf管理
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 使用disconf必须添加以下配置 --> <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean" destroy-method="destroy"> <property name="scanPackage" value="com.sijibao.scfs"/> </bean> <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond" init-method="init" destroy-method="destroy"> </bean> <!-- 需要托管的配置文件 --> <bean id="configproperties_disconf" class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean"> <property name="locations"> <list> <value>application.properties</value> </list> </property> </bean> <bean id="propertyConfigurer" class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer"> <property name="ignoreResourceNotFound" value="true"/> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="propertiesArray"> <list> <ref bean="configproperties_disconf"/> </list> </property> </bean> </beans>
3.添加disconf.properties
这里需要填服务端地址,应用名,配置环境等
enable.remote.conf=true
conf_server_host=xxx
env=dev
version=1.0
app=xxx
debug=true
ignore=
conf_server_url_retry_times=10
conf_server_url_retry_sleep_seconds=3
4.登录discnof服务器建立托管的配置文件
需要注意的是springboot或者spring5集成disconf启动时会报错,需要改动源码
改动点:
com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer第97行:
return super.parseStringValue(buf.toString(), props, visitedPlaceholders);
改为:
return helper.replacePlaceholders(strVal, props);
5.需要注意的是,虽然配置交给disconf托管了。但disconf.properties在开发环境和测试环境,生产环境的配置都不一样,这样每次打包的时候还需要修改disconf.properties。为了打包时不需要修改文件,可以将disconf.properties文件外置
在启动时输入:
扫描二维码关注公众号,回复:
3533071 查看本文章
java -Ddisconf.conf=/hao24/config/disconf_open/disconf.properties
如果是tomcat启动:
tomcat 启动示例
linux环境在catalina.sh
在
“if [ $have_tty -eq 1 ]; then"之后增加
JAVA_OPTS=”-Ddisconf.conf=/hao24/config/disconf_open/disconf.properties"
window环境 startup.bat第一行
SET CATALINA_OPTS= “-Ddisconf.conf=/hao24/config/disconf_open/disconf.properties”