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
9683e5b7
Commit
9683e5b7
authored
Jul 25, 2017
by
nobodyiam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplify spring demo
parent
84053f70
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
80 additions
and
164 deletions
+80
-164
AnnotatedBean.java
...amework/apollo/demo/spring/common/bean/AnnotatedBean.java
+8
-39
RefreshScopeBean.java
...work/apollo/demo/spring/common/bean/RefreshScopeBean.java
+0
-38
AppConfig.java
...framework/apollo/demo/spring/common/config/AppConfig.java
+0
-11
ApolloRefreshConfig.java
...pollo/demo/spring/common/refresh/ApolloRefreshConfig.java
+11
-32
RefreshScopeConfig.java
...demo/spring/javaConfigDemo/config/RefreshScopeConfig.java
+0
-3
SampleRedisConfig.java
.../demo/spring/springBootDemo/config/SampleRedisConfig.java
+2
-5
SpringBootApolloRefreshConfig.java
...springBootDemo/refresh/SpringBootApolloRefreshConfig.java
+5
-17
XmlBean.java
...mework/apollo/demo/spring/xmlConfigDemo/bean/XmlBean.java
+13
-14
ManualRefreshUtil.java
.../demo/spring/xmlConfigDemo/refresh/ManualRefreshUtil.java
+37
-0
spring.xml
apollo-demo/src/main/resources/spring.xml
+4
-5
No files found.
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/bean/AnnotatedBean.java
View file @
9683e5b7
package
com
.
ctrip
.
framework
.
apollo
.
demo
.
spring
.
common
.
bean
;
import
com.ctrip.framework.apollo.Config
;
import
com.ctrip.framework.apollo.model.ConfigChange
;
import
com.ctrip.framework.apollo.model.ConfigChangeEvent
;
import
com.ctrip.framework.apollo.spring.annotation.ApolloConfig
;
import
com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener
;
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
;
...
...
@@ -16,7 +11,8 @@ import javax.annotation.PostConstruct;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Component
@RefreshScope
@Component
(
"annotatedBean"
)
public
class
AnnotatedBean
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AnnotatedBean
.
class
);
...
...
@@ -24,18 +20,10 @@ public class AnnotatedBean {
private
int
timeout
;
private
int
batch
;
@ApolloConfig
private
Config
config
;
@ApolloConfig
(
"FX.apollo"
)
private
Config
anotherConfig
;
@PostConstruct
void
initialize
()
{
logger
.
info
(
"timeout is {}"
,
timeout
);
logger
.
info
(
"batch is {}"
,
batch
);
logger
.
info
(
"Keys for config: {}"
,
config
.
getPropertyNames
());
logger
.
info
(
"Keys for anotherConfig: {}"
,
anotherConfig
.
getPropertyNames
());
logger
.
info
(
"timeout is initialized as {}"
,
timeout
);
logger
.
info
(
"batch is initialized as {}"
,
batch
);
}
@Value
(
"${batch:100}"
)
...
...
@@ -43,28 +31,9 @@ public class AnnotatedBean {
this
.
batch
=
batch
;
}
@ApolloConfigChangeListener
(
"application"
)
private
void
someChangeHandler
(
ConfigChangeEvent
changeEvent
)
{
logger
.
info
(
"[someChangeHandler]Changes for namespace {}"
,
changeEvent
.
getNamespace
());
if
(
changeEvent
.
isChanged
(
"timeout"
))
{
refreshTimeout
();
}
}
@ApolloConfigChangeListener
({
"application"
,
"FX.apollo"
})
private
void
anotherChangeHandler
(
ConfigChangeEvent
changeEvent
)
{
logger
.
info
(
"[anotherChangeHandler]Changes for namespace {}"
,
changeEvent
.
getNamespace
());
for
(
String
key
:
changeEvent
.
changedKeys
())
{
ConfigChange
change
=
changeEvent
.
getChange
(
key
);
logger
.
info
(
"[anotherChangeHandler]Change - key: {}, oldValue: {}, newValue: {}, changeType: {}"
,
change
.
getPropertyName
(),
change
.
getOldValue
(),
change
.
getNewValue
(),
change
.
getChangeType
());
}
}
private
void
refreshTimeout
()
{
//do some custom logic to update placeholder value
timeout
=
config
.
getIntProperty
(
"timeout"
,
timeout
);
logger
.
info
(
"Refreshing timeout to {}"
,
timeout
);
@Override
public
String
toString
()
{
return
String
.
format
(
"[AnnotatedBean] timeout: %d, batch: %d"
,
timeout
,
batch
);
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/bean/RefreshScopeBean.java
deleted
100644 → 0
View file @
84053f70
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(song_s@ctrip.com)
*/
@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
);
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/config/AppConfig.java
View file @
9683e5b7
package
com
.
ctrip
.
framework
.
apollo
.
demo
.
spring
.
common
.
config
;
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
;
/**
...
...
@@ -14,11 +10,4 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@EnableApolloConfig
(
value
=
"application"
,
order
=
10
)
public
class
AppConfig
{
@Bean
(
"normalBean"
)
@RefreshScope
public
NormalBean
normalBean
(
@Value
(
"${batch:100}"
)
int
batch
)
{
NormalBean
bean
=
new
NormalBean
();
bean
.
setBatch
(
batch
);
return
bean
;
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/refresh/ApolloRefreshConfig.java
View file @
9683e5b7
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.demo.spring.common.bean.AnnotatedBean
;
import
com.ctrip.framework.apollo.model.ConfigChangeEvent
;
import
com.ctrip.framework.apollo.spring.annotation.ApolloConfig
;
import
com.ctrip.framework.apollo.spring.annotation.ApolloConfig
ChangeListener
;
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(song_s@ctrip.com)
*/
@Component
public
class
ApolloRefreshConfig
implements
ConfigChangeListener
{
public
class
ApolloRefreshConfig
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ApolloRefreshConfig
.
class
);
@ApolloConfig
private
Config
config
;
@ApolloConfig
(
"FX.apollo"
)
private
Config
anotherConfig
;
@Autowired
private
RefreshScope
refreshScope
;
@Autowired
private
RefreshScopeBean
refreshScopeBean
;
@Autowired
private
NormalBean
normalBean
;
private
AnnotatedBean
annotatedBean
;
@PostConstruct
private
void
init
()
{
config
.
addChangeListener
(
this
);
anotherConfig
.
addChangeListener
(
this
);
}
@Override
public
void
onChange
(
ConfigChangeEvent
changeEvent
)
{
//could also call refreshScope.refreshAll();
logger
.
info
(
"refreshScopeBean before refresh {}"
,
refreshScopeBean
.
toString
());
@ApolloConfigChangeListener
({
"application"
,
"FX.apollo"
})
private
void
onChange
(
ConfigChangeEvent
changeEvent
)
{
if
(
changeEvent
.
isChanged
(
"timeout"
)
||
changeEvent
.
isChanged
(
"batch"
))
{
logger
.
info
(
"before refresh {}"
,
annotatedBean
.
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
());
refreshScope
.
refresh
(
"annotatedBean"
);
logger
.
info
(
"after refresh {}"
,
annotatedBean
.
toString
());
}
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/javaConfigDemo/config/RefreshScopeConfig.java
View file @
9683e5b7
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 @
9683e5b7
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
;
...
...
@@ -25,8 +22,8 @@ public class SampleRedisConfig {
private
int
commandTimeout
;
@PostConstruct
private
void
init
()
{
logger
.
info
(
"
ConfigurationProperties sample
- expireSeconds: {}, clusterNodes: {}, commandTimeout: {}"
,
private
void
init
ialize
()
{
logger
.
info
(
"
SampleRedisConfig initialized
- expireSeconds: {}, clusterNodes: {}, commandTimeout: {}"
,
expireSeconds
,
clusterNodes
,
commandTimeout
);
}
...
...
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/springBootDemo/refresh/SpringBootApolloRefreshConfig.java
View file @
9683e5b7
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
com.ctrip.framework.apollo.spring.annotation.ApolloConfig
ChangeListener
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -13,13 +11,11 @@ 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
{
public
class
SpringBootApolloRefreshConfig
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
SpringBootApolloRefreshConfig
.
class
);
@Autowired
...
...
@@ -28,21 +24,13 @@ public class SpringBootApolloRefreshConfig implements ConfigChangeListener {
@Autowired
private
SampleRedisConfig
sampleRedisConfig
;
@ApolloConfig
private
Config
config
;
@Autowired
private
RefreshScope
refreshScope
;
@PostConstruct
private
void
init
()
{
config
.
addChangeListener
(
this
);
}
@Override
@ApolloConfigChangeListener
public
void
onChange
(
ConfigChangeEvent
changeEvent
)
{
logger
.
info
(
"
sampleRedisConfig
before refresh {}"
,
sampleRedisConfig
.
toString
());
logger
.
info
(
"before refresh {}"
,
sampleRedisConfig
.
toString
());
refreshScope
.
refresh
(
"sampleRedisConfig"
);
logger
.
info
(
"
sampleRedisConfig
after refresh {}"
,
sampleRedisConfig
.
toString
());
logger
.
info
(
"after refresh {}"
,
sampleRedisConfig
.
toString
());
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/
common/bean/Norma
lBean.java
→
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/
xmlConfigDemo/bean/Xm
lBean.java
View file @
9683e5b7
package
com
.
ctrip
.
framework
.
apollo
.
demo
.
spring
.
common
.
bean
;
package
com
.
ctrip
.
framework
.
apollo
.
demo
.
spring
.
xmlConfigDemo
.
bean
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
javax.annotation.PostConstruct
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
Norma
lBean
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
Norma
lBean
.
class
);
public
class
Xm
lBean
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
Xm
lBean
.
class
);
@Value
(
"${timeout:200}"
)
private
int
timeout
;
private
int
batch
;
@PostConstruct
void
initialize
()
{
logger
.
info
(
"timeout is {}"
,
timeout
);
logger
.
info
(
"batch is {}"
,
batch
);
public
void
setTimeout
(
int
timeout
)
{
logger
.
info
(
"updating timeout, old value: {}, new value: {}"
,
this
.
timeout
,
timeout
);
this
.
timeout
=
timeout
;
}
public
void
setBatch
(
int
batch
)
{
logger
.
info
(
"updating batch, old value: {}, new value: {}"
,
this
.
batch
,
batch
);
this
.
batch
=
batch
;
}
@Override
public
String
toString
()
{
return
String
.
format
(
"[NormalBean] timeout: %d, batch: %d"
,
timeout
,
batch
);
public
int
getTimeout
()
{
return
timeout
;
}
public
int
getBatch
()
{
return
batch
;
}
}
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/xmlConfigDemo/refresh/ManualRefreshUtil.java
0 → 100644
View file @
9683e5b7
package
com
.
ctrip
.
framework
.
apollo
.
demo
.
spring
.
xmlConfigDemo
.
refresh
;
import
com.ctrip.framework.apollo.Config
;
import
com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean.XmlBean
;
import
com.ctrip.framework.apollo.model.ConfigChangeEvent
;
import
com.ctrip.framework.apollo.spring.annotation.ApolloConfig
;
import
com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
ManualRefreshUtil
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ManualRefreshUtil
.
class
);
@ApolloConfig
private
Config
config
;
@Autowired
private
XmlBean
xmlBean
;
@ApolloConfigChangeListener
private
void
onChange
(
ConfigChangeEvent
changeEvent
)
{
if
(
changeEvent
.
isChanged
(
"timeout"
))
{
logger
.
info
(
"Manually refreshing xmlBean.timeout"
);
xmlBean
.
setTimeout
(
config
.
getIntProperty
(
"timeout"
,
xmlBean
.
getTimeout
()));
}
if
(
changeEvent
.
isChanged
(
"batch"
))
{
logger
.
info
(
"Manually refreshing xmlBean.batch"
);
xmlBean
.
setBatch
(
config
.
getIntProperty
(
"batch"
,
xmlBean
.
getBatch
()));
}
}
}
apollo-demo/src/main/resources/spring.xml
View file @
9683e5b7
...
...
@@ -2,21 +2,20 @@
<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
name=
"normalBean"
id=
"normalBean"
class=
"com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean"
scope=
"refresh"
>
<aop:scoped-proxy
proxy-target-class=
"true"
/>
<bean
class=
"com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean.XmlBean"
>
<property
name=
"timeout"
value=
"${timeout:200}"
/>
<property
name=
"batch"
value=
"${batch:100}"
/>
</bean>
<bean
class=
"com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.refresh.ManualRefreshUtil"
/>
<!-- to support RefreshScope -->
<bean
class=
"org.springframework.cloud.autoconfigure.RefreshAutoConfiguration"
/>
...
...
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