disconf实践(二)

因为有些系统的配置文件会随着业务更改,如某些控制开关,当大批量集群时,按照上一篇文章的配置就不够啦,需要做到热加载。 
研究了一下,还好,比较简单,只要替换上一篇文章第4步的配置文件(spring-disconf.xml)即可。 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8.        http://www.springframework.org/schema/aop  
  9.        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  10.        http://www.springframework.org/schema/context  
  11.        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  12.        http://www.springframework.org/schema/tx  
  13.        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
  14.        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">  
  15.       
  16.     <!-- 使用disconf必须添加以下配置 -->  
  17.     <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"  
  18.           destroy-method="destroy">  
  19.         <property name="scanPackage" value="com.baidu"/>  
  20.     </bean>  
  21.     <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"  
  22.           init-method="init" destroy-method="destroy">  
  23.     </bean>  
  24.       
  25.     <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改自动reload)-->  
  26.     <bean id="configproperties_disconf"  
  27.       class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">  
  28.         <property name="locations">  
  29.             <list>  
  30.                 <value>config.properties</value>  
  31.             </list>  
  32.         </property>  
  33.     </bean>  
  34.       
  35.     <bean id="propertyConfigurer"  
  36.       class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">  
  37.         <property name="ignoreResourceNotFound" value="true" />  
  38.         <property name="ignoreUnresolvablePlaceholders" value="true" />  
  39.         <property name="propertiesArray">  
  40.             <list>  
  41.                 <ref bean="configproperties_disconf"/>  
  42.             </list>  
  43.         </property>  
  44.     </bean>  
  45.       
  46.     <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改不会自动reload)-->  
  47.     <bean id="configproperties_no_reloadable_disconf"  
  48.           class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">  
  49.         <property name="locations">  
  50.             <list>  
  51.                 <value>redis.properties</value>  
  52.                 <value>jdbc.properties</value>  
  53.             </list>  
  54.         </property>  
  55.     </bean>  
  56.       
  57.     <bean id="propertyConfigurerForProject1"  
  58.           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  59.         <property name="ignoreResourceNotFound" value="true"/>  
  60.         <property name="ignoreUnresolvablePlaceholders" value="true"/>  
  61.         <property name="propertiesArray">  
  62.             <list>  
  63.                 <ref bean="configproperties_no_reloadable_disconf"/>  
  64.             </list>  
  65.         </property>  
  66.     </bean>  
  67. </beans>  



其中 config.properties 实现了热加载, jdbc.properties和redis.properties只实现同步,需要重启加载。 

当我们在disconf管理端,任意修改一个config.properties的属性,就会在控制台打印出相应信息,如: 

Java代码  收藏代码
    1. INFO [main-EventThread] ReloadingPropertyPlaceholderConfigurer.propertiesReloaded(155) | Property changed detected: gtw.route.socket.timeout=8000  
    2. INFO [main-EventThread] ReloadingPropertyPlaceholderConfigurer.propertiesReloaded(227) | Updating property routeService.socketTimeout to 8000  

 

发现了很有用的功能。 
1. 测试和开发不能共用 disconf.properties ,因为对应的env不同。 
解决方案: 在启动脚本中增加 -Ddisconf.env=local 即可,可以根据不同的环境更改env.

posted @ 2016-12-12 14:17  空谷幽澜  阅读(931)  评论(0编辑  收藏  举报