1. Disconf 的安装
参考文档:
https://blog.csdn.net/u014687848/article/details/80876510
2.Disconf的使用
官方文档:
https://disconf.readthedocs.io/zh_CN/latest/index.html
博客参考文档:
https://blog.csdn.net/lby0307/article/details/80100319
第一步:添加disconf的maven支持
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.36</version>
</dependency>
第二步:创建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:8083
# 版本, 请采用 X_X_X_X 格式
disconf.version=1.0.0
# APP 请采用 产品线_服务名 格式
disconf.app=testBook
# 环境
disconf.env=qa
# 忽略哪些分布式配置,用逗号分隔
disconf.ignore=
# 获取远程配置 重试次数,默认是3次
disconf.conf_server_url_retry_times=1
# 获取远程配置 重试时休眠时间,默认是5秒
disconf.conf_server_url_retry_sleep_seconds=1
# 自定义的下载路径
disconf.user_define_download_dir=./archetype205-service/src/main/resources/config/
#disconf.enable_local_download_dir_in_class_path=truea
第三步:创建disconf的java配置类 或xml
(二选一)xml形式:
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-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 使用disconf必须添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="com.ohaotian.archetype205"/>
</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>classpath:dev.properties</value>-->
<value>classpath: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>
这个位置可以写多个,需要管理的配置文件

(二选一)java配置类 形式:
DisconfConfig.java
@Configuration
public class DisconfConfig implements BeanPostProcessor {
//使用disconf必须添加以下配置
@Bean(destroyMethod = "destroy")
public DisconfMgrBean disconfMgrBean() {
DisconfMgrBean disconfMgrBean = new DisconfMgrBean();
disconfMgrBean.setScanPackage("com.ohaotian.archetype205");
return disconfMgrBean;
}
@Bean(destroyMethod = "destroy", initMethod = "init")
public DisconfMgrBeanSecond disconfMgrBean2() {
return new DisconfMgrBeanSecond();
}
//需要托管的配置文件
@Bean(name = "reloadablePropertiesFactoryBean")
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
public ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean() {
ReloadablePropertiesFactoryBean propertiesFactoryBean = new ReloadablePropertiesFactoryBean();
propertiesFactoryBean.setSingleton(true);
// 配置文件 (这里只有application.properties)
List<String> fileNames = new ArrayList<>();
fileNames.add("classpath:application.properties");
propertiesFactoryBean.setLocations(fileNames);
return propertiesFactoryBean;
}
}
第三步:启动类加载
在 SpringbootApplication 中添加
@ImportResource({"classpath:disconf.xml"})
后,启动项目即可
可以看到,网页上写的配置文件都下载到了指定的目录下
disconf.user_define_download_dir=./archetype205-service/src/main/resources/config/
关于下载到本地的配置文件未立即生效,需要重启项目的问题
例如:
未启动项目之前,原项目里有一个原始默认的application.properties

启动项目成功后,disconf下载了最新的application.properties到 /config 目录下,但最新的application.properties未生效,项目还是以最原始的application.properties运行。

猜测可能原因 是 disconf在项目启动后进行第一次扫描,只读取了disconf.properties 的配置文件信息,application.properties未下载
启动日志关键字段
------ LOAD CONFIG START -----
********* DISCONF START FIRST SCAN ***********
之后,tomcat 启动,使用的是原始默认的application.properties
tomcat 字段信息
加载Service的bean
TomcatWebServer:90 -Tomcat initialized with port(s): 8070 (http)
再然后disconf二次扫描,项目开始读取disconf.xml文件或java配置类
启动日志 关键字段
DisconfMgr:128 -************************** DISCONF START SECOND SCAN *************************
最终,项目启动成功,配置为原始的application.properties。此时若要下载的配置文件生效,需重启项目
不用重启的解决方案
在启动类的main方法中,添加
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("disconf.xml");
或
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(DisconfConfig.class);
例如:注意是写在run()之前

启动后查查看控制台,可发现项目启动后,优先下载配置文件,然后再以最新下载的配置文件初始化tomcat。
606




被折叠的 条评论
为什么被折叠?



