disconf实践(二)基于XML的分布式配置文件管理,不会自动reload
上一篇博文介绍了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的更多相关文章
- disconf实践(三)基于XML的分布式配置文件管理,自动reload
上一篇介绍了基于xml的非自动reload的分布式配置文件管理,这一篇介绍自动reload的方式(基于disconf实践二). 1. 修改RedisConfig.java package org.sp ...
- disconf实践(四)基于注解的分布式配置文件管理,自动reload
上一篇讲解了基于xml的自动reload的分布式配置文件管理,这一篇讲解基于注解的自动reload的方式(基于disconf实践二). 1. 修改spring配置文件 <?xml version ...
- 基于zookeeper实现分布式配置中心(二)
上一篇(基于zookeeper实现分布式配置中心(一))讲述了zookeeper相关概念和工作原理.接下来根据zookeeper的特性,简单实现一个分布式配置中心. 配置中心的优势 1.各环境配置集中 ...
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
- 基于XML的AOP配置
创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...
- 01Spring基于xml的IOC配置--入门
01Spring基于xml的IOC配置 1.创建一个普通的maven工程 1.1 选择maven,不用骨架,点击下一步. 1.2 填写GroupId.ArtifactId.Version.填完点击下一 ...
- Spring中AOP的基于xml开发和配置
pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- 基于zookeeper实现分布式配置中心(一)
最近在学习zookeeper,发现zk真的是一个优秀的中间件.在分布式环境下,可以高效解决数据管理问题.在学习的过程中,要深入zk的工作原理,并根据其特性做一些简单的分布式环境下数据管理工具.本文首先 ...
- mybatis学习一:基于xml与注解配置入门实例与问题
注:本case参考自:http://www.cnblogs.com/ysocean/p/7277545.html 一:Mybatis的介绍: MyBatis 本是apache的一个开源项目iBatis ...
随机推荐
- Hunger Snake3
- poj 2992 Divisors (素数打表+阶乘因子求解)
Divisors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9617 Accepted: 2821 Descript ...
- HBase入门教程
# 背景 最近看到公司一个项目用到hbase, 之前也一直想看下hbase.个人理解Hbase作为一个nosql数据库,逻辑模型感觉跟关系型数据库有点类似.一个table,有row即行,列.不过列是一 ...
- 零基础学python习题 - 进入python的世界
1. python拥有以下特性:面向对象的特性.动态性.内置的数据结构.简单性.健壮性.跨平台性.可扩展性.强类型语言.应用广泛 2. python 需要 编译 3. 以下不属于python内置数据 ...
- CF17E Palisection
题意 给定一个长度为n的小写字母串.问你有多少对相交的回文子串(包含也算相交) 相交的回文子串个数 \(mod\ 51123987\) Sol 求相交的回文子串不太好求 考虑用总数减去不相交的回文串个 ...
- MSSQL中的表变量
最近在看<Microsoft SQL Server2005技术内幕:T-SQL程序设计> 1.表变量的事务上下文中提到,表变量不受外部事务回滚影响. 举个例子: DECLARE @TA ...
- drupal7 profile2模块获取个人信息
一.问题背景: 用profile2模块,扩展个人信息,增加了“手机号”等信息,一些地方想要获取当前用户的手机号 二.解决办法: 用profile2自带的方法:profile2_load_by_user ...
- LeetCode 531----Lonely Pixel I----两种算法之间性能的比较
Lonely Pixel I 两种算法之间的性能比较 今天参加LeetCode Weekly Contest 22,第二题 "Lonely Pixel I" 问题描述如下: Giv ...
- Same Tree 比较两个二叉树是否完全相同
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 有关使用 iview 表单验证的问题
Vue的UI解决框架,element-UI, iview-UI 有关表单验证使用的是同一个插件,async-validator,有关这个插件的用法就不做赘述,但是在iview表单的使用中可能会用到验证 ...