在上一篇文章Disconf实践指南:安装篇介绍了如何在本地搭建Disconf环境,下面我们介绍如何在项目中使用Disconf。由于某些功能特性对源码做了修改,所以在官方文档并没有提及。
环境基于macOS Sirerra。Windows建议安装Linux虚拟机
首先打开disconf控制台:http://localhost:8091,第一步:创建应用,awesome-project(自定);第二步:创建配置文件。创建后应用和配置文件信息如下:
以一个简单的例子演示如何使用Disconf:
假如应用有一个redis.properties配置文件,内容如下:
redis.host=128.0.0.1redis.port=6379
- 1
- 2
在disconf控制台选中刚才你创建的应用,再选择新建->新建配置文件:
创建后就可以在主界面看到刚才创建的redis.properties文件了。启动要接入disconf的应用有四部曲:
1、添加Maven依赖:
<dependency> <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> <version>2.6.35</version></dependency>
- 1
- 2
- 3
- 4
- 5
2、创建disconf.properties
# 是否使用远程配置文件# true(默认)会从远程获取配置 false则直接获取本地配置disconf.enable.remote.conf=true# 配置服务器的 HOST,用逗号分隔 127.0.0.1:8000,127.0.0.1:8000disconf.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# debugdisconf.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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
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); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
这里有几个关键点:
接口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支持公共配置。