Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhangjian1949
apollo
提交
6781b213
apollo
项目概览
zhangjian1949
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6781b213
编写于
3月 25, 2016
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add auto refresh config support
上级
1a3a2842
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
45 addition
and
1 deletion
+45
-1
apollo-client/src/main/java/com/ctrip/apollo/client/ApolloConfigManager.java
...ain/java/com/ctrip/apollo/client/ApolloConfigManager.java
+32
-1
apollo-client/src/main/java/com/ctrip/apollo/client/util/ConfigUtil.java
...rc/main/java/com/ctrip/apollo/client/util/ConfigUtil.java
+13
-0
未找到文件。
apollo-client/src/main/java/com/ctrip/apollo/client/ApolloConfigManager.java
浏览文件 @
6781b213
...
...
@@ -24,6 +24,10 @@ import org.springframework.core.env.CompositePropertySource;
import
org.springframework.core.env.MutablePropertySources
;
import
java.util.List
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ThreadFactory
;
import
java.util.concurrent.atomic.AtomicLong
;
import
java.util.concurrent.atomic.AtomicReference
;
/**
...
...
@@ -37,6 +41,9 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
private
ConfigLoaderManager
configLoaderManager
;
private
ConfigurableApplicationContext
applicationContext
;
private
ConfigUtil
configUtil
;
private
ScheduledExecutorService
executorService
;
private
AtomicLong
counter
;
private
RefreshScope
scope
;
public
ApolloConfigManager
()
{
...
...
@@ -44,6 +51,15 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
throw
new
IllegalStateException
(
"There should be only one ApolloConfigManager instance!"
);
}
this
.
configLoaderManager
=
ConfigLoaderFactory
.
getInstance
().
getConfigLoaderManager
();
this
.
configUtil
=
ConfigUtil
.
getInstance
();
this
.
counter
=
new
AtomicLong
();
executorService
=
Executors
.
newScheduledThreadPool
(
1
,
new
ThreadFactory
()
{
@Override
public
Thread
newThread
(
Runnable
r
)
{
Thread
thread
=
new
Thread
(
r
,
"ApolloConfigManager-"
+
counter
.
incrementAndGet
());
return
thread
;
}
});
}
@Override
...
...
@@ -53,7 +69,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
String
.
format
(
"ApplicationContext must implement ConfigurableApplicationContext, but found: %s"
,
applicationContext
.
getClass
().
getName
()));
}
this
.
applicationContext
=
(
ConfigurableApplicationContext
)
applicationContext
;
ConfigUtil
.
getInstance
()
.
setApplicationContext
(
applicationContext
);
this
.
configUtil
.
setApplicationContext
(
applicationContext
);
}
/**
...
...
@@ -65,6 +81,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
public
void
postProcessBeanDefinitionRegistry
(
BeanDefinitionRegistry
registry
)
throws
BeansException
{
registerDependentBeans
(
registry
);
initializePropertySource
();
schedulePeriodicRefresh
();
}
/**
...
...
@@ -119,9 +136,23 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
currentPropertySources
.
addFirst
(
currentPropertySource
);
}
void
schedulePeriodicRefresh
()
{
executorService
.
scheduleAtFixedRate
(
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
updatePropertySource
();
}
catch
(
Throwable
e
)
{
logger
.
error
(
"Refreshing config failed"
,
e
);
}
}
},
configUtil
.
getRefreshInterval
(),
configUtil
.
getRefreshInterval
(),
configUtil
.
getRefreshTimeUnit
());
}
public
List
<
PropertyChange
>
updatePropertySource
()
{
PropertySourceReloadResult
result
=
this
.
configLoaderManager
.
reloadPropertySource
();
if
(
result
.
hasChanges
())
{
logger
.
info
(
"Found changes, refresh environment and refreshscope beans."
);
updateEnvironmentPropertySource
(
result
.
getPropertySource
());
refreshBeans
();
}
...
...
apollo-client/src/main/java/com/ctrip/apollo/client/util/ConfigUtil.java
浏览文件 @
6781b213
...
...
@@ -15,12 +15,17 @@ import java.net.URL;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Properties
;
import
java.util.concurrent.TimeUnit
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
ConfigUtil
{
public
static
final
String
APOLLO_PROPERTY
=
"apollo.properties"
;
//TODO read from config?
private
static
final
int
refreshInterval
=
5
;
private
static
final
TimeUnit
refreshIntervalTimeUnit
=
TimeUnit
.
MINUTES
;
private
static
ConfigUtil
configUtil
=
new
ConfigUtil
();
private
ApplicationContext
applicationContext
;
...
...
@@ -40,6 +45,14 @@ public class ConfigUtil {
this
.
applicationContext
=
applicationContext
;
}
public
int
getRefreshInterval
()
{
return
refreshInterval
;
}
public
TimeUnit
getRefreshTimeUnit
()
{
return
refreshIntervalTimeUnit
;
}
public
List
<
ApolloRegistry
>
loadApolloRegistries
()
throws
IOException
{
List
<
URL
>
resourceUrls
=
Collections
.
list
(
ClassLoaderUtil
.
getLoader
().
getResources
(
APOLLO_PROPERTY
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录