Commit d34c91fc by nobodyiam

add more refresh scope sample

parent 7b41718c
......@@ -25,4 +25,9 @@ public class NormalBean {
public void setBatch(int batch) {
this.batch = batch;
}
@Override
public String toString() {
return String.format("[NormalBean] timeout: %d, batch: %d", timeout, batch);
}
}
......@@ -4,6 +4,7 @@ import com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -13,7 +14,8 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@EnableApolloConfig(value = "application", order = 10)
public class AppConfig {
@Bean
@Bean("normalBean")
@RefreshScope
public NormalBean normalBean(@Value("${batch:100}") int batch) {
NormalBean bean = new NormalBean();
bean.setBatch(batch);
......
......@@ -2,6 +2,7 @@ package com.ctrip.framework.apollo.demo.spring.common.refresh;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean;
import com.ctrip.framework.apollo.demo.spring.common.bean.RefreshScopeBean;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
......@@ -32,21 +33,26 @@ public class ApolloRefreshConfig implements ConfigChangeListener {
private RefreshScope refreshScope;
@Autowired
@Qualifier("refreshScopeBean")
private RefreshScopeBean refreshScopeBean;
@Autowired
private NormalBean normalBean;
@PostConstruct
private void init() {
logger.info(refreshScopeBean.toString());
config.addChangeListener(this);
anotherConfig.addChangeListener(this);
}
@Override
public void onChange(ConfigChangeEvent changeEvent) {
//could also call refreshScope.refreshAll();
logger.info("refreshScopeBean before refresh {}", refreshScopeBean.toString());
//could also call refreshScope.refreshAll();
refreshScope.refresh("refreshScopeBean");
logger.info("refreshScopeBean after refresh {}", refreshScopeBean.toString());
logger.info("normalBean with refresh scope before refresh {}", normalBean.toString());
refreshScope.refresh("normalBean");
logger.info("normalBean with refresh scope after refresh {}", normalBean.toString());
}
}
package com.ctrip.framework.apollo.demo.spring.javaConfigDemo.config;
import com.ctrip.framework.apollo.demo.spring.common.refresh.ApolloRefreshConfig;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
......
package com.ctrip.framework.apollo.demo.spring.springBootDemo.config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
......@@ -11,7 +15,8 @@ import javax.annotation.PostConstruct;
* @author Jason Song(song_s@ctrip.com)
*/
@ConfigurationProperties(prefix = "redis.cache")
@Component
@Component("sampleRedisConfig")
@RefreshScope
public class SampleRedisConfig {
private static final Logger logger = LoggerFactory.getLogger(SampleRedisConfig.class);
......@@ -36,4 +41,10 @@ public class SampleRedisConfig {
public void setCommandTimeout(int commandTimeout) {
this.commandTimeout = commandTimeout;
}
@Override
public String toString() {
return String.format("[SampleRedisConfig] expireSeconds: %d, clusterNodes: %s, commandTimeout: %d",
expireSeconds, clusterNodes, commandTimeout);
}
}
package com.ctrip.framework.apollo.demo.spring.springBootDemo.refresh;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.demo.spring.common.refresh.ApolloRefreshConfig;
import com.ctrip.framework.apollo.demo.spring.springBootDemo.config.SampleRedisConfig;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Component
public class SpringBootApolloRefreshConfig implements ConfigChangeListener {
private static final Logger logger = LoggerFactory.getLogger(SpringBootApolloRefreshConfig.class);
@Autowired
private ApolloRefreshConfig apolloRefreshConfig;
@Autowired
private SampleRedisConfig sampleRedisConfig;
@ApolloConfig
private Config config;
@Autowired
private RefreshScope refreshScope;
@PostConstruct
private void init() {
config.addChangeListener(this);
}
@Override
public void onChange(ConfigChangeEvent changeEvent) {
logger.info("sampleRedisConfig before refresh {}", sampleRedisConfig.toString());
refreshScope.refresh("sampleRedisConfig");
logger.info("sampleRedisConfig after refresh {}", sampleRedisConfig.toString());
}
}
......@@ -2,14 +2,18 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:apollo="http://www.ctrip.com/schema/apollo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
<apollo:config order="10"/>
<apollo:config namespaces="FX.apollo" order="11"/>
<bean class="com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean">
<bean name="normalBean" id="normalBean" class="com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean"
scope="refresh">
<aop:scoped-proxy proxy-target-class="true"/>
<property name="batch" value="${batch:100}"/>
</bean>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment