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下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM