一、Disconf:Distributed Configuration Management Platform(分佈式配置管理平臺),專一於各類「分佈式系統配置管理」的「通用組件」和「通用平臺」, 提供統一的「配置管理服務」css
二、配置步驟:redis
1.maven中添加jar依賴:spring
<dependency> <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> </dependency>
2.spring配置文件配置(配置在springmvc的主配置文件中):服務器
<!-- 使用disconf必須添加如下配置 --> <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean" destroy-method="destroy"> <property name="scanPackage" value="com.sf" /> </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:redis.properties</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 配置文件配置disconf:mvc
# # 配置服務器的 HOST,用逗號分隔 127.0.0.1:8000,127.0.0.1:8000 # conf_server_host=XXXXXX # 版本, 請採用 X_X_X_X 格式 version=1_0_0_0 # APP 請採用 產品線_服務名 格式 app=XXXXX # 是否使用遠程配置文件 # true(默認)會從遠程獲取配置 false則直接獲取本地配置 enable.remote.conf=true # 環境 env=rd # debug debug=true # 忽略哪些分佈式配置,用逗號分隔 ignore= # 獲取遠程配置 重試次數,默認是3次 conf_server_url_retry_times=3 # 獲取遠程配置 重試時休眠時間,默認是5秒 conf_server_url_retry_sleep_seconds=1
4.添加properties配置文件:app
redis.ip=127.0.0.1
redis1.port=8888
redis1.password=123456
5.使用spring的註解@Value來讀取配置文件,並注入值maven
@Repository public class RedisConfig { @Value("#{configproperties_disconf['redis.ip']}") public String ip; @Value("#{configproperties_disconf['redis.port']}") public int port;
// getter & setter
}
6.經過注入類的屬性使用proerties配置分佈式
經過上面配置已經將redis.properties的讀取工做配置完成,在須要讀取redis.properties的類中注入RedisConfigurl
@Autowired
RedisConfig redisConfig;
讀取方式:redisConfig.getIp();spa
以上是使用spring集成disconf讀取properties文件的配置過程,還須要將redisConfig配置文件上傳到disconf的服務上,重啓服務便可下載使用