Commit 70a0b5f1 by Jason Song

Change default namespace to appid itself

parent 6141e812
...@@ -4,6 +4,7 @@ import com.ctrip.apollo.core.ConfigConsts; ...@@ -4,6 +4,7 @@ import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.internals.ConfigManager; import com.ctrip.apollo.internals.ConfigManager;
import com.ctrip.apollo.spi.ConfigFactory; import com.ctrip.apollo.spi.ConfigFactory;
import com.ctrip.apollo.spi.ConfigRegistry; import com.ctrip.apollo.spi.ConfigRegistry;
import com.ctrip.apollo.util.ConfigUtil;
import com.dianping.cat.Cat; import com.dianping.cat.Cat;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
...@@ -28,7 +29,7 @@ public class ConfigService { ...@@ -28,7 +29,7 @@ public class ConfigService {
* @return config instance * @return config instance
*/ */
public static Config getConfig() { public static Config getConfig() {
return getConfig(ConfigConsts.NAMESPACE_APPLICATION); return getConfig(getDefaultNamespace());
} }
/** /**
...@@ -59,8 +60,17 @@ public class ConfigService { ...@@ -59,8 +60,17 @@ public class ConfigService {
} }
} }
private static String getDefaultNamespace() {
try {
return s_instance.m_container.lookup(ConfigUtil.class).getAppId();
} catch (ComponentLookupException ex) {
Cat.logError(ex);
throw new IllegalStateException("Unable to load ConfigUtil!", ex);
}
}
public static void setConfig(Config config) { public static void setConfig(Config config) {
setConfig(ConfigConsts.NAMESPACE_APPLICATION, config); setConfig(getDefaultNamespace(), config);
} }
/** /**
...@@ -78,7 +88,7 @@ public class ConfigService { ...@@ -78,7 +88,7 @@ public class ConfigService {
} }
public static void setConfigFactory(ConfigFactory factory) { public static void setConfigFactory(ConfigFactory factory) {
setConfigFactory(ConfigConsts.NAMESPACE_APPLICATION, factory); setConfigFactory(getDefaultNamespace(), factory);
} }
/** /**
......
...@@ -81,6 +81,7 @@ public class HttpUtil { ...@@ -81,6 +81,7 @@ public class HttpUtil {
private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest, private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
Function<String, T> serializeFunction) { Function<String, T> serializeFunction) {
InputStream is = null; InputStream is = null;
int statusCode;
try { try {
HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection();
...@@ -102,7 +103,7 @@ public class HttpUtil { ...@@ -102,7 +103,7 @@ public class HttpUtil {
conn.connect(); conn.connect();
int statusCode = conn.getResponseCode(); statusCode = conn.getResponseCode();
if (statusCode == 200) { if (statusCode == 200) {
is = conn.getInputStream(); is = conn.getInputStream();
...@@ -114,9 +115,6 @@ public class HttpUtil { ...@@ -114,9 +115,6 @@ public class HttpUtil {
return new HttpResponse<>(statusCode, null); return new HttpResponse<>(statusCode, null);
} }
throw new RuntimeException(String.format("Get operation failed for %s, status code - %d",
httpRequest.getUrl(), statusCode));
} catch (Throwable ex) { } catch (Throwable ex) {
throw new RuntimeException("Could not complete get operation", ex); throw new RuntimeException("Could not complete get operation", ex);
} finally { } finally {
...@@ -128,6 +126,8 @@ public class HttpUtil { ...@@ -128,6 +126,8 @@ public class HttpUtil {
} }
} }
} }
throw new RuntimeException(String.format("Get operation failed for %s, status code - %d",
httpRequest.getUrl(), statusCode));
} }
} }
...@@ -2,6 +2,7 @@ package com.ctrip.apollo; ...@@ -2,6 +2,7 @@ package com.ctrip.apollo;
import com.ctrip.apollo.core.ConfigConsts; import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.spi.ConfigFactory; import com.ctrip.apollo.spi.ConfigFactory;
import com.ctrip.apollo.util.ConfigUtil;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -13,13 +14,16 @@ import static org.junit.Assert.assertEquals; ...@@ -13,13 +14,16 @@ import static org.junit.Assert.assertEquals;
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class ConfigServiceTest extends ComponentTestCase { public class ConfigServiceTest extends ComponentTestCase {
private static String someAppId;
@Override @Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
someAppId = "someAppId";
//as ConfigService is singleton, so we must manually clear its container //as ConfigService is singleton, so we must manually clear its container
ConfigService.setContainer(getContainer()); ConfigService.setContainer(getContainer());
defineComponent(ConfigUtil.class, MockConfigUtil.class);
} }
@Test @Test
...@@ -41,7 +45,7 @@ public class ConfigServiceTest extends ComponentTestCase { ...@@ -41,7 +45,7 @@ public class ConfigServiceTest extends ComponentTestCase {
Config config = ConfigService.getConfig(); Config config = ConfigService.getConfig();
assertEquals(ConfigConsts.NAMESPACE_APPLICATION + ":" + someKey, assertEquals(someAppId + ":" + someKey,
config.getProperty(someKey, null)); config.getProperty(someKey, null));
} }
...@@ -85,4 +89,12 @@ public class ConfigServiceTest extends ComponentTestCase { ...@@ -85,4 +89,12 @@ public class ConfigServiceTest extends ComponentTestCase {
return new MockConfig(namespace); return new MockConfig(namespace);
} }
} }
public static class MockConfigUtil extends ConfigUtil {
@Override
public String getAppId() {
return someAppId;
}
}
} }
...@@ -7,7 +7,6 @@ import com.google.common.collect.Maps; ...@@ -7,7 +7,6 @@ import com.google.common.collect.Maps;
import com.ctrip.apollo.Config; import com.ctrip.apollo.Config;
import com.ctrip.apollo.ConfigChangeListener; import com.ctrip.apollo.ConfigChangeListener;
import com.ctrip.apollo.ConfigService; import com.ctrip.apollo.ConfigService;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ApolloConfig; import com.ctrip.apollo.core.dto.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfigNotification; import com.ctrip.apollo.core.dto.ApolloConfigNotification;
import com.ctrip.apollo.core.utils.ClassLoaderUtil; import com.ctrip.apollo.core.utils.ClassLoaderUtil;
...@@ -44,13 +43,13 @@ import static org.junit.Assert.assertThat; ...@@ -44,13 +43,13 @@ import static org.junit.Assert.assertThat;
public class ConfigIntegrationTest extends BaseIntegrationTest { public class ConfigIntegrationTest extends BaseIntegrationTest {
private String someReleaseId; private String someReleaseId;
private File configDir; private File configDir;
private String someNamespace; private String defaultNamespace;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
someNamespace = ConfigConsts.NAMESPACE_APPLICATION; defaultNamespace = someAppId;
someReleaseId = "1"; someReleaseId = "1";
configDir = new File(ClassLoaderUtil.getClassPath() + "config-cache"); configDir = new File(ClassLoaderUtil.getClassPath() + "config-cache");
configDir.mkdirs(); configDir.mkdirs();
...@@ -314,7 +313,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -314,7 +313,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
private ApolloConfig assembleApolloConfig(Map<String, String> configurations) { private ApolloConfig assembleApolloConfig(Map<String, String> configurations) {
ApolloConfig apolloConfig = ApolloConfig apolloConfig =
new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseId); new ApolloConfig(someAppId, someClusterName, defaultNamespace, someReleaseId);
apolloConfig.setConfigurations(configurations); apolloConfig.setConfigurations(configurations);
...@@ -336,6 +335,6 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -336,6 +335,6 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
} }
private String assembleLocalCacheFileName() { private String assembleLocalCacheFileName() {
return String.format("%s-%s-%s.properties", someAppId, someClusterName, someNamespace); return String.format("%s-%s-%s.properties", someAppId, someClusterName, defaultNamespace);
} }
} }
...@@ -2,7 +2,6 @@ package com.ctrip.apollo.configservice.controller; ...@@ -2,7 +2,6 @@ package com.ctrip.apollo.configservice.controller;
import com.ctrip.apollo.biz.entity.Release; import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ConfigService; import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ApolloConfig; import com.ctrip.apollo.core.dto.ApolloConfig;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -29,9 +28,8 @@ public class ConfigController { ...@@ -29,9 +28,8 @@ public class ConfigController {
public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName, public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName,
@RequestParam(value = "releaseId", defaultValue = "-1") String clientSideReleaseId, @RequestParam(value = "releaseId", defaultValue = "-1") String clientSideReleaseId,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
return this //default namespace is appId
.queryConfig(appId, clusterName, ConfigConsts.NAMESPACE_APPLICATION, clientSideReleaseId, return this.queryConfig(appId, clusterName, appId, clientSideReleaseId, response);
response);
} }
@RequestMapping(value = "/{appId}/{clusterName}/{namespace}", method = RequestMethod.GET) @RequestMapping(value = "/{appId}/{clusterName}/{namespace}", method = RequestMethod.GET)
......
...@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam; ...@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.DeferredResult;
import java.util.Objects;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
...@@ -44,10 +45,15 @@ public class NotificationController { ...@@ -44,10 +45,15 @@ public class NotificationController {
public DeferredResult<ApolloConfigNotification> pollNotification( public DeferredResult<ApolloConfigNotification> pollNotification(
@RequestParam(value = "appId") String appId, @RequestParam(value = "appId") String appId,
@RequestParam(value = "cluster") String cluster, @RequestParam(value = "cluster") String cluster,
@RequestParam(value = "namespace", defaultValue = ConfigConsts.NAMESPACE_APPLICATION) String namespace, @RequestParam(value = "namespace", required = false) String namespace,
@RequestParam(value = "datacenter", required = false) String datacenter, @RequestParam(value = "datacenter", required = false) String datacenter,
@RequestParam(value = "releaseId", defaultValue = "-1") String clientSideReleaseId, @RequestParam(value = "releaseId", defaultValue = "-1") String clientSideReleaseId,
HttpServletResponse response) { HttpServletResponse response) {
//check default namespace
if (Objects.isNull(namespace)) {
namespace = appId;
}
DeferredResult<ApolloConfigNotification> deferredResult = DeferredResult<ApolloConfigNotification> deferredResult =
new DeferredResult<>(TIMEOUT); new DeferredResult<>(TIMEOUT);
String key = assembleKey(appId, cluster, namespace); String key = assembleKey(appId, cluster, namespace);
...@@ -55,13 +61,14 @@ public class NotificationController { ...@@ -55,13 +61,14 @@ public class NotificationController {
//to record all the keys related to deferredResult //to record all the keys related to deferredResult
this.deferredResultReversed.put(deferredResult, key); this.deferredResultReversed.put(deferredResult, key);
final String finalNamespace = namespace;
deferredResult.onCompletion(() -> { deferredResult.onCompletion(() -> {
logger.info("deferred result for {} {} {} completed", appId, cluster, namespace); logger.info("deferred result for {} {} {} completed", appId, cluster, finalNamespace);
deferredResults.remove(key, deferredResult); deferredResults.remove(key, deferredResult);
}); });
deferredResult.onTimeout(() -> { deferredResult.onTimeout(() -> {
logger.info("deferred result for {} {} {} timeout", appId, cluster, namespace); logger.info("deferred result for {} {} {} timeout", appId, cluster, finalNamespace);
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}); });
......
package com.ctrip.apollo.core; package com.ctrip.apollo.core;
public interface ConfigConsts { public interface ConfigConsts {
String CLUSTER_NAME_DEFAULT = "default"; String CLUSTER_NAME_DEFAULT = "default";
String NAMESPACE_APPLICATION = "application";
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment