您的位置:首页 > 编程语言 > Java开发

java - disconf分布式管理-配置文件篇

2016-11-29 11:08 387 查看
1、pom.xml文件引入disconf依赖jar包
<dependencies>
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.35</version>
</dependency>
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-core</artifactId>
<version>2.6.35</version>
</dependency>
</dependencies>
2、applicationContext.xml文件引入disconf配置
<!-- 使用disconf必须添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="com.source"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>

<!--disconf 托管文件 配置更改会自动reload-->
<bean id="configproperties_disconf" class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/logback.xml</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>
3、resources目录下新建disconf.properties环境配置文件
#是否使用远程配置文件,true(默认)从远程获取配置信息,false从本地获取配置文件
disconf.enable.remote.conf=true
#disconf配置服务器的HOST,用逗号分隔127.0.0.1:8000,127.0.0.1:8000
disconf.conf_server_host=127.0.0.1:8000
#版本
disconf.version=1.0.0
#产品线_服务名
disconf.app=source
#环境
disconf.env=test
#debug
debug=true
#忽略哪些分布式配置,用逗号分隔
disconf.ignore=
#获取远程配置 重试次数,默认是3次
disconf.conf_server_url_retry_times=1
#获取远程配置 重试时休眠时间,默认是5秒
disconf.conf_server_url_retry_sleep_seconds=1
#指定文件下载路径
#disconf.user_define_download_dir=F:\space\CarSource\target\classes
# 配置文件下载默认classpath,false为下载到指定路径
disconf.enable_local_download_dir_in_class_path=true


Disconf接入,各环境部署使用同一个war包,需要修改tomcat的服务端的配置。

 tomcat bin目录下的catalina.sh   

找到:# Bugzilla 37848: only output this if we have a TTY

在上面增加:

JAVA_OPTS="$JAVA_OPTS -Ddisconf.env=test-Ddisconf.enable.remote.conf=true -Ddisconf.conf_server_host=127.0.0.1:8000"

根据指定的disconf环境进行部署,此时,diconf.properties文件中的以上被指定了的变量将会被写入指定值,进而读取该环境下其他托管的配置文件信息。

4、获取.properties文件的内容

DisconfDataGetter.getByFile("source.properties").get("test.value").toString();

test.value为source.properties文件中的要获取的value对应的key.

5、spring+mybatis的配置文件(spring-mybatis.xml)中,去除beans属性中的
default-autowire="byName"

具体原因可参见:https://my.oschina.NET/u/1455908/blog/215953

Disconf接入,各环境部署使用同一个war包,需要修改tomcat的服务端的配置。

 tomcat bin目录下的catalina.sh   

找到:# Bugzilla 37848: only output this if we have a TTY

在上面增加:

JAVA_OPTS="$JAVA_OPTS -Ddisconf.env=test-Ddisconf.enable.remote.conf=true -Ddisconf.conf_server_host=127.0.0.1:8000"

根据指定的disconf环境进行部署,此时,diconf.properties文件中的以上被指定了的变量将会被写入指定值,进而读取该环境下其他托管的配置文件信息。

4、获取.properties文件的内容

DisconfDataGetter.getByFile("source.properties").get("test.value").toString();

test.value为source.properties文件中的要获取的value对应的key.

5、spring+mybatis的配置文件(spring-mybatis.xml)中,去除beans属性中的
default-autowire="byName"

具体原因可参见:https://my.oschina.NET/u/1455908/blog/215953
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: