disconf实践(一)

  公司目前的应用基本采用分布式部署,通过F5进行集群管理。分布式应用带来的好处是,随着流量的增加,可以快速扩展应用节点,分摊压力。分布式也会带来一定的挑战,譬如配置文件管理。如果某个配置要修改,那么所有的节点都要进行修改,当面临大规模集群时,很容易改错或改漏。因此,需要一个统一的配置管理中心对配置进行管理,集中修改一个配置文件,所有机器能够自动同步。disconf就是百度开源的配置管理中心。 
    以下是参照开源文档与公司的项目进行集成实践。 
1. 下载管理端,并安装。 
   https://github.com/knightliao/disconf/tree/master/disconf-web 
2. 登录管理端,并新建APP,然后上传相关配置文件 
   
3. 新建disconf.properties,根据管理端新建的APP修改相关属性,放在classpath下。 

Java代码  收藏代码
  1. enable.remote.conf=true  
  2. conf_server_host=http://192.168.3.141:8080/  
  3. version=V1.0  
  4. app=GTW  
  5. env=local  
  6. debug=true  
  7. ignore=  
  8. conf_server_url_retry_times=1  
  9. conf_server_url_retry_sleep_seconds=1  


4. 增加spring配置(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_no_reloadable_disconf"  
  27.           class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">  
  28.         <property name="locations">  
  29.             <list>  
  30.                 <value>redis.properties</value>  
  31.                 <value>jdbc.properties</value>  
  32.                 <value>config.properties</value>  
  33.             </list>  
  34.         </property>  
  35.     </bean>  
  36.       
  37.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  38.         <property name="ignoreResourceNotFound" value="true"/>  
  39.         <property name="ignoreUnresolvablePlaceholders" value="true"/>  
  40.         <property name="propertiesArray">  
  41.             <list>  
  42.                 <ref bean="configproperties_no_reloadable_disconf"/>  
  43.             </list>  
  44.         </property>  
  45.     </bean>  
  46. </beans>  


其中 redis.properties, jdbc.properties,config.properties 为管理端上传的三个配置文件。 
5.添加依赖包 

Xml代码  收藏代码
  1.    <dependency>  
  2. <groupId>com.baidu.disconf</groupId>  
  3. <artifactId>disconf-client</artifactId>  
  4. <version>2.6.36</version>  
  5.    </dependency>  


至此,已经可以使用properties中的属性。如果要修改属性,只要在管理端修改相应的配置文件即可,相关属性会自动同步到各个应用部署的机器中。 
注意:这种做法是最简单的应用,只会同步属性文件到本地,但不会reload到系统中,需要系统重启一下。disconf也可以做到热加载,同时也可以通过annotation的方式进行集成,后续再介绍相关内容。对于大部分应用,这样的集成已经可以,毕竟配置文件不会经常改动。 

只要正确运行过一遍配置文件,文件就会被缓存在本地,即使与管理端断开,也不影响系统的正常运行。从集成情况看,应该是下载到disconf/download目录下,然后在运行的时候发布到classpath下。

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