Skip to content

Commit

Permalink
Merge pull request apolloconfig#668 from nobodyiam/refresh-scope-demo
Browse files Browse the repository at this point in the history
add RefreshScope demo
  • Loading branch information
nobodyiam authored Jul 23, 2017
2 parents 3381b33 + ac5ba9e commit 5f7f1d3
Show file tree
Hide file tree
Showing 26 changed files with 136 additions and 444 deletions.
5 changes: 5 additions & 0 deletions apollo-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- for refresh scope demo -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<!-- required for spring 3.1.0 -->
<dependency>
<groupId>cglib</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ctrip.framework.apollo.demo.spring.bean;
package com.ctrip.framework.apollo.demo.spring.common.bean;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ctrip.framework.apollo.demo.spring.bean;
package com.ctrip.framework.apollo.demo.spring.common.bean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ctrip.framework.apollo.demo.spring.config;
package com.ctrip.framework.apollo.demo.spring.common.config;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ctrip.framework.apollo.demo.spring.config;
package com.ctrip.framework.apollo.demo.spring.common.config;

import com.ctrip.framework.apollo.demo.spring.bean.NormalBean;
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;
Expand Down
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());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
package com.ctrip.framework.apollo.demo.spring;

import com.ctrip.framework.apollo.demo.spring.bean.AnnotatedBean;
import com.ctrip.framework.apollo.demo.spring.config.AppConfig;
package com.ctrip.framework.apollo.demo.spring.javaConfigDemo;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

Expand All @@ -12,8 +9,8 @@
*/
public class AnnotationApplication {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(
AppConfig.class.getPackage().getName(), AnnotatedBean.class.getPackage().getName());
new AnnotationConfigApplicationContext("com.ctrip.framework.apollo.demo.spring.common",
"com.ctrip.framework.apollo.demo.spring.javaConfigDemo");
onKeyExit();
}

Expand Down
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 {
}
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);
Expand Down
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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ctrip.framework.apollo.demo.spring;
package com.ctrip.framework.apollo.demo.spring.xmlConfigDemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

Expand Down
11 changes: 9 additions & 2 deletions apollo-demo/src/main/resources/spring.xml
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>
26 changes: 0 additions & 26 deletions apollo-spring-boot-sample/.gitignore

This file was deleted.

39 changes: 0 additions & 39 deletions apollo-spring-boot-sample/README.md

This file was deleted.

Loading

0 comments on commit 5f7f1d3

Please sign in to comment.