上一篇博文介绍了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. 中南月赛F ZZY and his little friends

    Problem F: ZZY and his little friends Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 137  Solved: 70 ...

  2. NIO与Socket

    一.Socket 的使用 1.单线程Socket的使用 /** * 单线程版本 * 问题描述:只能服务单个客户端 * 解决方案:多线程版本 */ public class Socket_V1 { pu ...

  3. 关于Python的那点吐槽

    之前听到过别人有说过Python只是一个玩具做不了大项目,我当时是嗤之以鼻的,不说豆瓣这样的公司采用Python做的网站,GitHub上那么多大项目都是用Python写的,怎么能说Python只是一个 ...

  4. Window ssh免密登录到远程Linux服务器

    SSH采用的是”非对称密钥系统”,即耳熟能详的公钥私钥加密系统. 1. 基于口令的安全验证 这种方式使用用户名密码进行联机登录,一般情况下我们使用的都是这种方式.整个过程大致如下: (1)客户端发起连 ...

  5. 利用PHP QR Code生成二维码(带logo)

    转自:http://www.cnblogs.com/txw1958/p/phpqrcode.html HP QR Code是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示 ...

  6. Swiper测试

    在页面body中插入 <div class="swiper-container temp"> <div class="swiper-wrapper&qu ...

  7. 主要看思路:区域数据去重 + JavaScript一次性展示几万条数据实例代码

    近期做1功能,Gis地图 基于百度地图api , 会遇到的问题的, 如后台接口给的数据很多,大几千上万的,如果拿了数据直接渲染dom ,这滋味爽爽的. 再遇上 客户端浏览器悲催的,这卡顿就来了... ...

  8. Output assignment statements in the output statement

    package output.statement; public class OutputAssignmentStatementsInTheOutputStatement { public stati ...

  9. 弧形菜单2(动画渐入)Kotlin开发(附带java源码)

    弧形菜单2(动画渐入+Kotlin开发) 前言:基于AndroidStudio的采用Kotlin语言开发的动画渐入的弧形菜单...... 效果: 开发环境:AndroidStudio2.2.1+gra ...

  10. CSS技巧教程:margin在IE中的表现

    margin的位移方向是指margin数值为正值时候的情形,如果是负值则位移方向相反. 如上图所示:黄色子元素盒的margin-top,margin-left为负值时,如-10px,则黄色子元素盒向上 ...