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#672 from nobodyiam/refresh-scope-demo-2
add more refresh scope sample
- Loading branch information
Showing
7 changed files
with
84 additions
and
5 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
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
3 changes: 3 additions & 0 deletions
3
...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
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,8 +1,12 @@ | ||
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 @@ | |
* @author Jason Song([email protected]) | ||
*/ | ||
@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 void setClusterNodes(String clusterNodes) { | |
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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ip/framework/apollo/demo/spring/springBootDemo/refresh/SpringBootApolloRefreshConfig.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,48 @@ | ||
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([email protected]) | ||
*/ | ||
@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()); | ||
} | ||
} |
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