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

来源:互联网 发布:网络教育 高起专 时间 编辑:程序博客网 时间:2024/04/26 15:42
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.valuesource.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.valuesource.properties文件中的要获取的value对应的key.

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

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

0 0
原创粉丝点击