上一篇博文介绍了disconf web的搭建流程,这一篇就介绍disconf client通过配置xml文件来获取disconf管理端的配置信息。

1. 登录管理端,并新建APP,然后上传配置文件

2. 在工程中新建disconf.properties,根据管理端新建的APP修改相关属性,放在classpath下

 # 是否使用远程配置文件
# true(默认)会从远程获取配置 false则直接获取本地配置
enable.remote.conf=true #
# 配置服务器的 HOST,用逗号分隔 127.0.0.1:8000,127.0.0.1:8000
#
conf_server_host=192.168.1.103:8081 # 版本, 请采用 X_X_X_X 格式
version=1_0_0_0 # APP 请采用 产品线_服务名 格式
app=weather_forecast # 环境
env=local # debug
debug=true # 忽略哪些分布式配置,用逗号分隔
ignore= # 获取远程配置 重试次数,默认是3次
conf_server_url_retry_times=1
# 获取远程配置 重试时休眠时间,默认是5秒
conf_server_url_retry_sleep_seconds=1

3. 增加spring配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="org.springinaction.weather.config" /> <!-- 使用disconf必须添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="org.springinaction.weather.config" />
</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>
</list>
</property>
</bean> <bean id="propertyConfigurer"
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为管理端上传的配置文件。

4. 添加依赖库

<!-- disconf -->
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.36</version>
</dependency>

5. 新建RedisConfig.java类,便于直接在程序中使用redis.properties中的配置信息

 package org.springinaction.weather.config;

 import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("redisConfig")
public class RedisConfig { @Value("${redis.host}")
private String host; @Value("${redis.port}")
private String port; public String getHost() {
return host;
} public String getPort() {
return port;
}
}

6. 运行程序

 package org.springinaction.weather.service;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springinaction.weather.config.RedisConfig; public class DisconfTest { public static void main(String[] args) throws InterruptedException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-disconf.xml");
RedisConfig redisConfig = context.getBean(RedisConfig.class);
while (true) {
System.out.println(redisConfig.getHost());
System.out.println(redisConfig.getPort());
Thread.sleep(3000);
}
} }

至此,已经可以使用properties中的属性。如果要修改属性,只要在管理端修改相应的配置文件即可,相关属性会自动同步到各个应用部署的机器中。 
但这种做法是最简单的应用,只会同步属性文件到本地,但不会reload到系统中,需要系统重启一下。disconf也可以做到热加载,同时也可以通过annotation的方式进行集成,后续再介绍相关内容。对于大部分应用,这样的集成已经可以,毕竟配置文件不会经常改动。

只要正确运行过一遍配置文件,文件就会被缓存在本地,即使与管理端断开,也不影响系统的正常运行。默认下载到disconf/download目录下,然后在运行的时候发布到classpath下。

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

  1. disconf实践(三)基于XML的分布式配置文件管理,自动reload

    上一篇介绍了基于xml的非自动reload的分布式配置文件管理,这一篇介绍自动reload的方式(基于disconf实践二). 1. 修改RedisConfig.java package org.sp ...

  2. disconf实践(四)基于注解的分布式配置文件管理,自动reload

    上一篇讲解了基于xml的自动reload的分布式配置文件管理,这一篇讲解基于注解的自动reload的方式(基于disconf实践二). 1. 修改spring配置文件 <?xml version ...

  3. 基于zookeeper实现分布式配置中心(二)

    上一篇(基于zookeeper实现分布式配置中心(一))讲述了zookeeper相关概念和工作原理.接下来根据zookeeper的特性,简单实现一个分布式配置中心. 配置中心的优势 1.各环境配置集中 ...

  4. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  5. 基于XML的AOP配置

    创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...

  6. 01Spring基于xml的IOC配置--入门

    01Spring基于xml的IOC配置 1.创建一个普通的maven工程 1.1 选择maven,不用骨架,点击下一步. 1.2 填写GroupId.ArtifactId.Version.填完点击下一 ...

  7. Spring中AOP的基于xml开发和配置

    pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  8. 基于zookeeper实现分布式配置中心(一)

    最近在学习zookeeper,发现zk真的是一个优秀的中间件.在分布式环境下,可以高效解决数据管理问题.在学习的过程中,要深入zk的工作原理,并根据其特性做一些简单的分布式环境下数据管理工具.本文首先 ...

  9. mybatis学习一:基于xml与注解配置入门实例与问题

    注:本case参考自:http://www.cnblogs.com/ysocean/p/7277545.html 一:Mybatis的介绍: MyBatis 本是apache的一个开源项目iBatis ...

随机推荐

  1. 10、springboot之集成druid

    在pom.xml中添加 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid< ...

  2. 使用Mac自带svn搭建服务器,并使用idea进行连接

    一.检查Mac自带SVN版本号 二.创建代码仓库 $ mkdir -p /Users/Shared/svn/repository $ svnadmin create /Users/Shared/svn ...

  3. 【Chromium】sandboxed window问题记录

    问题发现 在业务逻辑中发现有时使用chrome.app.window.create这个API创建出来的窗口无法使用其他的API,不仅其他chrome.app.window的API说window is ...

  4. Redis教程基本命令

    Redis是什么? Redis(REmote DIctionary Server)是一个key-value存储系统,能够高速存储数据,value值可以为字符串.哈希表.列表.集合.有序集合,位图,hy ...

  5. MySQL:PyMySQL&ORM

    一.PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. Django中也可以使用PyMySQL连接MySQ ...

  6. vue-cli新建一个项目

    零.我想把项目安装在C:\www\Arup.DAH.ABCD\SourceCode\FrontEnd这个目录下,所以在我想安装的位置,Shift+右键-->powershell窗口,打开下图位置 ...

  7. drupal7 profile2模块获取个人信息

    一.问题背景: 用profile2模块,扩展个人信息,增加了“手机号”等信息,一些地方想要获取当前用户的手机号 二.解决办法: 用profile2自带的方法:profile2_load_by_user ...

  8. ztree 获取当前选中节点的子节点集合

    功能:获取当前选中节点的子节点id集合. 步骤:1.获取当前节点 2.用ztree的方法transformToArray()获取当前选中节点(含选中节点)的子节点对象集合. 3.遍历集合,取出需要的值 ...

  9. number to string

    C++进行int to string和string to int 下面方法一存在内存泄露 #include<strstream>void main(){ std::strstream ss ...

  10. Android 图文混排 通过webview实现并实现点击图片

    在一个开源项目看到是用的webview 实现的 1. 这是在asset中的一个模板html <html> <head> <title>News Detail< ...