上一篇博文介紹了disconf web的搭建流程,這一篇就介紹disconf client通過配置xml文件來獲取disconf管理端的配置信息。
1. 登錄管理端,並新建APP,然后上傳配置文件
2. 在工程中新建disconf.properties,根據管理端新建的APP修改相關屬性,放在classpath下
1 # 是否使用遠程配置文件 2 # true(默認)會從遠程獲取配置 false則直接獲取本地配置 3 enable.remote.conf=true 4 5 # 6 # 配置服務器的 HOST,用逗號分隔 127.0.0.1:8000,127.0.0.1:8000 7 # 8 conf_server_host=192.168.1.103:8081 9 10 # 版本, 請采用 X_X_X_X 格式 11 version=1_0_0_0 12 13 # APP 請采用 產品線_服務名 格式 14 app=weather_forecast 15 16 # 環境 17 env=local 18 19 # debug 20 debug=true 21 22 # 忽略哪些分布式配置,用逗號分隔 23 ignore= 24 25 # 獲取遠程配置 重試次數,默認是3次 26 conf_server_url_retry_times=1 27 # 獲取遠程配置 重試時休眠時間,默認是5秒 28 conf_server_url_retry_sleep_seconds=1
3. 增加spring配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 6 http://www.springframework.org/schema/context 7 http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 8 9 <context:component-scan base-package="org.springinaction.weather.config" /> 10 11 <!-- 使用disconf必須添加以下配置 --> 12 <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean" 13 destroy-method="destroy"> 14 <property name="scanPackage" value="org.springinaction.weather.config" /> 15 </bean> 16 <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond" 17 init-method="init" destroy-method="destroy"> 18 </bean> 19 20 <!-- 使用托管方式的disconf配置(無代碼侵入, 配置更改不會自動reload) --> 21 <bean id="configproperties_no_reloadable_disconf" 22 class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean"> 23 <property name="locations"> 24 <list> 25 <value>redis.properties</value> 26 </list> 27 </property> 28 </bean> 29 30 <bean id="propertyConfigurer" 31 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 32 <property name="ignoreResourceNotFound" value="true" /> 33 <property name="ignoreUnresolvablePlaceholders" value="true" /> 34 <property name="propertiesArray"> 35 <list> 36 <ref bean="configproperties_no_reloadable_disconf" /> 37 </list> 38 </property> 39 </bean> 40 </beans>
其中 redis.properties為管理端上傳的配置文件。
4. 添加依賴庫
<!-- disconf --> <dependency> <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> <version>2.6.36</version> </dependency>
5. 新建RedisConfig.java類,便於直接在程序中使用redis.properties中的配置信息
1 package org.springinaction.weather.config; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.stereotype.Component; 5 6 @Component("redisConfig") 7 public class RedisConfig { 8 9 @Value("${redis.host}") 10 private String host; 11 12 @Value("${redis.port}") 13 private String port; 14 15 public String getHost() { 16 return host; 17 } 18 19 public String getPort() { 20 return port; 21 } 22 }
6. 運行程序
1 package org.springinaction.weather.service; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import org.springinaction.weather.config.RedisConfig; 6 7 public class DisconfTest { 8 9 public static void main(String[] args) throws InterruptedException { 10 ApplicationContext context = new ClassPathXmlApplicationContext("spring-disconf.xml"); 11 RedisConfig redisConfig = context.getBean(RedisConfig.class); 12 while (true) { 13 System.out.println(redisConfig.getHost()); 14 System.out.println(redisConfig.getPort()); 15 Thread.sleep(3000); 16 } 17 } 18 19 }
至此,已經可以使用properties中的屬性。如果要修改屬性,只要在管理端修改相應的配置文件即可,相關屬性會自動同步到各個應用部署的機器中。
但這種做法是最簡單的應用,只會同步屬性文件到本地,但不會reload到系統中,需要系統重啟一下。disconf也可以做到熱加載,同時也可以通過annotation的方式進行集成,后續再介紹相關內容。對於大部分應用,這樣的集成已經可以,畢竟配置文件不會經常改動。
只要正確運行過一遍配置文件,文件就會被緩存在本地,即使與管理端斷開,也不影響系統的正常運行。默認下載到disconf/download目錄下,然后在運行的時候發布到classpath下。