在上一篇文章Disconf实践指南:安装篇介绍了如何在本地搭建Disconf环境,下面我们介绍如何在项目中使用Disconf。由于某些功能特性对源码做了修改,所以在官方文档并没有提及。

环境基于macOS Sirerra。Windows建议安装Linux虚拟机

首先打开disconf控制台:http://localhost:8091,第一步:创建应用,awesome-project(自定);第二步:创建配置文件。创建后应用和配置文件信息如下:

以一个简单的例子演示如何使用Disconf:

假如应用有一个redis.properties配置文件,内容如下:

redis.host=128.0.0.1
redis.port=6379

在disconf控制台选中刚才你创建的应用,再选择新建->新建配置文件:

创建后就可以在主界面看到刚才创建的redis.properties文件了。启动要接入disconf的应用有四部曲:

1、添加Maven依赖:

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

2、创建disconf.properties

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

3、创建disconf.xml

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 使用disconf必须添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<!-- 项目要被disconf扫描的包路径,多个路径逗号隔开 -->
<property name="scanPackage" value="com.rhwayfun.springboot"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean> <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload)-->
<bean id="configproperties_disconf"
class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:app.properties</value>
</list>
</property>
</bean> <bean id="propertyConfigurer"
class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="propertiesArray">
<list>
<ref bean="configproperties_disconf"/>
</list>
</property>
</bean> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>

4、redis.properties配置监听

package com.rhwayfun.springboot.disconf;

import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfFileItem;
import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; /**
* Created by rhwayfun on 2017/6/18.
*/
@Service
@Scope("singleton")
@DisconfFile(filename = "redis.properties")
@DisconfUpdateService(classes = {JedisConfig.class})
public class JedisConfig implements IDisconfUpdate { /** Logger */
private static Logger log = LoggerFactory.getLogger(JedisConfig.class); // 代表连接地址
private String host; // 代表连接port
private int port; /**
* 地址, 分布式文件配置
*
* @return
*/
@DisconfFileItem(name = "redis.host", associateField = "host")
public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} /**
* 端口, 分布式文件配置
*
* @return
*/
@DisconfFileItem(name = "redis.port", associateField = "port")
public int getPort() {
return port;
} public void setPort(int port) {
this.port = port;
} /**
* 每次更新分布式配置都会调reload方法
*
* @throws Exception
*/
@Override
public void reload() throws Exception {
log.info(">>>>>>>>>host: " + host);
} }

这里有几个关键点:

接口IDisconfUpdate:需要监听的服务都要实现该接口 
注解@DisconfFile:指定分布式配置文件,就是刚才控制台创建的redis.properties 
注解@DisconfUpdateService:标识配置更新时需要进行更新的服务,需要指定它影响的配置数据,可以是配置文件或者是配置项,比如这里JedisConfig 
注解@DisconfFileItem:分布式的配置文件中的key,比如redis.host

启动项目,成功后刷新disconf控制台,在实例列表就会出现你本地的机器了。disconf会自动帮程序获取控制台的配置了。

下面修改disconf控制台的redis.properties,将host改为127.0.0.1,观察IDEA控制台日志输出:

发现配置确实变更了,而且是实时刷新。至此在项目中使用disconf就接入完成了,接下来要做的是移除所有项目配置(properties文件),统一由disconf管理,并且让disconf支持公共配置。

