diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
index fa6562377bad87ee368bb0c6cc1522c1670a1a2e..a718e98d8c863508a89422aa0a1d9700738a2949 100644
--- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
+++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
@@ -184,6 +184,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
properties = new Properties();
properties.load(in);
+ logger.debug("Loading local config file {} successfully!", file.getAbsolutePath());
} catch (IOException ex) {
Cat.logError(ex);
throw new ApolloConfigException(String
diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java
index 18e7b28a0c38c183eb98534611a3a5a3a55c3f6d..e5e85412805e651a71f1c65b0c10dcff9e44be6a 100644
--- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java
+++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java
@@ -9,9 +9,11 @@ import com.ctrip.framework.apollo.internals.LocalFileConfigRepository;
import com.ctrip.framework.apollo.internals.PropertiesConfigFile;
import com.ctrip.framework.apollo.internals.RemoteConfigRepository;
import com.ctrip.framework.apollo.internals.XmlConfigFile;
+import com.ctrip.framework.apollo.util.ConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;
/**
@@ -20,6 +22,8 @@ import org.unidal.lookup.annotation.Named;
@Named(type = ConfigFactory.class)
public class DefaultConfigFactory implements ConfigFactory {
private static final Logger logger = LoggerFactory.getLogger(DefaultConfigFactory.class);
+ @Inject
+ private ConfigUtil m_configUtil;
@Override
public Config create(String namespace) {
@@ -44,7 +48,13 @@ public class DefaultConfigFactory implements ConfigFactory {
LocalFileConfigRepository createLocalConfigRepository(String namespace) {
LocalFileConfigRepository localFileConfigRepository =
new LocalFileConfigRepository(namespace);
- localFileConfigRepository.setUpstreamRepository(createRemoteConfigRepository(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 localFileConfigRepository;
}
diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
index cbb8338f19f807678a7a84c32526da21ce36fdab..bc7afd5103ee8600625cf38bbf77f9687f018553 100644
--- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
+++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
@@ -184,4 +184,14 @@ public class ConfigUtil {
//TODO call Framework Foundation to get the default local cache dir
return String.format("/opt/data/%s", getAppId());
}
+
+ public boolean isInLocalMode() {
+ try {
+ Env env = getApolloEnv();
+ return env == Env.LOCAL;
+ } catch (Throwable ex) {
+ //ignore
+ }
+ return false;
+ }
}
diff --git a/apollo-client/src/main/resources/META-INF/plexus/components.xml b/apollo-client/src/main/resources/META-INF/plexus/components.xml
index 515a3bae72f17f55f692c2cc551f7714bb4fc839..d0b2d333c02225a2c0a9ee6d99288ce5a27937ce 100644
--- a/apollo-client/src/main/resources/META-INF/plexus/components.xml
+++ b/apollo-client/src/main/resources/META-INF/plexus/components.xml
@@ -12,6 +12,11 @@
com.ctrip.framework.apollo.spi.ConfigFactory
com.ctrip.framework.apollo.spi.DefaultConfigFactory
+
+
+ com.ctrip.framework.apollo.util.ConfigUtil
+
+
com.ctrip.framework.apollo.spi.ConfigRegistry
diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java
index 531a5d14879a2665f6a5213c850da3fc2ef97bb1..0b007b4c7b3c34520d851aff750d9fd2506d8239 100644
--- a/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java
+++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java
@@ -50,7 +50,7 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
@BeforeClass
public static void beforeClass() throws Exception {
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();
}
@@ -159,7 +159,7 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
@Override
public Env getApolloEnv() {
- return Env.LOCAL;
+ return Env.DEV;
}
@Override
diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java
index c58b4c794441be3cdb4241de5424b2ca1f1eb6da..45861ca75163d19bc5f89604704e4b698d5feb91 100644
--- a/apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java
+++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java
@@ -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.ApolloConfigNotification;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
+import com.ctrip.framework.apollo.internals.RemoteConfigLongPollService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import org.eclipse.jetty.server.Request;
@@ -22,6 +23,7 @@ import org.eclipse.jetty.server.handler.ContextHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.springframework.test.util.ReflectionTestUtils;
import java.io.File;
import java.io.FileOutputStream;
@@ -50,6 +52,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
private File configDir;
private String defaultNamespace;
private String someOtherNamespace;
+ private RemoteConfigLongPollService remoteConfigLongPollService;
@Before
public void setUp() throws Exception {
@@ -63,11 +66,13 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
configDir.delete();
}
configDir.mkdirs();
+ remoteConfigLongPollService = lookup(RemoteConfigLongPollService.class);
}
@Override
@After
public void tearDown() throws Exception {
+ ReflectionTestUtils.invokeMethod(remoteConfigLongPollService, "stopLongPollingRefresh");
recursiveDelete(configDir);
super.tearDown();
}
@@ -309,7 +314,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
apolloConfig.getConfigurations().put(someKey, anotherValue);
- longPollFinished.get(pollTimeoutInMS * 50, TimeUnit.MILLISECONDS);
+ longPollFinished.get(5000, TimeUnit.MILLISECONDS);
assertEquals(anotherValue, config.getProperty(someKey, null));
@@ -361,8 +366,8 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
apolloConfig.getConfigurations().put(someKey, anotherValue);
- longPollFinished.get(pollTimeoutInMS * 20, TimeUnit.MILLISECONDS);
- someOtherNamespacelongPollFinished.get(pollTimeoutInMS * 20, TimeUnit.MILLISECONDS);
+ longPollFinished.get(5000, TimeUnit.MILLISECONDS);
+ someOtherNamespacelongPollFinished.get(5000, TimeUnit.MILLISECONDS);
assertEquals(anotherValue, config.getProperty(someKey, null));
assertEquals(anotherValue, someOtherConfig.getProperty(someKey, null));
diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java
index 6fd046706a7c540f9d5f5309c245af004829825a..bb00fe01939bf556a4ee5280d006c8bd69eea21b 100644
--- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java
+++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java
@@ -186,6 +186,7 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
.assembleQueryConfigUrl(someUri, someAppId, someCluster, someNamespace, null,
someApolloConfig);
+ remoteConfigLongPollService.stopLongPollingRefresh();
assertTrue(queryConfigUrl
.contains(
"http://someServer/configs/someAppId/someCluster+%20&.-_someSign/" + someNamespace));
diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java
index bc62e63829ec1c3134ae510d3e24398a9dc91734..6b758bfba3d9dc3172b31fbad226ff7b55775684 100644
--- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java
+++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java
@@ -3,13 +3,16 @@ package com.ctrip.framework.apollo.spi;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile;
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.LocalFileConfigRepository;
import com.ctrip.framework.apollo.internals.PropertiesConfigFile;
import com.ctrip.framework.apollo.internals.XmlConfigFile;
+import com.ctrip.framework.apollo.util.ConfigUtil;
import org.junit.Before;
import org.junit.Test;
+import org.springframework.test.util.ReflectionTestUtils;
import org.unidal.lookup.ComponentTestCase;
import java.util.Properties;
@@ -17,8 +20,8 @@ import java.util.Properties;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
@@ -29,10 +32,15 @@ import static org.mockito.Mockito.when;
*/
public class DefaultConfigFactoryTest extends ComponentTestCase {
private DefaultConfigFactory defaultConfigFactory;
+ private static String someAppId;
+ private static Env someEnv;
@Before
public void setUp() throws Exception {
super.setUp();
+ someAppId = "someId";
+ someEnv = Env.DEV;
+ defineComponent(ConfigUtil.class, MockConfigUtil.class);
defaultConfigFactory = spy((DefaultConfigFactory) lookup(ConfigFactory.class));
}
@@ -56,6 +64,17 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
assertEquals(someValue, result.getProperty(someKey, null));
}
+ @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 {
String someNamespace = "someName";
@@ -82,4 +101,16 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
assertEquals(anotherNamespace, xmlConfigFile.getNamespace());
}
+ public static class MockConfigUtil extends ConfigUtil {
+ @Override
+ public String getAppId() {
+ return someAppId;
+ }
+
+ @Override
+ public Env getApolloEnv() {
+ return someEnv;
+ }
+ }
+
}
diff --git a/apollo-client/src/test/resources/log4j2.xml b/apollo-client/src/test/resources/log4j2.xml
index 224a9f06610e239a5cd143a40902b84c01ff52e9..14181f27b67d63eae887dc00a5afa4e8fab923b4 100644
--- a/apollo-client/src/test/resources/log4j2.xml
+++ b/apollo-client/src/test/resources/log4j2.xml
@@ -10,7 +10,7 @@
-
+
diff --git a/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigFileController.java b/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigFileController.java
index 6577d4f28e082ae18faaf916bf5804a2f8c4dfe9..54331fc7d34ee040ae52ebbd229ad6f6f7b86c03 100644
--- a/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigFileController.java
+++ b/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigFileController.java
@@ -159,7 +159,7 @@ public class ConfigFileController implements ReleaseMessageListener {
String result = localCache.getIfPresent(cacheKey);
if (Strings.isNullOrEmpty(result)) {
- Cat.logEvent("ConfigFile-Cache-Miss", cacheKey);
+ Cat.logEvent("ConfigFile.Cache.Miss", cacheKey);
ApolloConfig apolloConfig =
configController
.queryConfig(appId, clusterName, namespace, dataCenter, "-1", clientIp,
@@ -193,7 +193,7 @@ public class ConfigFileController implements ReleaseMessageListener {
cacheKey2WatchedKeys.putAll(cacheKey, watchedKeys);
logger.debug("added cache for key: {}", cacheKey);
} else {
- Cat.logEvent("ConfigFile-Cache-Hit", cacheKey);
+ Cat.logEvent("ConfigFile.Cache.Hit", cacheKey);
}
return result;