forked from apolloconfig/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apolloconfig#668 from nobodyiam/refresh-scope-demo
add RefreshScope demo
- Loading branch information
Showing
26 changed files
with
136 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 0 additions & 62 deletions
62
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/bean/XmlBean.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...pollo/demo/spring/bean/AnnotatedBean.java → ...emo/spring/common/bean/AnnotatedBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...k/apollo/demo/spring/bean/NormalBean.java → ...o/demo/spring/common/bean/NormalBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...mo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/bean/RefreshScopeBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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([email protected]) | ||
*/ | ||
@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); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../demo/spring/config/AnotherAppConfig.java → ...pring/common/config/AnotherAppConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
.../apollo/demo/spring/config/AppConfig.java → .../demo/spring/common/config/AppConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
.../main/java/com/ctrip/framework/apollo/demo/spring/common/refresh/ApolloRefreshConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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.RefreshScopeBean; | ||
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.beans.factory.annotation.Qualifier; | ||
import org.springframework.cloud.context.scope.refresh.RefreshScope; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
/** | ||
* To refresh the config bean when config is changed | ||
* | ||
* @author Jason Song([email protected]) | ||
*/ | ||
@Component | ||
public class ApolloRefreshConfig implements ConfigChangeListener { | ||
private static final Logger logger = LoggerFactory.getLogger(ApolloRefreshConfig.class); | ||
|
||
@ApolloConfig | ||
private Config config; | ||
@ApolloConfig("FX.apollo") | ||
private Config anotherConfig; | ||
@Autowired | ||
private RefreshScope refreshScope; | ||
|
||
@Autowired | ||
@Qualifier("refreshScopeBean") | ||
private RefreshScopeBean refreshScopeBean; | ||
|
||
@PostConstruct | ||
private void init() { | ||
logger.info(refreshScopeBean.toString()); | ||
config.addChangeListener(this); | ||
anotherConfig.addChangeListener(this); | ||
} | ||
|
||
@Override | ||
public void onChange(ConfigChangeEvent changeEvent) { | ||
logger.info("refreshScopeBean before refresh {}", refreshScopeBean.toString()); | ||
//could also call refreshScope.refreshAll(); | ||
refreshScope.refresh("refreshScopeBean"); | ||
logger.info("refreshScopeBean after refresh {}", refreshScopeBean.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...java/com/ctrip/framework/apollo/demo/spring/javaConfigDemo/config/RefreshScopeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.ctrip.framework.apollo.demo.spring.javaConfigDemo.config; | ||
|
||
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
|
||
/** | ||
* to support RefreshScope | ||
* @author Jason Song([email protected]) | ||
*/ | ||
@Configuration | ||
@Import(RefreshAutoConfiguration.class) | ||
public class RefreshScopeConfig { | ||
} |
15 changes: 5 additions & 10 deletions
15
...o/spring/SpringBootSampleApplication.java → ...BootDemo/SpringBootSampleApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
package com.ctrip.framework.apollo.demo.spring; | ||
|
||
import com.ctrip.framework.apollo.demo.spring.config.SampleRedisConfig; | ||
package com.ctrip.framework.apollo.demo.spring.springBootDemo; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import java.util.Scanner; | ||
|
||
/** | ||
* @author Jason Song | ||
* @author Jason Song([email protected]) | ||
*/ | ||
@SpringBootApplication | ||
@SpringBootApplication(scanBasePackages = {"com.ctrip.framework.apollo.demo.spring.common", | ||
"com.ctrip.framework.apollo.demo.spring.springBootDemo" | ||
}) | ||
public class SpringBootSampleApplication { | ||
@Bean | ||
public SampleRedisConfig sampleRedisConfig() { | ||
return new SampleRedisConfig(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
new SpringApplicationBuilder(SpringBootSampleApplication.class).run(args); | ||
|
6 changes: 4 additions & 2 deletions
6
...demo/spring/config/SampleRedisConfig.java → ...ingBootDemo/config/SampleRedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
package com.ctrip.framework.apollo.demo.spring.config; | ||
package com.ctrip.framework.apollo.demo.spring.springBootDemo.config; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
/** | ||
* @author Jason Song | ||
* @author Jason Song([email protected]) | ||
*/ | ||
@ConfigurationProperties(prefix = "redis.cache") | ||
@Component | ||
public class SampleRedisConfig { | ||
private static final Logger logger = LoggerFactory.getLogger(SampleRedisConfig.class); | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...rk/apollo/demo/spring/XmlApplication.java → .../spring/xmlConfigDemo/XmlApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
<?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:context="http://www.springframework.org/schema/context" | ||
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.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.bean.XmlBean"> | ||
<property name="timeout" value="${timeout:200}"/> | ||
<bean class="com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean"> | ||
<property name="batch" value="${batch:100}"/> | ||
</bean> | ||
|
||
<!-- to support RefreshScope --> | ||
<bean class="org.springframework.cloud.autoconfigure.RefreshAutoConfiguration"/> | ||
|
||
<context:component-scan | ||
base-package="com.ctrip.framework.apollo.demo.spring.common.bean,com.ctrip.framework.apollo.demo.spring.common.refresh"/> | ||
</beans> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.