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
4d79b4f7
Commit
4d79b4f7
authored
Jul 28, 2016
by
张乐
Committed by
GitHub
Jul 28, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #352 from nobodyiam/support-client-local-mode-merge
Add local dev mode
parents
8d7991c4
bdacf5a5
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
72 additions
and
9 deletions
+72
-9
LocalFileConfigRepository.java
...framework/apollo/internals/LocalFileConfigRepository.java
+1
-0
DefaultConfigFactory.java
.../com/ctrip/framework/apollo/spi/DefaultConfigFactory.java
+10
-0
ConfigUtil.java
...main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
+10
-0
components.xml
...-client/src/main/resources/META-INF/plexus/components.xml
+5
-0
BaseIntegrationTest.java
.../java/com/ctrip/framework/apollo/BaseIntegrationTest.java
+2
-2
ConfigIntegrationTest.java
...p/framework/apollo/integration/ConfigIntegrationTest.java
+8
-3
RemoteConfigRepositoryTest.java
...ramework/apollo/internals/RemoteConfigRepositoryTest.java
+1
-0
DefaultConfigFactoryTest.java
.../ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java
+32
-1
log4j2.xml
apollo-client/src/test/resources/log4j2.xml
+1
-1
ConfigFileController.java
...apollo/configservice/controller/ConfigFileController.java
+2
-2
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
View file @
4d79b4f7
...
@@ -184,6 +184,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -184,6 +184,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
properties
=
new
Properties
();
properties
=
new
Properties
();
properties
.
load
(
in
);
properties
.
load
(
in
);
logger
.
debug
(
"Loading local config file {} successfully!"
,
file
.
getAbsolutePath
());
}
catch
(
IOException
ex
)
{
}
catch
(
IOException
ex
)
{
Cat
.
logError
(
ex
);
Cat
.
logError
(
ex
);
throw
new
ApolloConfigException
(
String
throw
new
ApolloConfigException
(
String
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java
View file @
4d79b4f7
...
@@ -9,9 +9,11 @@ import com.ctrip.framework.apollo.internals.LocalFileConfigRepository;
...
@@ -9,9 +9,11 @@ import com.ctrip.framework.apollo.internals.LocalFileConfigRepository;
import
com.ctrip.framework.apollo.internals.PropertiesConfigFile
;
import
com.ctrip.framework.apollo.internals.PropertiesConfigFile
;
import
com.ctrip.framework.apollo.internals.RemoteConfigRepository
;
import
com.ctrip.framework.apollo.internals.RemoteConfigRepository
;
import
com.ctrip.framework.apollo.internals.XmlConfigFile
;
import
com.ctrip.framework.apollo.internals.XmlConfigFile
;
import
com.ctrip.framework.apollo.util.ConfigUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.unidal.lookup.annotation.Inject
;
import
org.unidal.lookup.annotation.Named
;
import
org.unidal.lookup.annotation.Named
;
/**
/**
...
@@ -20,6 +22,8 @@ import org.unidal.lookup.annotation.Named;
...
@@ -20,6 +22,8 @@ import org.unidal.lookup.annotation.Named;
@Named
(
type
=
ConfigFactory
.
class
)
@Named
(
type
=
ConfigFactory
.
class
)
public
class
DefaultConfigFactory
implements
ConfigFactory
{
public
class
DefaultConfigFactory
implements
ConfigFactory
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
DefaultConfigFactory
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
DefaultConfigFactory
.
class
);
@Inject
private
ConfigUtil
m_configUtil
;
@Override
@Override
public
Config
create
(
String
namespace
)
{
public
Config
create
(
String
namespace
)
{
...
@@ -44,7 +48,13 @@ public class DefaultConfigFactory implements ConfigFactory {
...
@@ -44,7 +48,13 @@ public class DefaultConfigFactory implements ConfigFactory {
LocalFileConfigRepository
createLocalConfigRepository
(
String
namespace
)
{
LocalFileConfigRepository
createLocalConfigRepository
(
String
namespace
)
{
LocalFileConfigRepository
localFileConfigRepository
=
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
namespace
);
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
));
localFileConfigRepository
.
setUpstreamRepository
(
createRemoteConfigRepository
(
namespace
));
}
return
localFileConfigRepository
;
return
localFileConfigRepository
;
}
}
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
View file @
4d79b4f7
...
@@ -184,4 +184,14 @@ public class ConfigUtil {
...
@@ -184,4 +184,14 @@ public class ConfigUtil {
//TODO call Framework Foundation to get the default local cache dir
//TODO call Framework Foundation to get the default local cache dir
return
String
.
format
(
"/opt/data/%s"
,
getAppId
());
return
String
.
format
(
"/opt/data/%s"
,
getAppId
());
}
}
public
boolean
isInLocalMode
()
{
try
{
Env
env
=
getApolloEnv
();
return
env
==
Env
.
LOCAL
;
}
catch
(
Throwable
ex
)
{
//ignore
}
return
false
;
}
}
}
apollo-client/src/main/resources/META-INF/plexus/components.xml
View file @
4d79b4f7
...
@@ -12,6 +12,11 @@
...
@@ -12,6 +12,11 @@
<component>
<component>
<role>
com.ctrip.framework.apollo.spi.ConfigFactory
</role>
<role>
com.ctrip.framework.apollo.spi.ConfigFactory
</role>
<implementation>
com.ctrip.framework.apollo.spi.DefaultConfigFactory
</implementation>
<implementation>
com.ctrip.framework.apollo.spi.DefaultConfigFactory
</implementation>
<requirements>
<requirement>
<role>
com.ctrip.framework.apollo.util.ConfigUtil
</role>
</requirement>
</requirements>
</component>
</component>
<component>
<component>
<role>
com.ctrip.framework.apollo.spi.ConfigRegistry
</role>
<role>
com.ctrip.framework.apollo.spi.ConfigRegistry
</role>
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java
View file @
4d79b4f7
...
@@ -50,7 +50,7 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
...
@@ -50,7 +50,7 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
@BeforeClass
@BeforeClass
public
static
void
beforeClass
()
throws
Exception
{
public
static
void
beforeClass
()
throws
Exception
{
File
apolloEnvPropertiesFile
=
new
File
(
ClassLoaderUtil
.
getClassPath
(),
"apollo-env.properties"
);
File
apolloEnvPropertiesFile
=
new
File
(
ClassLoaderUtil
.
getClassPath
(),
"apollo-env.properties"
);
Files
.
write
(
"
local
.meta="
+
metaServiceUrl
,
apolloEnvPropertiesFile
,
Charsets
.
UTF_8
);
Files
.
write
(
"
dev
.meta="
+
metaServiceUrl
,
apolloEnvPropertiesFile
,
Charsets
.
UTF_8
);
apolloEnvPropertiesFile
.
deleteOnExit
();
apolloEnvPropertiesFile
.
deleteOnExit
();
}
}
...
@@ -159,7 +159,7 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
...
@@ -159,7 +159,7 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
@Override
@Override
public
Env
getApolloEnv
()
{
public
Env
getApolloEnv
()
{
return
Env
.
LOCAL
;
return
Env
.
DEV
;
}
}
@Override
@Override
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java
View file @
4d79b4f7
...
@@ -14,6 +14,7 @@ import com.ctrip.framework.apollo.core.ConfigConsts;
...
@@ -14,6 +14,7 @@ import com.ctrip.framework.apollo.core.ConfigConsts;
import
com.ctrip.framework.apollo.core.dto.ApolloConfig
;
import
com.ctrip.framework.apollo.core.dto.ApolloConfig
;
import
com.ctrip.framework.apollo.core.dto.ApolloConfigNotification
;
import
com.ctrip.framework.apollo.core.dto.ApolloConfigNotification
;
import
com.ctrip.framework.apollo.core.utils.ClassLoaderUtil
;
import
com.ctrip.framework.apollo.core.utils.ClassLoaderUtil
;
import
com.ctrip.framework.apollo.internals.RemoteConfigLongPollService
;
import
com.ctrip.framework.apollo.model.ConfigChangeEvent
;
import
com.ctrip.framework.apollo.model.ConfigChangeEvent
;
import
org.eclipse.jetty.server.Request
;
import
org.eclipse.jetty.server.Request
;
...
@@ -22,6 +23,7 @@ import org.eclipse.jetty.server.handler.ContextHandler;
...
@@ -22,6 +23,7 @@ import org.eclipse.jetty.server.handler.ContextHandler;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.FileOutputStream
;
...
@@ -50,6 +52,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
...
@@ -50,6 +52,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
private
File
configDir
;
private
File
configDir
;
private
String
defaultNamespace
;
private
String
defaultNamespace
;
private
String
someOtherNamespace
;
private
String
someOtherNamespace
;
private
RemoteConfigLongPollService
remoteConfigLongPollService
;
@Before
@Before
public
void
setUp
()
throws
Exception
{
public
void
setUp
()
throws
Exception
{
...
@@ -63,11 +66,13 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
...
@@ -63,11 +66,13 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
configDir
.
delete
();
configDir
.
delete
();
}
}
configDir
.
mkdirs
();
configDir
.
mkdirs
();
remoteConfigLongPollService
=
lookup
(
RemoteConfigLongPollService
.
class
);
}
}
@Override
@Override
@After
@After
public
void
tearDown
()
throws
Exception
{
public
void
tearDown
()
throws
Exception
{
ReflectionTestUtils
.
invokeMethod
(
remoteConfigLongPollService
,
"stopLongPollingRefresh"
);
recursiveDelete
(
configDir
);
recursiveDelete
(
configDir
);
super
.
tearDown
();
super
.
tearDown
();
}
}
...
@@ -309,7 +314,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
...
@@ -309,7 +314,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
apolloConfig
.
getConfigurations
().
put
(
someKey
,
anotherValue
);
apolloConfig
.
getConfigurations
().
put
(
someKey
,
anotherValue
);
longPollFinished
.
get
(
pollTimeoutInMS
*
5
0
,
TimeUnit
.
MILLISECONDS
);
longPollFinished
.
get
(
500
0
,
TimeUnit
.
MILLISECONDS
);
assertEquals
(
anotherValue
,
config
.
getProperty
(
someKey
,
null
));
assertEquals
(
anotherValue
,
config
.
getProperty
(
someKey
,
null
));
...
@@ -361,8 +366,8 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
...
@@ -361,8 +366,8 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
apolloConfig
.
getConfigurations
().
put
(
someKey
,
anotherValue
);
apolloConfig
.
getConfigurations
().
put
(
someKey
,
anotherValue
);
longPollFinished
.
get
(
pollTimeoutInMS
*
2
0
,
TimeUnit
.
MILLISECONDS
);
longPollFinished
.
get
(
500
0
,
TimeUnit
.
MILLISECONDS
);
someOtherNamespacelongPollFinished
.
get
(
pollTimeoutInMS
*
2
0
,
TimeUnit
.
MILLISECONDS
);
someOtherNamespacelongPollFinished
.
get
(
500
0
,
TimeUnit
.
MILLISECONDS
);
assertEquals
(
anotherValue
,
config
.
getProperty
(
someKey
,
null
));
assertEquals
(
anotherValue
,
config
.
getProperty
(
someKey
,
null
));
assertEquals
(
anotherValue
,
someOtherConfig
.
getProperty
(
someKey
,
null
));
assertEquals
(
anotherValue
,
someOtherConfig
.
getProperty
(
someKey
,
null
));
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java
View file @
4d79b4f7
...
@@ -186,6 +186,7 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
...
@@ -186,6 +186,7 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
.
assembleQueryConfigUrl
(
someUri
,
someAppId
,
someCluster
,
someNamespace
,
null
,
.
assembleQueryConfigUrl
(
someUri
,
someAppId
,
someCluster
,
someNamespace
,
null
,
someApolloConfig
);
someApolloConfig
);
remoteConfigLongPollService
.
stopLongPollingRefresh
();
assertTrue
(
queryConfigUrl
assertTrue
(
queryConfigUrl
.
contains
(
.
contains
(
"http://someServer/configs/someAppId/someCluster+%20&.-_someSign/"
+
someNamespace
));
"http://someServer/configs/someAppId/someCluster+%20&.-_someSign/"
+
someNamespace
));
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java
View file @
4d79b4f7
...
@@ -3,13 +3,16 @@ package com.ctrip.framework.apollo.spi;
...
@@ -3,13 +3,16 @@ package com.ctrip.framework.apollo.spi;
import
com.ctrip.framework.apollo.Config
;
import
com.ctrip.framework.apollo.Config
;
import
com.ctrip.framework.apollo.ConfigFile
;
import
com.ctrip.framework.apollo.ConfigFile
;
import
com.ctrip.framework.apollo.core.enums.ConfigFileFormat
;
import
com.ctrip.framework.apollo.core.enums.ConfigFileFormat
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.internals.DefaultConfig
;
import
com.ctrip.framework.apollo.internals.DefaultConfig
;
import
com.ctrip.framework.apollo.internals.LocalFileConfigRepository
;
import
com.ctrip.framework.apollo.internals.LocalFileConfigRepository
;
import
com.ctrip.framework.apollo.internals.PropertiesConfigFile
;
import
com.ctrip.framework.apollo.internals.PropertiesConfigFile
;
import
com.ctrip.framework.apollo.internals.XmlConfigFile
;
import
com.ctrip.framework.apollo.internals.XmlConfigFile
;
import
com.ctrip.framework.apollo.util.ConfigUtil
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.unidal.lookup.ComponentTestCase
;
import
org.unidal.lookup.ComponentTestCase
;
import
java.util.Properties
;
import
java.util.Properties
;
...
@@ -17,8 +20,8 @@ import java.util.Properties;
...
@@ -17,8 +20,8 @@ import java.util.Properties;
import
static
org
.
hamcrest
.
core
.
Is
.
is
;
import
static
org
.
hamcrest
.
core
.
Is
.
is
;
import
static
org
.
hamcrest
.
core
.
IsInstanceOf
.
instanceOf
;
import
static
org
.
hamcrest
.
core
.
IsInstanceOf
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
spy
;
import
static
org
.
mockito
.
Mockito
.
spy
;
...
@@ -29,10 +32,15 @@ import static org.mockito.Mockito.when;
...
@@ -29,10 +32,15 @@ import static org.mockito.Mockito.when;
*/
*/
public
class
DefaultConfigFactoryTest
extends
ComponentTestCase
{
public
class
DefaultConfigFactoryTest
extends
ComponentTestCase
{
private
DefaultConfigFactory
defaultConfigFactory
;
private
DefaultConfigFactory
defaultConfigFactory
;
private
static
String
someAppId
;
private
static
Env
someEnv
;
@Before
@Before
public
void
setUp
()
throws
Exception
{
public
void
setUp
()
throws
Exception
{
super
.
setUp
();
super
.
setUp
();
someAppId
=
"someId"
;
someEnv
=
Env
.
DEV
;
defineComponent
(
ConfigUtil
.
class
,
MockConfigUtil
.
class
);
defaultConfigFactory
=
spy
((
DefaultConfigFactory
)
lookup
(
ConfigFactory
.
class
));
defaultConfigFactory
=
spy
((
DefaultConfigFactory
)
lookup
(
ConfigFactory
.
class
));
}
}
...
@@ -57,6 +65,17 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
...
@@ -57,6 +65,17 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
}
}
@Test
@Test
public
void
testCreateLocalConfigRepositoryInLocalDev
()
throws
Exception
{
String
someNamespace
=
"someName"
;
someEnv
=
Env
.
LOCAL
;
LocalFileConfigRepository
localFileConfigRepository
=
defaultConfigFactory
.
createLocalConfigRepository
(
someNamespace
);
assertNull
(
ReflectionTestUtils
.
getField
(
localFileConfigRepository
,
"m_upstream"
));
}
@Test
public
void
testCreateConfigFile
()
throws
Exception
{
public
void
testCreateConfigFile
()
throws
Exception
{
String
someNamespace
=
"someName"
;
String
someNamespace
=
"someName"
;
String
anotherNamespace
=
"anotherName"
;
String
anotherNamespace
=
"anotherName"
;
...
@@ -82,4 +101,16 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
...
@@ -82,4 +101,16 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
assertEquals
(
anotherNamespace
,
xmlConfigFile
.
getNamespace
());
assertEquals
(
anotherNamespace
,
xmlConfigFile
.
getNamespace
());
}
}
public
static
class
MockConfigUtil
extends
ConfigUtil
{
@Override
public
String
getAppId
()
{
return
someAppId
;
}
@Override
public
Env
getApolloEnv
()
{
return
someEnv
;
}
}
}
}
apollo-client/src/test/resources/log4j2.xml
View file @
4d79b4f7
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
</appenders>
</appenders>
<loggers>
<loggers>
<logger
name=
"com.ctrip.framework.apollo"
additivity=
"false"
level=
"trace"
>
<logger
name=
"com.ctrip.framework.apollo"
additivity=
"false"
level=
"trace"
>
<AppenderRef
ref=
"Async"
level=
"
INFO
"
/>
<AppenderRef
ref=
"Async"
level=
"
DEBUG
"
/>
</logger>
</logger>
<root
level=
"INFO"
>
<root
level=
"INFO"
>
<AppenderRef
ref=
"Async"
/>
<AppenderRef
ref=
"Async"
/>
...
...
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigFileController.java
View file @
4d79b4f7
...
@@ -159,7 +159,7 @@ public class ConfigFileController implements ReleaseMessageListener {
...
@@ -159,7 +159,7 @@ public class ConfigFileController implements ReleaseMessageListener {
String
result
=
localCache
.
getIfPresent
(
cacheKey
);
String
result
=
localCache
.
getIfPresent
(
cacheKey
);
if
(
Strings
.
isNullOrEmpty
(
result
))
{
if
(
Strings
.
isNullOrEmpty
(
result
))
{
Cat
.
logEvent
(
"ConfigFile
-Cache-
Miss"
,
cacheKey
);
Cat
.
logEvent
(
"ConfigFile
.Cache.
Miss"
,
cacheKey
);
ApolloConfig
apolloConfig
=
ApolloConfig
apolloConfig
=
configController
configController
.
queryConfig
(
appId
,
clusterName
,
namespace
,
dataCenter
,
"-1"
,
clientIp
,
.
queryConfig
(
appId
,
clusterName
,
namespace
,
dataCenter
,
"-1"
,
clientIp
,
...
@@ -193,7 +193,7 @@ public class ConfigFileController implements ReleaseMessageListener {
...
@@ -193,7 +193,7 @@ public class ConfigFileController implements ReleaseMessageListener {
cacheKey2WatchedKeys
.
putAll
(
cacheKey
,
watchedKeys
);
cacheKey2WatchedKeys
.
putAll
(
cacheKey
,
watchedKeys
);
logger
.
debug
(
"added cache for key: {}"
,
cacheKey
);
logger
.
debug
(
"added cache for key: {}"
,
cacheKey
);
}
else
{
}
else
{
Cat
.
logEvent
(
"ConfigFile
-Cache-
Hit"
,
cacheKey
);
Cat
.
logEvent
(
"ConfigFile
.Cache.
Hit"
,
cacheKey
);
}
}
return
result
;
return
result
;
...
...
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