Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
84053f70
Commit
84053f70
authored
Jul 25, 2017
by
Jason Song
Committed by
GitHub
Jul 25, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #672 from nobodyiam/refresh-scope-demo-2
add more refresh scope sample
parents
7b41718c
d34c91fc
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
84 additions
and
5 deletions
+84
-5
NormalBean.java
.../framework/apollo/demo/spring/common/bean/NormalBean.java
+5
-0
AppConfig.java
...framework/apollo/demo/spring/common/config/AppConfig.java
+3
-1
ApolloRefreshConfig.java
...pollo/demo/spring/common/refresh/ApolloRefreshConfig.java
+8
-2
RefreshScopeConfig.java
...demo/spring/javaConfigDemo/config/RefreshScopeConfig.java
+3
-0
SampleRedisConfig.java
.../demo/spring/springBootDemo/config/SampleRedisConfig.java
+12
-1
SpringBootApolloRefreshConfig.java
...springBootDemo/refresh/SpringBootApolloRefreshConfig.java
+48
-0
spring.xml
apollo-demo/src/main/resources/spring.xml
+5
-1
No files found.
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/bean/NormalBean.java
View file @
84053f70
...
...
@@ -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
);
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/config/AppConfig.java
View file @
84053f70
...
...
@@ -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
);
...
...
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/refresh/ApolloRefreshConfig.java
View file @
84053f70
...
...
@@ -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
());
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/javaConfigDemo/config/RefreshScopeConfig.java
View file @
84053f70
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
;
...
...
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/springBootDemo/config/SampleRedisConfig.java
View file @
84053f70
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
);
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/springBootDemo/refresh/SpringBootApolloRefreshConfig.java
0 → 100644
View file @
84053f70
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
());
}
}
apollo-demo/src/main/resources/spring.xml
View file @
84053f70
...
...
@@ -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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment