Commit 70a0b5f1 by Jason Song

Change default namespace to appid itself

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