Disconf实践指南:使用篇的更多相关文章

  1. Disconf实践指南:改造篇

    上一篇文章Disconf实践指南:使用篇介绍了如何在项目中应用disconf,虽然实现了分布式配置的实时刷新,但是我们希望能够去除所有的配置文件,把配置都交给disconf管理,本地只需要实现配置监听 ...

  2. Disconf实践指南:安装篇

    Disconf是百度开源出来的一款基于Zookeeper的分布式配置管理软件.目前很多公司都在使用,包括滴滴.百度.网易.顺丰等公司.通过简单的界面操作就可以动态修改配置属性,还是很方便的.使用Dis ...

  3. 如何让HTTPS站点评级达到A+? 还得看这篇HTTPS安全优化配置最佳实践指南

    0x00 前言简述 SSL/TLS 简单说明 描述: 当下越来越多的网站管理员为企业站点或自己的站点进行了SSL/TLS配置, SSL/TLS 是一种简单易懂的技术,它很容易部署及运行,但要对其进行安 ...

  4. 【转载】 Spark性能优化指南——基础篇

    转自:http://tech.meituan.com/spark-tuning-basic.html?from=timeline 前言 开发调优 调优概述 原则一:避免创建重复的RDD 原则二:尽可能 ...

  5. 【转】【技术博客】Spark性能优化指南——高级篇

    http://mp.weixin.qq.com/s?__biz=MjM5NjQ5MTI5OA==&mid=2651745207&idx=1&sn=3d70d59cede236e ...

  6. 【转】Spark性能优化指南——基础篇

    http://mp.weixin.qq.com/s?__biz=MjM5NDMwNjMzNA==&mid=2651805828&idx=1&sn=2f413828d1fdc6a ...

  7. [CoreOS 转载] CoreOS实践指南(七):Docker容器管理服务

    转载:http://www.csdn.net/article/2015-02-11/2823925 摘要:当Docker还名不见经传的时候,CoreOS创始人Alex就预见了这个项目的价值,并将其做为 ...

  8. [CoreOS 转载] CoreOS实践指南(五):分布式数据存储Etcd(上)

    转载:http://www.csdn.net/article/2015-01-22/2823659 摘要:在“漫步云端:CoreOS实践指南”系列的前几篇,分别介绍了如何架设CoreOS集群,系统服务 ...

  9. [CoreOS 转载] CoreOS实践指南(四):集群的指挥所Fleet

    转载:http://www.csdn.net/article/2015-01-14/2823554/2 摘要:CoreOS是采用了高度精简的系统内核及外围定制的操作系统.ThoughtWorks的软件 ...

随机推荐

  1. 使用微软T4 template进行代码生成

    使得软件工程高效开发的主要方法是复用.复用的宗旨是提高设计的内聚性,主要包括:函数,类,模式,组件,框架等等.而有些应用场景并都是可以直接拿来现成代码使用的,有时代码库的代码不是那么容易修改,或者根本 ...

  2. 访问路径、URL、资源加载、转码、 btn的设置、枚举

    一.URL 1.什么是URL? URL是某个资源的唯一路径,通过这个路径就能访问对应的资源 2.URL的组成 协议头://全路径 * 协议头就代表资源的类型,比如http代表网络服务器资源,ftp代表 ...

  3. Uncaught TypeError: jQuery.i18n.browserLang is not a function

    /********************************************************************* * Uncaught TypeError: jQuery. ...

  4. python3.4 使用BeautifulSoup问题

    事情 记得昨儿还是什么时候,反正是以前,肯定安装过BeautifulSoup,只不过当初可能用的是python setup.py install,这是Python2的安装.然而用Python3运行Be ...

  5. ajax前置处理实现异步请求session过期时跳转登录页面

    第一篇博文,mark一下zhq[0]. 问题描述:用户页面,当session过期或都session注销后,普通页面后端都会有过滤器,session过期Redirect到登录页面,但是ajax请求后端只 ...

  6. Android中的按键顺序打乱

    首先要找到相对应的控件,之后再来一个数组,再把数组里面的数字显示到控件上面. private void initView() { mNum0 = (Button) findViewById(R.id. ...

  7. 自定义requestAnimationFrame帧频

    requestAnimationFrame(callback)触发的callback方法会接受一个时间戳参数,所以如果不想直接跟随浏览器系统帧频的话, 就可以利用这个时间戳参数来做到自定义帧频,做法就 ...

  8. C++调用SQLServer存储过程

    同事手头的C++工程中涉及SQLServer数据库的操作需要优化,说是测试调用存储过程失败,提示: 要了C++的源码: 折腾半天,最终定位问题,问题不在C++的代码,而是SQLServer的存储过程要 ...

  9. 【IDEA】创建maven项目时,报错[FATAL_ERROR] Cannot start Maven: Cannot find JRE '1.7'

    在使用IDEA使用maven创建springMVC项目时,出现下面的错误,导致无法创建生成正常的springMVC项目结构,而只有一个pom文件: [FATAL_ERROR] Cannot start ...

  10. Uoj 441 保卫王国

    Uoj 441 保卫王国 动态 \(dp\) .今天才来写这个题. 设 \(f[u][0/1]\) 表示子树 \(u\) 中不选/选 \(u\) 时的最小权值和,显然有:\(f[u][0]=\sum ...