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
d34c91fc
Commit
d34c91fc
authored
Jul 25, 2017
by
nobodyiam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add more refresh scope sample
parent
7b41718c
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 @
d34c91fc
...
@@ -25,4 +25,9 @@ public class NormalBean {
...
@@ -25,4 +25,9 @@ public class NormalBean {
public
void
setBatch
(
int
batch
)
{
public
void
setBatch
(
int
batch
)
{
this
.
batch
=
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 @
d34c91fc
...
@@ -4,6 +4,7 @@ import com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean;
...
@@ -4,6 +4,7 @@ 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.beans.factory.annotation.Value
;
import
org.springframework.cloud.context.config.annotation.RefreshScope
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
...
@@ -13,7 +14,8 @@ import org.springframework.context.annotation.Configuration;
...
@@ -13,7 +14,8 @@ 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
@Bean
(
"normalBean"
)
@RefreshScope
public
NormalBean
normalBean
(
@Value
(
"${batch:100}"
)
int
batch
)
{
public
NormalBean
normalBean
(
@Value
(
"${batch:100}"
)
int
batch
)
{
NormalBean
bean
=
new
NormalBean
();
NormalBean
bean
=
new
NormalBean
();
bean
.
setBatch
(
batch
);
bean
.
setBatch
(
batch
);
...
...
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/refresh/ApolloRefreshConfig.java
View file @
d34c91fc
...
@@ -2,6 +2,7 @@ package com.ctrip.framework.apollo.demo.spring.common.refresh;
...
@@ -2,6 +2,7 @@ package com.ctrip.framework.apollo.demo.spring.common.refresh;
import
com.ctrip.framework.apollo.Config
;
import
com.ctrip.framework.apollo.Config
;
import
com.ctrip.framework.apollo.ConfigChangeListener
;
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.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.ApolloConfig
;
...
@@ -32,21 +33,26 @@ public class ApolloRefreshConfig implements ConfigChangeListener {
...
@@ -32,21 +33,26 @@ public class ApolloRefreshConfig implements ConfigChangeListener {
private
RefreshScope
refreshScope
;
private
RefreshScope
refreshScope
;
@Autowired
@Autowired
@Qualifier
(
"refreshScopeBean"
)
private
RefreshScopeBean
refreshScopeBean
;
private
RefreshScopeBean
refreshScopeBean
;
@Autowired
private
NormalBean
normalBean
;
@PostConstruct
@PostConstruct
private
void
init
()
{
private
void
init
()
{
logger
.
info
(
refreshScopeBean
.
toString
());
config
.
addChangeListener
(
this
);
config
.
addChangeListener
(
this
);
anotherConfig
.
addChangeListener
(
this
);
anotherConfig
.
addChangeListener
(
this
);
}
}
@Override
@Override
public
void
onChange
(
ConfigChangeEvent
changeEvent
)
{
public
void
onChange
(
ConfigChangeEvent
changeEvent
)
{
//could also call refreshScope.refreshAll();
logger
.
info
(
"refreshScopeBean before refresh {}"
,
refreshScopeBean
.
toString
());
logger
.
info
(
"refreshScopeBean before refresh {}"
,
refreshScopeBean
.
toString
());
//could also call refreshScope.refreshAll();
//could also call refreshScope.refreshAll();
refreshScope
.
refresh
(
"refreshScopeBean"
);
refreshScope
.
refresh
(
"refreshScopeBean"
);
logger
.
info
(
"refreshScopeBean after refresh {}"
,
refreshScopeBean
.
toString
());
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 @
d34c91fc
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
;
...
...
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/springBootDemo/config/SampleRedisConfig.java
View file @
d34c91fc
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
;
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
;
...
@@ -11,7 +15,8 @@ import javax.annotation.PostConstruct;
...
@@ -11,7 +15,8 @@ import javax.annotation.PostConstruct;
* @author Jason Song(song_s@ctrip.com)
* @author Jason Song(song_s@ctrip.com)
*/
*/
@ConfigurationProperties
(
prefix
=
"redis.cache"
)
@ConfigurationProperties
(
prefix
=
"redis.cache"
)
@Component
@Component
(
"sampleRedisConfig"
)
@RefreshScope
public
class
SampleRedisConfig
{
public
class
SampleRedisConfig
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
SampleRedisConfig
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
SampleRedisConfig
.
class
);
...
@@ -36,4 +41,10 @@ public class SampleRedisConfig {
...
@@ -36,4 +41,10 @@ public class SampleRedisConfig {
public
void
setCommandTimeout
(
int
commandTimeout
)
{
public
void
setCommandTimeout
(
int
commandTimeout
)
{
this
.
commandTimeout
=
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 @
d34c91fc
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 @
d34c91fc
...
@@ -2,14 +2,18 @@
...
@@ -2,14 +2,18 @@
<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
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}"
/>
<property
name=
"batch"
value=
"${batch:100}"
/>
</bean>
</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