提交 28929a3f 编写于 作者: J Jason Song

adapt for situations when no appid is provided in client side

上级 0a5424ed
......@@ -7,12 +7,13 @@ import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.MetaDomainConsts;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.enums.EnvUtils;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.foundation.Foundation;
import com.dianping.cat.Cat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Named;
import org.unidal.net.Networks;
import java.util.concurrent.TimeUnit;
......@@ -41,12 +42,15 @@ public class ConfigUtil {
/**
* Get the app id for the current application.
*
* @return the app id
* @throws IllegalStateException if app id is not set
* @return the app id or ConfigConsts.NO_APPID_PLACEHOLDER if app id is not available
*/
public String getAppId() {
String appId = Foundation.app().getAppId();
Preconditions.checkState(appId != null, "app.id is not set");
if (Strings.isNullOrEmpty(appId)) {
appId = ConfigConsts.NO_APPID_PLACEHOLDER;
logger.error("app.id is not set, apollo will only load public namespace configurations!");
Cat.logError(new ApolloConfigException("app.id is not set"));
}
return appId;
}
......
......@@ -59,19 +59,20 @@ public class ConfigController {
@RequestParam(value = "ip", required = false) String clientIp,
HttpServletResponse response) throws IOException {
String originalNamespace = namespace;
//strip out .properties suffix
namespace = namespaceUtil.filterNamespaceName(namespace);
List<Release> releases = Lists.newLinkedList();
Release currentAppRelease = loadConfig(appId, clusterName, namespace, dataCenter);
String appClusterNameLoaded = clusterName;
if (!ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
Release currentAppRelease = loadConfig(appId, clusterName, namespace, dataCenter);
if (currentAppRelease != null) {
releases.add(currentAppRelease);
//we have cluster search process, so the cluster name might be overridden
appClusterNameLoaded = currentAppRelease.getClusterName();
if (currentAppRelease != null) {
releases.add(currentAppRelease);
//we have cluster search process, so the cluster name might be overridden
appClusterNameLoaded = currentAppRelease.getClusterName();
}
}
//if namespace does not belong to this appId, should check if there is a public configuration
......@@ -116,6 +117,11 @@ public class ConfigController {
return true;
}
//if no appId is present, then no other namespace belongs to it
if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return false;
}
AppNamespace appNamespace = appNamespaceService.findOne(appId, namespaceName);
return appNamespace != null;
......
......@@ -40,6 +40,7 @@ public class WatchKeysUtil {
/**
* Assemble watch keys for the given appId, cluster, namespaces, dataCenter combination
*
* @return a multimap with namespace as the key and watch keys as the value
*/
public Multimap<String, String> assembleAllWatchKeys(String appId, String clusterName,
......@@ -91,7 +92,9 @@ public class WatchKeysUtil {
private Set<String> assembleWatchKeys(String appId, String clusterName, String namespace,
String dataCenter) {
if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return Collections.emptySet();
}
Set<String> watchedKeys = Sets.newHashSet();
//watch specified cluster config change
......@@ -124,6 +127,9 @@ public class WatchKeysUtil {
}
private Set<String> namespacesBelongToAppId(String appId, Set<String> namespaces) {
if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return Collections.emptySet();
}
List<AppNamespace> appNamespaces =
appNamespaceService.findByAppIdAndNamespaces(appId, namespaces);
......
......@@ -30,6 +30,7 @@ import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -283,8 +284,8 @@ public class ConfigControllerTest {
when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey);
ApolloConfig result = configController
.queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter,
someClientSideReleaseKey, someClientIp, someResponse);
.queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter,
someClientSideReleaseKey, someClientIp, someResponse);
assertEquals(someServerSideReleaseKey, result.getReleaseKey());
assertEquals(someAppId, result.getAppId());
......@@ -431,6 +432,50 @@ public class ConfigControllerTest {
configController.mergeReleaseConfigurations(Lists.newArrayList(someRelease));
}
@Test
public void testQueryConfigForNoAppIdPlaceHolder() throws Exception {
String someClientSideReleaseKey = "1";
HttpServletResponse someResponse = mock(HttpServletResponse.class);
String appId = ConfigConsts.NO_APPID_PLACEHOLDER;
ApolloConfig result = configController.queryConfig(appId, someClusterName,
defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
someClientIp, someResponse);
verify(releaseService, never()).findLatestActiveRelease(appId, someClusterName, defaultNamespaceName);
verify(appNamespaceService, never()).findPublicNamespaceByName(defaultNamespaceName);
assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
}
@Test
public void testQueryConfigForNoAppIdPlaceHolderWithPublicNamespace() throws Exception {
String someClientSideReleaseKey = "1";
String someServerSideReleaseKey = "2";
HttpServletResponse someResponse = mock(HttpServletResponse.class);
String somePublicAppId = "somePublicAppId";
AppNamespace somePublicAppNamespace =
assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName);
String appId = ConfigConsts.NO_APPID_PLACEHOLDER;
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName))
.thenReturn(somePublicAppNamespace);
when(releaseService.findLatestActiveRelease(somePublicAppId, someDataCenter, somePublicNamespaceName))
.thenReturn(somePublicRelease);
when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey);
ApolloConfig result = configController.queryConfig(appId, someClusterName,
somePublicNamespaceName, someDataCenter, someClientSideReleaseKey,
someClientIp, someResponse);
verify(releaseService, never()).findLatestActiveRelease(appId, someClusterName, somePublicNamespaceName);
assertEquals(someServerSideReleaseKey, result.getReleaseKey());
assertEquals(appId, result.getAppId());
assertEquals(someClusterName, result.getCluster());
assertEquals(somePublicNamespaceName, result.getNamespaceName());
assertEquals("foo", result.getConfigurations().get("apollo.public.bar"));
}
private AppNamespace assemblePublicAppNamespace(String appId, String namespace) {
return assembleAppNamespace(appId, namespace, true);
}
......
......@@ -217,4 +217,37 @@ public class ConfigControllerIntegrationTest extends AbstractBaseIntegrationTest
assertEquals("v1-file", result.getConfigurations().get("k1"));
assertEquals(null, result.getConfigurations().get("k2"));
}
@Test
public void testQueryConfigForNoAppIdPlaceHolderWithPrivateNamespace() throws Exception {
HttpStatusCodeException httpException = null;
try {
ResponseEntity<ApolloConfig> response = restTemplate
.getForEntity("{baseurl}/configs/{appId}/{clusterName}/{namespace}", ApolloConfig.class,
getHostUrl(), ConfigConsts.NO_APPID_PLACEHOLDER, someCluster, ConfigConsts.NAMESPACE_APPLICATION);
} catch (HttpStatusCodeException ex) {
httpException = ex;
}
assertEquals(HttpStatus.NOT_FOUND, httpException.getStatusCode());
}
@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryPublicConfigForNoAppIdPlaceHolder() throws Exception {
ResponseEntity<ApolloConfig> response = restTemplate
.getForEntity("{baseurl}/configs/{appId}/{clusterName}/{namespace}?dataCenter={dateCenter}",
ApolloConfig.class,
getHostUrl(), ConfigConsts.NO_APPID_PLACEHOLDER, someCluster, somePublicNamespace, someDC);
ApolloConfig result = response.getBody();
assertEquals("TEST-RELEASE-KEY4", result.getReleaseKey());
assertEquals(ConfigConsts.NO_APPID_PLACEHOLDER, result.getAppId());
assertEquals(someCluster, result.getCluster());
assertEquals(somePublicNamespace, result.getNamespaceName());
assertEquals("someDC-v1", result.getConfigurations().get("k1"));
assertEquals("someDC-v2", result.getConfigurations().get("k2"));
}
}
......@@ -75,6 +75,8 @@ public class WatchKeysUtilTest {
when(somePublicAppNamespace.getName()).thenReturn(somePublicNamespace);
when(appNamespaceService.findPublicNamespacesByNames(Sets.newHashSet(somePublicNamespace)))
.thenReturn(Lists.newArrayList(somePublicAppNamespace));
when(appNamespaceService.findPublicNamespacesByNames(Sets.newHashSet(someNamespace, somePublicNamespace)))
.thenReturn(Lists.newArrayList(somePublicAppNamespace));
ReflectionTestUtils.setField(watchKeysUtil, "appNamespaceService", appNamespaceService);
}
......@@ -141,6 +143,28 @@ public class WatchKeysUtilTest {
assertWatchKeys(somePublicAppId, clusters, somePublicNamespace, watchKeysMap.get(somePublicNamespace));
}
@Test
public void testAssembleWatchKeysForNoAppIdPlaceHolder() throws Exception {
Multimap<String, String> watchKeysMap =
watchKeysUtil.assembleAllWatchKeys(ConfigConsts.NO_APPID_PLACEHOLDER, someCluster,
Sets.newHashSet(someNamespace, anotherNamespace), someDC);
assertTrue(watchKeysMap.isEmpty());
}
@Test
public void testAssembleWatchKeysForNoAppIdPlaceHolderAndPublicNamespace() throws Exception {
Multimap<String, String> watchKeysMap =
watchKeysUtil.assembleAllWatchKeys(ConfigConsts.NO_APPID_PLACEHOLDER, someCluster,
Sets.newHashSet(someNamespace, somePublicNamespace), someDC);
Set<String> clusters = Sets.newHashSet(defaultCluster, someCluster, someDC);
assertEquals(clusters.size(), watchKeysMap.size());
assertWatchKeys(somePublicAppId, clusters, somePublicNamespace, watchKeysMap.get(somePublicNamespace));
}
private void assertWatchKeys(String appId, Set<String> clusters, String namespaceName,
Collection<String> watchedKeys) {
for (String cluster : clusters) {
......
......@@ -6,4 +6,5 @@ public interface ConfigConsts {
String CLUSTER_NAMESPACE_SEPARATOR = "+";
String APOLLO_CLUSTER_KEY = "apollo.cluster";
String CONFIG_FILE_CONTENT_KEY = "content";
String NO_APPID_PLACEHOLDER = "ApolloNoAppIdPlaceHolder";
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册