<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.3.36</version>
</dependency>
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.sf.j"/>
<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.sf.j"/>
</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:/redis.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>
redis.properties
redis.host=127.0.0.1
redis.port=6379
disconf.properties
# 是否使用远程配置文件
# true(默认)会从远程获取配置 false则直接获取本地配置enable.remote.conf=true
#
# 配置服务器的 HOST,用逗号分隔 127.0.0.1:8000,127.0.0.1:8000
# conf_server_host=127.0.0.1:8080
# 版本, 请采用 X_X_X_X 格式version=1_0_0_0
# APP 请采用 产品线_服务名 格式app=disconf_demo
# 环境env=rd
# debugdebug=true
# 忽略哪些分布式配置,用逗号分隔ignore=
# 获取远程配置 重试次数,默认是3次conf_server_url_retry_times=1
# 获取远程配置 重试时休眠时间,默认是5秒conf_server_url_retry_sleep_seconds=1
@SpringBootApplication
@MapperScan("com.sf.j.mapper")
@ComponentScan("com.sf.j")
@ImportResource({"classpath:disconf.xml"})
public class SpringBootDisconfApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDisconfApplication.class, args);
}
}
JedisConfig.java
@Service
@Scope("singleton")
@PropertySource("classpath:redis.properties")
@DisconfFile(filename = "redis.properties")
public class JedisConfig {
// 代表连接地址
@Value("${redis.host}")
private String host;
// 代表连接port
@Value("${redis.port}")
private String 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 String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
SimpleDemoService.java
@Service
@Scope("singleton")
public class SimpleDemoService {
/**
* 分布式配置
*/
@Autowired
private JedisConfig jedisConfig;
/**
* 后台更改值
*/
public void changeConfig() {
System.out.println("host:"+jedisConfig.getHost());
System.out.println("port"+jedisConfig.getPort());
}
}
SimpleDemoServiceUpdateCallback.java
@Service
@DisconfUpdateService(classes = { JedisConfig.class})
public class SimpleDemoServiceUpdateCallback implements IDisconfUpdate {
@Autowired
private SimpleDemoService simpleDemoService;
public void reload() throws Exception {
simpleDemoService.changeConfig();
}
}
报错信息:
Exception in thread "main" java.lang.IllegalArgumentException:
LoggerFactory is not a Logback LoggerContext but Logback is on the
classpath. Either remove Logback or the competing implementation
(class org.slf4j.impl.Log4jLoggerFactory) Object of class
[org.slf4j.impl.Log4jLoggerFactory] must be an instance of class
ch.qos.logback.classic.LoggerContext at
org.springframework.util.Assert.isInstanceOf(Assert.java:339) at
org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:93)
at
org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSensibleDefaults(AbstractLoggingSystem.java:62)
At org.springframework.boot.logging.AbstractLoggingSystem.beforeInitialize(AbstractLoggingSystem.java:45)
at
org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:69)
at
org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:135)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98)
at
org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100)
at
org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:54)
At
org.springframework.boot.SpringApplication.run(SpringApplication.java:276)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at org.magnum.mobilecloud.video.Application.main(Application.java:30)
添加依赖:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
报错信息:
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer.parseStringValue(ReloadingPropertyPlac
eholderConfigurer.java:88)
The following method did not exist:
修改com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer.parseStringValue的源码即可:
// Fixed an issue with using obsolete api, which can't be started in spring5.
// then, business as usual. no recursive reloading placeholders please.PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
return helper.replacePlaceholders(strVal, props);
2019-10-11 11:20:31.312 INFO 4032 --- [ main] com.baidu.disconf.client.DisconfMgr : ******************************* DISCONF END *******************************
2019-10-11 11:20:31.454 INFO 4032 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-11 11:20:31.639 INFO 4032 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9998 (http) with context path ''
2019-10-11 11:20:31.641 INFO 4032 --- [ main] c.s.j.s.SpringBootDisconfApplication : Started SpringBootDisconfApplication in 11.446 seconds (JVM running for 11.879)
2019-10-11 11:32:00.668 INFO 4032 --- [ain-EventThread] c.b.d.client.watch.inner.NodeWatcher : ============GOT UPDATE EVENT WatchedEvent state:SyncConnected type:NodeDataChanged path:/disconf/Jtest_1_0_0_0_dev/file/redis.properties: (/disconf/Jtest_1_0_0_0_dev/file/redis.properties,redis.properties,配置文件)======================
2019-10-11 11:32:00.781 INFO 4032 --- [ain-EventThread] c.b.d.c.c.p.i.DisconfCoreProcessUtils : start to call class com.sf.j.service.SimpleDemoServiceUpdateCallback
host:127.0.0.101
port6379
2019-10-11 11:32:00.799 INFO 4032 --- [ain-EventThread] c.b.d.client.watch.inner.NodeWatcher : ============GOT UPDATE EVENT WatchedEvent state:SyncConnected type:NodeDataChanged path:/disconf/Jtest_1_0_0_0_dev/file/redis.properties: (/disconf/Jtest_1_0_0_0_dev/file/redis.properties,redis.properties,配置文件)======================
2019-10-11 11:32:00.907 INFO 4032 --- [ain-EventThread] c.b.d.c.c.p.i.DisconfCoreProcessUtils : start to call class com.sf.j.service.SimpleDemoServiceUpdateCallback
host:127.0.0.101
port6379