公司目前的應用基本采用分布式部署,通過F5進行集群管理。分布式應用帶來的好處是,隨着流量的增加,可以快速擴展應用節點,分攤壓力。分布式也會帶來一定的挑戰,譬如配置文件管理。如果某個配置要修改,那么所有的節點都要進行修改,當面臨大規模集群時,很容易改錯或改漏。因此,需要一個統一的配置管理中心對配置進行管理,集中修改一個配置文件,所有機器能夠自動同步。disconf就是百度開源的配置管理中心。
以下是參照開源文檔與公司的項目進行集成實踐。
1. 下載管理端,並安裝。
https://github.com/knightliao/disconf/tree/master/disconf-web
2. 登錄管理端,並新建APP,然后上傳相關配置文件
3. 新建disconf.properties,根據管理端新建的APP修改相關屬性,放在classpath下。
- enable.remote.conf=true
- conf_server_host=http://192.168.3.141:8080/
- version=V1.0
- app=GTW
- env=local
- debug=true
- ignore=
- conf_server_url_retry_times=1
- conf_server_url_retry_sleep_seconds=1
4. 增加spring配置(spring-disconf.xml)
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
- <!-- 使用disconf必須添加以下配置 -->
- <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
- destroy-method="destroy">
- <property name="scanPackage" value="com.baidu"/>
- </bean>
- <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
- init-method="init" destroy-method="destroy">
- </bean>
- <!-- 使用托管方式的disconf配置(無代碼侵入, 配置更改不會自動reload)-->
- <bean id="configproperties_no_reloadable_disconf"
- class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>redis.properties</value>
- <value>jdbc.properties</value>
- <value>config.properties</value>
- </list>
- </property>
- </bean>
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="ignoreResourceNotFound" value="true"/>
- <property name="ignoreUnresolvablePlaceholders" value="true"/>
- <property name="propertiesArray">
- <list>
- <ref bean="configproperties_no_reloadable_disconf"/>
- </list>
- </property>
- </bean>
- </beans>
其中 redis.properties, jdbc.properties,config.properties 為管理端上傳的三個配置文件。
5.添加依賴包
- <dependency>
- <groupId>com.baidu.disconf</groupId>
- <artifactId>disconf-client</artifactId>
- <version>2.6.36</version>
- </dependency>
至此,已經可以使用properties中的屬性。如果要修改屬性,只要在管理端修改相應的配置文件即可,相關屬性會自動同步到各個應用部署的機器中。
注意:這種做法是最簡單的應用,只會同步屬性文件到本地,但不會reload到系統中,需要系統重啟一下。disconf也可以做到熱加載,同時也可以通過annotation的方式進行集成,后續再介紹相關內容。對於大部分應用,這樣的集成已經可以,畢竟配置文件不會經常改動。
只要正確運行過一遍配置文件,文件就會被緩存在本地,即使與管理端斷開,也不影響系統的正常運行。從集成情況看,應該是下載到disconf/download目錄下,然后在運行的時候發布到classpath下。