Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
51363619
apollo
项目概览
wrr-cat
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
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,发现更多精彩内容 >>
提交
51363619
编写于
6月 27, 2016
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
adjust local cache directory logic
上级
43db6322
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
40 addition
and
13 deletion
+40
-13
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
...framework/apollo/internals/LocalFileConfigRepository.java
+29
-7
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
...main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
+5
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java
...ework/apollo/internals/LocalFileConfigRepositoryTest.java
+6
-6
未找到文件。
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
浏览文件 @
51363619
...
...
@@ -25,6 +25,8 @@ import java.io.IOException;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.Properties
;
/**
...
...
@@ -55,15 +57,29 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
Cat
.
logError
(
ex
);
throw
new
ApolloConfigException
(
"Unable to load component!"
,
ex
);
}
this
.
initialize
(
new
File
(
ClassLoaderUtil
.
getClassPath
()
+
CONFIG_DIR
));
this
.
setLocalCacheDir
(
findLocalCacheDir
(
));
}
void
initialize
(
File
baseDir
)
{
void
setLocalCacheDir
(
File
baseDir
)
{
m_baseDir
=
baseDir
;
this
.
checkLocalConfigCacheDir
(
m_baseDir
);
this
.
trySync
();
}
private
File
findLocalCacheDir
()
{
try
{
String
defaultCacheDir
=
m_configUtil
.
getDefaultLocalCacheDir
();
Path
path
=
Paths
.
get
(
defaultCacheDir
);
if
(
Files
.
exists
(
path
)
&&
Files
.
isWritable
(
path
))
{
return
new
File
(
defaultCacheDir
,
CONFIG_DIR
);
}
}
catch
(
Throwable
ex
)
{
//ignore
}
return
new
File
(
ClassLoaderUtil
.
getClassPath
(),
CONFIG_DIR
);
}
@Override
public
Properties
getConfig
()
{
if
(
m_fileProperties
==
null
)
{
...
...
@@ -98,6 +114,13 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
@Override
protected
void
sync
()
{
//sync with upstream immediately
boolean
syncFromUpstreamResultSuccess
=
trySyncFromUpstream
();
if
(
syncFromUpstreamResultSuccess
)
{
return
;
}
Transaction
transaction
=
Cat
.
newTransaction
(
"Apollo.ConfigService"
,
"syncLocalConfig"
);
Throwable
exception
=
null
;
try
{
...
...
@@ -113,28 +136,27 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
transaction
.
complete
();
}
//sync with fallback immediately
trySyncFromUpstream
();
if
(
m_fileProperties
==
null
)
{
throw
new
ApolloConfigException
(
"Load config from local config failed!"
,
exception
);
}
}
private
void
trySyncFromUpstream
()
{
private
boolean
trySyncFromUpstream
()
{
if
(
m_upstream
==
null
)
{
return
;
return
false
;
}
try
{
Properties
properties
=
m_upstream
.
getConfig
();
updateFileProperties
(
properties
);
return
true
;
}
catch
(
Throwable
ex
)
{
Cat
.
logError
(
ex
);
logger
.
warn
(
"Sync config from upstream repository {} failed, reason: {}"
,
m_upstream
.
getClass
(),
ExceptionUtil
.
getDetailMessage
(
ex
));
}
return
false
;
}
private
synchronized
void
updateFileProperties
(
Properties
newProperties
)
{
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
浏览文件 @
51363619
...
...
@@ -179,4 +179,9 @@ public class ConfigUtil {
public
int
getLongPollQPS
()
{
return
longPollQPS
;
}
public
String
getDefaultLocalCacheDir
()
{
//TODO call Framework Foundation to get the default local cache dir
return
String
.
format
(
"/opt/data/%s"
,
getAppId
());
}
}
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java
浏览文件 @
51363619
...
...
@@ -93,7 +93,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
createLocalCachePropertyFile
(
someProperties
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
initialize
(
someBaseDir
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
Properties
properties
=
localRepo
.
getConfig
();
assertEquals
(
someValue
,
properties
.
getProperty
(
someKey
));
...
...
@@ -109,7 +109,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
Files
.
write
(
defaultKey
+
"="
+
someValue
,
file
,
Charsets
.
UTF_8
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
initialize
(
someBaseDir
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
//when fallback is set, it will try to sync from it
localRepo
.
setUpstreamRepository
(
fallbackRepo
);
...
...
@@ -124,7 +124,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someNamespace
);
localFileConfigRepository
.
initialize
(
someBaseDir
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
);
localFileConfigRepository
.
setUpstreamRepository
(
fallbackRepo
);
...
...
@@ -139,7 +139,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
public
void
testLoadConfigWithNoLocalFileMultipleTimes
()
throws
Exception
{
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
initialize
(
someBaseDir
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
localRepo
.
setUpstreamRepository
(
fallbackRepo
);
...
...
@@ -148,7 +148,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
LocalFileConfigRepository
anotherLocalRepoWithNoFallback
=
new
LocalFileConfigRepository
(
someNamespace
);
anotherLocalRepoWithNoFallback
.
initialize
(
someBaseDir
);
anotherLocalRepoWithNoFallback
.
setLocalCacheDir
(
someBaseDir
);
Properties
anotherProperties
=
anotherLocalRepoWithNoFallback
.
getConfig
();
...
...
@@ -164,7 +164,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someNamespace
);
localFileConfigRepository
.
initialize
(
someBaseDir
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
);
localFileConfigRepository
.
setUpstreamRepository
(
fallbackRepo
);
localFileConfigRepository
.
addChangeListener
(
someListener
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录