Commit 9683e5b7 by nobodyiam

simplify spring demo

parent 84053f70
package com.ctrip.framework.apollo.demo.spring.common.bean; package com.ctrip.framework.apollo.demo.spring.common.bean;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
...@@ -16,7 +11,8 @@ import javax.annotation.PostConstruct; ...@@ -16,7 +11,8 @@ import javax.annotation.PostConstruct;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Component @RefreshScope
@Component("annotatedBean")
public class AnnotatedBean { public class AnnotatedBean {
private static final Logger logger = LoggerFactory.getLogger(AnnotatedBean.class); private static final Logger logger = LoggerFactory.getLogger(AnnotatedBean.class);
...@@ -24,18 +20,10 @@ public class AnnotatedBean { ...@@ -24,18 +20,10 @@ public class AnnotatedBean {
private int timeout; private int timeout;
private int batch; private int batch;
@ApolloConfig
private Config config;
@ApolloConfig("FX.apollo")
private Config anotherConfig;
@PostConstruct @PostConstruct
void initialize() { void initialize() {
logger.info("timeout is {}", timeout); logger.info("timeout is initialized as {}", timeout);
logger.info("batch is {}", batch); logger.info("batch is initialized as {}", batch);
logger.info("Keys for config: {}", config.getPropertyNames());
logger.info("Keys for anotherConfig: {}", anotherConfig.getPropertyNames());
} }
@Value("${batch:100}") @Value("${batch:100}")
...@@ -43,28 +31,9 @@ public class AnnotatedBean { ...@@ -43,28 +31,9 @@ public class AnnotatedBean {
this.batch = batch; this.batch = batch;
} }
@ApolloConfigChangeListener("application")
private void someChangeHandler(ConfigChangeEvent changeEvent) {
logger.info("[someChangeHandler]Changes for namespace {}", changeEvent.getNamespace());
if (changeEvent.isChanged("timeout")) {
refreshTimeout();
}
}
@ApolloConfigChangeListener({"application", "FX.apollo"})
private void anotherChangeHandler(ConfigChangeEvent changeEvent) {
logger.info("[anotherChangeHandler]Changes for namespace {}", changeEvent.getNamespace());
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
logger.info("[anotherChangeHandler]Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
change.getPropertyName(), change.getOldValue(), change.getNewValue(),
change.getChangeType());
}
}
private void refreshTimeout() { @Override
//do some custom logic to update placeholder value public String toString() {
timeout = config.getIntProperty("timeout", timeout); return String.format("[AnnotatedBean] timeout: %d, batch: %d", timeout, batch);
logger.info("Refreshing timeout to {}", timeout);
} }
} }
package com.ctrip.framework.apollo.demo.spring.common.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* This bean is annotated with @RefreshScope so that it can be 'refreshed' at runtime
* @author Jason Song(song_s@ctrip.com)
*/
@RefreshScope
@Component("refreshScopeBean")
public class RefreshScopeBean {
private static final Logger logger = LoggerFactory.getLogger(RefreshScopeBean.class);
@Value("${timeout:200}")
private int timeout;
@Value("${batch:100}")
private int batch;
/**
* this method will be called when the bean is first initialized, and when it is 'refreshed' as well
*/
@PostConstruct
void initialize() {
logger.info("timeout is {}", timeout);
logger.info("batch is {}", batch);
}
@Override
public String toString() {
return String.format("[RefreshScopeBean] timeout: %d, batch: %d", timeout, batch);
}
}
package com.ctrip.framework.apollo.demo.spring.common.config; package com.ctrip.framework.apollo.demo.spring.common.config;
import com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; 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; import org.springframework.context.annotation.Configuration;
/** /**
...@@ -14,11 +10,4 @@ import org.springframework.context.annotation.Configuration; ...@@ -14,11 +10,4 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableApolloConfig(value = "application", order = 10) @EnableApolloConfig(value = "application", order = 10)
public class AppConfig { public class AppConfig {
@Bean("normalBean")
@RefreshScope
public NormalBean normalBean(@Value("${batch:100}") int batch) {
NormalBean bean = new NormalBean();
bean.setBatch(batch);
return bean;
}
} }
package com.ctrip.framework.apollo.demo.spring.common.refresh; package com.ctrip.framework.apollo.demo.spring.common.refresh;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.demo.spring.common.bean.AnnotatedBean;
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.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/** /**
* To refresh the config bean when config is changed * To refresh the config bean when config is changed
* *
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Component @Component
public class ApolloRefreshConfig implements ConfigChangeListener { public class ApolloRefreshConfig {
private static final Logger logger = LoggerFactory.getLogger(ApolloRefreshConfig.class); private static final Logger logger = LoggerFactory.getLogger(ApolloRefreshConfig.class);
@ApolloConfig
private Config config;
@ApolloConfig("FX.apollo")
private Config anotherConfig;
@Autowired @Autowired
private RefreshScope refreshScope; private RefreshScope refreshScope;
@Autowired @Autowired
private RefreshScopeBean refreshScopeBean; private AnnotatedBean annotatedBean;
@Autowired @ApolloConfigChangeListener({"application", "FX.apollo"})
private NormalBean normalBean; private void onChange(ConfigChangeEvent changeEvent) {
if (changeEvent.isChanged("timeout") || changeEvent.isChanged("batch")) {
@PostConstruct logger.info("before refresh {}", annotatedBean.toString());
private void init() { //could also call refreshScope.refreshAll();
config.addChangeListener(this); refreshScope.refresh("annotatedBean");
anotherConfig.addChangeListener(this); logger.info("after refresh {}", annotatedBean.toString());
} }
@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; 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.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
......
package com.ctrip.framework.apollo.demo.spring.springBootDemo.config; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
...@@ -25,8 +22,8 @@ public class SampleRedisConfig { ...@@ -25,8 +22,8 @@ public class SampleRedisConfig {
private int commandTimeout; private int commandTimeout;
@PostConstruct @PostConstruct
private void init() { private void initialize() {
logger.info("ConfigurationProperties sample - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}", logger.info("SampleRedisConfig initialized - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}",
expireSeconds, clusterNodes, commandTimeout); expireSeconds, clusterNodes, commandTimeout);
} }
......
package com.ctrip.framework.apollo.demo.spring.springBootDemo.refresh; 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.common.refresh.ApolloRefreshConfig;
import com.ctrip.framework.apollo.demo.spring.springBootDemo.config.SampleRedisConfig; import com.ctrip.framework.apollo.demo.spring.springBootDemo.config.SampleRedisConfig;
import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -13,13 +11,11 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -13,13 +11,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Component @Component
public class SpringBootApolloRefreshConfig implements ConfigChangeListener { public class SpringBootApolloRefreshConfig {
private static final Logger logger = LoggerFactory.getLogger(SpringBootApolloRefreshConfig.class); private static final Logger logger = LoggerFactory.getLogger(SpringBootApolloRefreshConfig.class);
@Autowired @Autowired
...@@ -28,21 +24,13 @@ public class SpringBootApolloRefreshConfig implements ConfigChangeListener { ...@@ -28,21 +24,13 @@ public class SpringBootApolloRefreshConfig implements ConfigChangeListener {
@Autowired @Autowired
private SampleRedisConfig sampleRedisConfig; private SampleRedisConfig sampleRedisConfig;
@ApolloConfig
private Config config;
@Autowired @Autowired
private RefreshScope refreshScope; private RefreshScope refreshScope;
@PostConstruct @ApolloConfigChangeListener
private void init() {
config.addChangeListener(this);
}
@Override
public void onChange(ConfigChangeEvent changeEvent) { public void onChange(ConfigChangeEvent changeEvent) {
logger.info("sampleRedisConfig before refresh {}", sampleRedisConfig.toString()); logger.info("before refresh {}", sampleRedisConfig.toString());
refreshScope.refresh("sampleRedisConfig"); refreshScope.refresh("sampleRedisConfig");
logger.info("sampleRedisConfig after refresh {}", sampleRedisConfig.toString()); logger.info("after refresh {}", sampleRedisConfig.toString());
} }
} }
package com.ctrip.framework.apollo.demo.spring.common.bean; package com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.PostConstruct;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class NormalBean { public class XmlBean {
private static final Logger logger = LoggerFactory.getLogger(NormalBean.class); private static final Logger logger = LoggerFactory.getLogger(XmlBean.class);
@Value("${timeout:200}")
private int timeout; private int timeout;
private int batch; private int batch;
@PostConstruct public void setTimeout(int timeout) {
void initialize() { logger.info("updating timeout, old value: {}, new value: {}", this.timeout, timeout);
logger.info("timeout is {}", timeout); this.timeout = timeout;
logger.info("batch is {}", batch);
} }
public void setBatch(int batch) { public void setBatch(int batch) {
logger.info("updating batch, old value: {}, new value: {}", this.batch, batch);
this.batch = batch; this.batch = batch;
} }
@Override public int getTimeout() {
public String toString() { return timeout;
return String.format("[NormalBean] timeout: %d, batch: %d", timeout, batch); }
public int getBatch() {
return batch;
} }
} }
package com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.refresh;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean.XmlBean;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class ManualRefreshUtil {
private static final Logger logger = LoggerFactory.getLogger(ManualRefreshUtil.class);
@ApolloConfig
private Config config;
@Autowired
private XmlBean xmlBean;
@ApolloConfigChangeListener
private void onChange(ConfigChangeEvent changeEvent) {
if (changeEvent.isChanged("timeout")) {
logger.info("Manually refreshing xmlBean.timeout");
xmlBean.setTimeout(config.getIntProperty("timeout", xmlBean.getTimeout()));
}
if (changeEvent.isChanged("batch")) {
logger.info("Manually refreshing xmlBean.batch");
xmlBean.setBatch(config.getIntProperty("batch", xmlBean.getBatch()));
}
}
}
...@@ -2,21 +2,20 @@ ...@@ -2,21 +2,20 @@
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:apollo="http://www.ctrip.com/schema/apollo" 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 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/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"> http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
<apollo:config order="10"/> <apollo:config order="10"/>
<apollo:config namespaces="FX.apollo" order="11"/> <apollo:config namespaces="FX.apollo" order="11"/>
<bean name="normalBean" id="normalBean" class="com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean" <bean class="com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean.XmlBean">
scope="refresh"> <property name="timeout" value="${timeout:200}"/>
<aop:scoped-proxy proxy-target-class="true"/>
<property name="batch" value="${batch:100}"/> <property name="batch" value="${batch:100}"/>
</bean> </bean>
<bean class="com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.refresh.ManualRefreshUtil"/>
<!-- to support RefreshScope --> <!-- to support RefreshScope -->
<bean class="org.springframework.cloud.autoconfigure.RefreshAutoConfiguration"/> <bean class="org.springframework.cloud.autoconfigure.RefreshAutoConfiguration"/>
......
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