disconf实践(二)基于XML的分布式配置文件管理,不会自动reload

上一篇博文介绍了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下。

posted @ 2017-05-21 16:23  写出高级BUG  阅读(2058)  评论(0编辑  收藏  举报