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
6e2e8ae7
Commit
6e2e8ae7
authored
Jul 28, 2016
by
Jason Song
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor local config repository strategy to load config first from upstream if possible
parent
4b487478
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
31 deletions
+32
-31
LocalFileConfigRepository.java
...framework/apollo/internals/LocalFileConfigRepository.java
+17
-5
DefaultConfigFactory.java
.../com/ctrip/framework/apollo/spi/DefaultConfigFactory.java
+2
-5
LocalFileConfigRepositoryTest.java
...ework/apollo/internals/LocalFileConfigRepositoryTest.java
+13
-21
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
View file @
6e2e8ae7
...
...
@@ -49,6 +49,10 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
* @param namespace the namespace
*/
public
LocalFileConfigRepository
(
String
namespace
)
{
this
(
namespace
,
null
);
}
public
LocalFileConfigRepository
(
String
namespace
,
ConfigRepository
upstream
)
{
m_namespace
=
namespace
;
m_container
=
ContainerLoader
.
getDefaultContainer
();
try
{
...
...
@@ -57,13 +61,17 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
Cat
.
logError
(
ex
);
throw
new
ApolloConfigException
(
"Unable to load component!"
,
ex
);
}
this
.
setLocalCacheDir
(
findLocalCacheDir
());
this
.
setLocalCacheDir
(
findLocalCacheDir
(),
false
);
this
.
setUpstreamRepository
(
upstream
);
this
.
trySync
();
}
void
setLocalCacheDir
(
File
baseDir
)
{
void
setLocalCacheDir
(
File
baseDir
,
boolean
syncImmediately
)
{
m_baseDir
=
baseDir
;
this
.
checkLocalConfigCacheDir
(
m_baseDir
);
this
.
trySync
();
if
(
syncImmediately
)
{
this
.
trySync
();
}
}
private
File
findLocalCacheDir
()
{
...
...
@@ -95,6 +103,9 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
@Override
public
void
setUpstreamRepository
(
ConfigRepository
upstreamConfigRepository
)
{
if
(
upstreamConfigRepository
==
null
)
{
return
;
}
//clear previous listener
if
(
m_upstream
!=
null
)
{
m_upstream
.
removeChangeListener
(
this
);
...
...
@@ -118,7 +129,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
@Override
protected
void
sync
()
{
//sync with upstream immediately
boolean
syncFromUpstreamResultSuccess
=
trySyncFromUpstream
();
boolean
syncFromUpstreamResultSuccess
=
trySyncFromUpstream
();
if
(
syncFromUpstreamResultSuccess
)
{
return
;
...
...
@@ -252,7 +263,8 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
}
catch
(
IOException
ex
)
{
ApolloConfigException
exception
=
new
ApolloConfigException
(
String
.
format
(
"Create local config directory %s failed"
,
baseDir
.
getAbsolutePath
()),
ex
);
String
.
format
(
"Create local config directory %s failed"
,
baseDir
.
getAbsolutePath
()),
ex
);
Cat
.
logError
(
exception
);
transaction
.
setStatus
(
exception
);
logger
.
warn
(
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java
View file @
6e2e8ae7
...
...
@@ -46,16 +46,13 @@ public class DefaultConfigFactory implements ConfigFactory {
}
LocalFileConfigRepository
createLocalConfigRepository
(
String
namespace
)
{
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
namespace
);
if
(
m_configUtil
.
isInLocalMode
())
{
logger
.
warn
(
"==== Apollo is in local mode! Won't pull configs from remote server for namespace {} ! ===="
,
namespace
);
}
else
{
localFileConfigRepository
.
setUpstreamRepository
(
createRemoteConfigRepository
(
namespace
));
return
new
LocalFileConfigRepository
(
namespace
);
}
return
localFileConfigRepository
;
return
new
LocalFileConfigRepository
(
namespace
,
createRemoteConfigRepository
(
namespace
))
;
}
RemoteConfigRepository
createRemoteConfigRepository
(
String
namespace
)
{
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java
View file @
6e2e8ae7
...
...
@@ -33,7 +33,7 @@ import static org.mockito.Mockito.when;
public
class
LocalFileConfigRepositoryTest
extends
ComponentTestCase
{
private
File
someBaseDir
;
private
String
someNamespace
;
private
ConfigRepository
fallback
Repo
;
private
ConfigRepository
upstream
Repo
;
private
Properties
someProperties
;
private
static
String
someAppId
=
"someApp"
;
private
static
String
someCluster
=
"someCluster"
;
...
...
@@ -51,8 +51,8 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
defaultKey
=
"defaultKey"
;
defaultValue
=
"defaultValue"
;
someProperties
.
setProperty
(
defaultKey
,
defaultValue
);
fallback
Repo
=
mock
(
ConfigRepository
.
class
);
when
(
fallback
Repo
.
getConfig
()).
thenReturn
(
someProperties
);
upstream
Repo
=
mock
(
ConfigRepository
.
class
);
when
(
upstream
Repo
.
getConfig
()).
thenReturn
(
someProperties
);
defineComponent
(
ConfigUtil
.
class
,
MockConfigUtil
.
class
);
}
...
...
@@ -93,7 +93,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
createLocalCachePropertyFile
(
someProperties
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
localRepo
.
setLocalCacheDir
(
someBaseDir
,
true
);
Properties
properties
=
localRepo
.
getConfig
();
assertEquals
(
someValue
,
properties
.
getProperty
(
someKey
));
...
...
@@ -108,11 +108,8 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
Files
.
write
(
defaultKey
+
"="
+
someValue
,
file
,
Charsets
.
UTF_8
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
//when fallback is set, it will try to sync from it
localRepo
.
setUpstreamRepository
(
fallbackRepo
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
,
upstreamRepo
);
localRepo
.
setLocalCacheDir
(
someBaseDir
,
true
);
Properties
properties
=
localRepo
.
getConfig
();
...
...
@@ -123,10 +120,8 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
public
void
testLoadConfigWithNoLocalFile
()
throws
Exception
{
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someNamespace
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
);
localFileConfigRepository
.
setUpstreamRepository
(
fallbackRepo
);
new
LocalFileConfigRepository
(
someNamespace
,
upstreamRepo
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
,
true
);
Properties
result
=
localFileConfigRepository
.
getConfig
();
...
...
@@ -138,17 +133,15 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
@Test
public
void
testLoadConfigWithNoLocalFileMultipleTimes
()
throws
Exception
{
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
localRepo
.
setUpstreamRepository
(
fallbackRepo
);
new
LocalFileConfigRepository
(
someNamespace
,
upstreamRepo
);
localRepo
.
setLocalCacheDir
(
someBaseDir
,
true
);
Properties
someProperties
=
localRepo
.
getConfig
();
LocalFileConfigRepository
anotherLocalRepoWithNoFallback
=
new
LocalFileConfigRepository
(
someNamespace
);
anotherLocalRepoWithNoFallback
.
setLocalCacheDir
(
someBaseDir
);
anotherLocalRepoWithNoFallback
.
setLocalCacheDir
(
someBaseDir
,
true
);
Properties
anotherProperties
=
anotherLocalRepoWithNoFallback
.
getConfig
();
...
...
@@ -163,9 +156,8 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
RepositoryChangeListener
someListener
=
mock
(
RepositoryChangeListener
.
class
);
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someNamespace
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
);
localFileConfigRepository
.
setUpstreamRepository
(
fallbackRepo
);
new
LocalFileConfigRepository
(
someNamespace
,
upstreamRepo
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
,
true
);
localFileConfigRepository
.
addChangeListener
(
someListener
);
localFileConfigRepository
.
getConfig
();
...
...
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