一)、首先在pom.xml配置引入disconf的jar
<!-- 引入disconf jar -->
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.36</version>
</dependency>
二)、在resources下创建一个spring-disconf.xml文件,也可以先创建一个conf文件夹,在创建该文件,springboot项目在启动的时候会按照一定的规则去读取配置文件。
com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer是配置的默认读取类,也可以自定义一个类,然后继承该类,自己重写它的方法,编写自己配置读取方式,一般默认该类就可以了。
<?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-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 使用disconf必须添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="com.gkk.configdisconf"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>
<!-- 需要托管的配置文件 -->
<bean id="configproperties_disconf"
class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
<property name="locations">
<list>
<value>application.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>
</beans>
三)、在resources下再创建一个disconf.properties文件,该文件是分布式配置,从服务器把文件拉取到本地。
# 是否使用远程配置文件, true(默认)会从远程获取配置 false则直接获取本地配置
disconf.enable.remote.conf=true
# 配置服务器的HOST,用逗号分隔 127.0.0.1:8080,127.0.0.1:8080
disconf.conf_server_host=127.0.0.1:8080
# APP版本, 请采用 X_X_X_X 格式
disconf.version=1_0_0_0
# APP名称 请采用 项目名_服务名 格式
disconf.app=oysept-springboot-disconf
# 需要下载对应环境disconf配置,该配置可以动态配置
disconf.env=dev
disconf.debug=true
# 忽略某一些分布式配置, 用逗号分隔
disconf.ignore=
# 获取远程配置, 重试次数, 默认是3次
#disconf.conf_server_url_retry_times=3
# 获取远程配置, 重试时休眠时间, 默认是5秒
#disconf.conf_server_url_retry_sleep_seconds=5
# 用户指定配置下载到的文件夹, 远程文件下载后会放在这里
disconf.user_define_download_dir=config
# 下载的文件会被迁移到classpath根路径下,强烈建议将此选项置为 true(默认是true)
disconf.enable_local_download_dir_in_class_path=true
四)、DisconfGetter.java代码,该文件主要是为了springboot项目启动,把配置打印出来,方面查看启动的配置信息。
package com.oysept.springboot.disconf;
import com.baidu.disconf.client.usertools.IDisconfDataGetter;
import com.baidu.disconf.client.usertools.impl.DisconfDataGetterDefaultImpl;
import java.util.Map;
/**
*
* @author ouyangjun
*/
public class DisconfGetter {
private static IDisconfDataGetter iDisconfDataGetter = new DisconfDataGetterDefaultImpl();
/**
* 根据 分布式配置文件 获取该配置文件的所有数据,以 map形式呈现
* @param fileName 文件名字
* @return.
*/
public static Map<String, Object> getByFile(String fileName) {
return iDisconfDataGetter.getByFile(fileName);
}
/**
* 获取 分布式配置文件 获取该配置文件 中 某个配置项 的值
* @param fileName 文件名字
* @param fileItem 配置项
* @return.
*/
public static Object getByFileItem(String fileName, String fileItem) {
return iDisconfDataGetter.getByFileItem(fileName, fileItem);
}
/**
* 根据 分布式配置 获取其值
* @param itemName 配置项
* @return.
*/
public static Object getByItem(String itemName) {
return iDisconfDataGetter.getByItem(itemName);
}
}
最后在Application启动类中配置@ImportResource({"classpath:spring-disconf.xml"})读取配置注解。
package com.oysept.springboot;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import com.oysept.springboot.disconf.DisconfGetter;
/**
* springboot启动类
* @author ouyangjun
*/
@SpringBootApplication
@ImportResource({"classpath:spring-disconf.xml"})
public class DisconfApplication {
public static void main(String[] args) {
SpringApplication.run(DisconfApplication.class, args);
// 读取配置、方便查看启动的配置
final Map<String, Object> disconfMap = DisconfGetter.getByFile("application.properties");
disconfMap.forEach((key, value)->{
System.out.println("==>"+key+":"+value);
});
}
}
五)、运行springboot Application就会从服务器下载disconf配置了,项目整体结构。
识别二维码关注个人微信公众号
本章完结,待续,欢迎转载!
本文说明:该文章属于原创,如需转载,请标明文章转载来源!