Disconf實踐指南:使用篇


在上一篇文章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支持公共配置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM