Commit ea3563c5 by lepdou

Merge pull request #229 from nobodyiam/log-client-ip

add ip for client-server interactions so that we could have more info…
parents 64a1d15b fd0f2ca4
package com.ctrip.framework.apollo.internals;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.escape.Escaper;
......@@ -23,7 +24,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;
import org.unidal.net.Networks;
import java.lang.reflect.Type;
import java.util.List;
......@@ -138,18 +138,21 @@ public class ConfigServiceLocator implements Initializable {
}
}
throw new RuntimeException("Get config services failed", exception);
throw new RuntimeException(
String.format("Get config services failed from %s", url), exception);
}
private String assembleMetaServiceUrl() {
String domainName = m_configUtil.getMetaServerDomainName();
String appId = m_configUtil.getAppId();
String localIp = Networks.forIp().getLocalHostAddress();
String localIp = m_configUtil.getLocalIp();
Escaper escaper = UrlEscapers.urlPathSegmentEscaper();
Map<String, String> queryParams = Maps.newHashMap();
queryParams.put("appId", escaper.escape(appId));
queryParams.put("ip", escaper.escape(localIp));
if (!Strings.isNullOrEmpty(localIp)) {
queryParams.put("ip", escaper.escape(localIp));
}
return domainName + "/services/config?" + MAP_JOINER.join(queryParams);
}
......
......@@ -181,7 +181,8 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
ApolloConfig result = response.getBody();
Cat.logEvent("Apollo.Client.ConfigLoaded." + result.getNamespaceName(), result.getReleaseKey());
Cat.logEvent("Apollo.Client.ConfigLoaded." + result.getNamespaceName(),
result.getReleaseKey());
logger.debug("Loaded config for {}: {}", m_namespace, result);
return result;
......@@ -227,6 +228,11 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
queryParams.put("dataCenter", escaper.escape(dataCenter));
}
String localIp = m_configUtil.getLocalIp();
if (!Strings.isNullOrEmpty(localIp)) {
queryParams.put("ip", escaper.escape(localIp));
}
String pathExpanded = String.format(path, pathParams.toArray());
if (!queryParams.isEmpty()) {
......@@ -329,6 +335,10 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
if (!Strings.isNullOrEmpty(dataCenter)) {
queryParams.put("dataCenter", escaper.escape(dataCenter));
}
String localIp = m_configUtil.getLocalIp();
if (!Strings.isNullOrEmpty(localIp)) {
queryParams.put("ip", escaper.escape(localIp));
}
String params = MAP_JOINER.join(queryParams);
if (!uri.endsWith("/")) {
......
......@@ -9,6 +9,7 @@ import com.ctrip.framework.apollo.core.enums.EnvUtils;
import com.ctrip.framework.foundation.Foundation;
import org.unidal.lookup.annotation.Named;
import org.unidal.net.Networks;
import java.util.concurrent.TimeUnit;
......@@ -79,6 +80,10 @@ public class ConfigUtil {
return env;
}
public String getLocalIp() {
return Networks.forIp().getLocalHostAddress();
}
public String getMetaServerDomainName() {
return MetaDomainConsts.getDomain(getApolloEnv());
}
......
......@@ -48,20 +48,12 @@ public class ConfigController {
}.getType();
private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR);
@RequestMapping(value = "/{appId}/{clusterName}", method = RequestMethod.GET)
public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName,
@RequestParam(value = "dataCenter", required = false) String dataCenter,
@RequestParam(value = "releaseKey", defaultValue = "-1") String clientSideReleaseKey,
HttpServletResponse response) throws IOException {
return this.queryConfig(appId, clusterName, ConfigConsts.NAMESPACE_DEFAULT, dataCenter,
clientSideReleaseKey, response);
}
@RequestMapping(value = "/{appId}/{clusterName}/{namespace}", method = RequestMethod.GET)
public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName,
@PathVariable String namespace,
@RequestParam(value = "dataCenter", required = false) String dataCenter,
@RequestParam(value = "releaseKey", defaultValue = "-1") String clientSideReleaseKey,
@RequestParam(value = "ip", required = false) String clientIp,
HttpServletResponse response) throws IOException {
List<Release> releases = Lists.newLinkedList();
......@@ -140,7 +132,7 @@ public class ConfigController {
}
//try to load via data center
if (!Objects.isNull(dataCenter) && !Objects.equals(dataCenter, clusterName)) {
if (!Strings.isNullOrEmpty(dataCenter) && !Objects.equals(dataCenter, clusterName)) {
Release dataCenterRelease =
configService.findRelease(appId, dataCenter, namespace);
if (!Objects.isNull(dataCenterRelease)) {
......
......@@ -60,7 +60,8 @@ public class NotificationController implements MessageListener {
@RequestParam(value = "appId") String appId,
@RequestParam(value = "cluster") String cluster,
@RequestParam(value = "namespace", defaultValue = ConfigConsts.NAMESPACE_DEFAULT) String namespace,
@RequestParam(value = "dataCenter", required = false) String dataCenter) {
@RequestParam(value = "dataCenter", required = false) String dataCenter,
@RequestParam(value = "ip", required = false) String clientIp) {
Set<String> watchedKeys = assembleWatchKeys(appId, cluster, namespace, dataCenter);
//Listen on more namespaces, since it's not the default namespace
......@@ -128,7 +129,7 @@ public class NotificationController implements MessageListener {
}
//watch data center config change
if (!Objects.isNull(dataCenter) && !Objects.equals(dataCenter, clusterName)) {
if (!Strings.isNullOrEmpty(dataCenter) && !Objects.equals(dataCenter, clusterName)) {
watchedKeys.add(assembleKey(appId, dataCenter, namespace));
}
......
......@@ -42,7 +42,7 @@ public class ServiceController {
@RequestMapping("/config")
public List<ServiceDTO> getConfigService(
@RequestParam(value = "appId", defaultValue = "") String appId,
@RequestParam(value = "ip", defaultValue = "") String clientIp) {
@RequestParam(value = "ip", required = false) String clientIp) {
List<InstanceInfo> instances = discoveryService.getConfigServiceInstances();
List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {
......
......@@ -49,6 +49,7 @@ public class ConfigControllerTest {
private String defaultNamespaceName;
private String somePublicNamespaceName;
private String someDataCenter;
private String someClientIp;
@Mock
private Release someRelease;
@Mock
......@@ -66,6 +67,7 @@ public class ConfigControllerTest {
defaultNamespaceName = ConfigConsts.NAMESPACE_DEFAULT;
somePublicNamespaceName = "somePublicNamespace";
someDataCenter = "someDC";
someClientIp = "someClientIp";
String someValidConfiguration = "{\"apollo.bar\": \"foo\"}";
String somePublicConfiguration = "{\"apollo.public.bar\": \"foo\"}";
......@@ -85,8 +87,8 @@ public class ConfigControllerTest {
when(someRelease.getReleaseKey()).thenReturn(someServerSideNewReleaseKey);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName,
defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
someResponse);
defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
someClientIp, someResponse);
verify(configService, times(1)).findRelease(someAppId, someClusterName, defaultNamespaceName);
assertEquals(someAppId, result.getAppId());
......@@ -106,7 +108,7 @@ public class ConfigControllerTest {
ApolloConfig result = configController.queryConfig(someAppId, someClusterName,
defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
someResponse);
someClientIp, someResponse);
assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
......@@ -125,7 +127,7 @@ public class ConfigControllerTest {
ApolloConfig
result =
configController.queryConfig(someAppId, someClusterName, defaultNamespaceName,
someDataCenter, String.valueOf(someClientSideReleaseKey), someResponse);
someDataCenter, String.valueOf(someClientSideReleaseKey), someClientIp, someResponse);
assertNull(result);
verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
......@@ -144,7 +146,7 @@ public class ConfigControllerTest {
ApolloConfig result = configController.queryConfig(someAppId, defaultClusterName,
defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
someResponse);
someClientIp, someResponse);
verify(configService, times(1)).findRelease(someAppId, someDataCenter, defaultNamespaceName);
assertEquals(someAppId, result.getAppId());
......@@ -168,10 +170,11 @@ public class ConfigControllerTest {
ApolloConfig result = configController.queryConfig(someAppId, defaultClusterName,
defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
someResponse);
someClientIp, someResponse);
verify(configService, times(1)).findRelease(someAppId, someDataCenter, defaultNamespaceName);
verify(configService, times(1)).findRelease(someAppId, defaultClusterName, defaultNamespaceName);
verify(configService, times(1))
.findRelease(someAppId, defaultClusterName, defaultNamespaceName);
assertEquals(someAppId, result.getAppId());
assertEquals(defaultClusterName, result.getCluster());
assertEquals(defaultNamespaceName, result.getNamespaceName());
......@@ -198,7 +201,7 @@ public class ConfigControllerTest {
ApolloConfig result =
configController
.queryConfig(someAppId, someClusterName, someAppOwnNamespaceName, someDataCenter,
someClientSideReleaseKey, someResponse);
someClientSideReleaseKey, someClientIp, someResponse);
assertEquals(someServerSideReleaseKey, result.getReleaseKey());
assertEquals(someAppId, result.getAppId());
......@@ -227,7 +230,7 @@ public class ConfigControllerTest {
ApolloConfig result =
configController
.queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter,
someClientSideReleaseKey, someResponse);
someClientSideReleaseKey, someClientIp, someResponse);
assertEquals(someServerSideReleaseKey, result.getReleaseKey());
assertEquals(someAppId, result.getAppId());
......@@ -259,7 +262,7 @@ public class ConfigControllerTest {
ApolloConfig result =
configController
.queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter,
someClientSideReleaseKey, someResponse);
someClientSideReleaseKey, someClientIp, someResponse);
assertEquals(someServerSideReleaseKey, result.getReleaseKey());
assertEquals(someAppId, result.getAppId());
......@@ -294,7 +297,7 @@ public class ConfigControllerTest {
ApolloConfig result =
configController
.queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter,
someAppSideReleaseKey, someResponse);
someAppSideReleaseKey, someClientIp, someResponse);
assertEquals(Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR)
.join(someAppSideReleaseKey, somePublicAppSideReleaseKey),
......
......@@ -39,6 +39,7 @@ public class NotificationControllerTest {
private String defaultNamespace;
private String somePublicNamespace;
private String someDataCenter;
private String someClientIp;
@Mock
private AppNamespaceService appNamespaceService;
@Mock
......@@ -58,6 +59,7 @@ public class NotificationControllerTest {
defaultNamespace = ConfigConsts.NAMESPACE_DEFAULT;
somePublicNamespace = "somePublicNamespace";
someDataCenter = "someDC";
someClientIp = "someClientIp";
deferredResults =
(Multimap<String, DeferredResult<ResponseEntity<ApolloConfigNotification>>>) ReflectionTestUtils
......@@ -68,7 +70,7 @@ public class NotificationControllerTest {
public void testPollNotificationWithDefaultNamespace() throws Exception {
DeferredResult<ResponseEntity<ApolloConfigNotification>>
deferredResult = controller
.pollNotification(someAppId, someCluster, defaultNamespace, someDataCenter);
.pollNotification(someAppId, someCluster, defaultNamespace, someDataCenter, someClientIp);
List<String> clusters =
Lists.newArrayList(someCluster, someDataCenter, ConfigConsts.CLUSTER_NAME_DEFAULT);
......@@ -88,7 +90,7 @@ public class NotificationControllerTest {
throws Exception {
DeferredResult<ResponseEntity<ApolloConfigNotification>>
deferredResult = controller
.pollNotification(someAppId, defaultCluster, defaultNamespace, someDataCenter);
.pollNotification(someAppId, defaultCluster, defaultNamespace, someDataCenter, someClientIp);
List<String> clusters =
Lists.newArrayList(someDataCenter, defaultCluster);
......@@ -108,7 +110,7 @@ public class NotificationControllerTest {
throws Exception {
DeferredResult<ResponseEntity<ApolloConfigNotification>>
deferredResult = controller
.pollNotification(someAppId, defaultCluster, defaultNamespace, null);
.pollNotification(someAppId, defaultCluster, defaultNamespace, null, someClientIp);
List<String> clusters =
Lists.newArrayList(defaultCluster);
......@@ -135,7 +137,7 @@ public class NotificationControllerTest {
DeferredResult<ResponseEntity<ApolloConfigNotification>>
deferredResult = controller
.pollNotification(someAppId, someCluster, somePublicNamespace, someDataCenter);
.pollNotification(someAppId, someCluster, somePublicNamespace, someDataCenter, someClientIp);
List<String> clusters =
Lists.newArrayList(someCluster, someDataCenter, ConfigConsts.CLUSTER_NAME_DEFAULT);
......@@ -161,7 +163,7 @@ public class NotificationControllerTest {
public void testPollNotificationWithDefaultNamespaceAndHandleMessage() throws Exception {
DeferredResult<ResponseEntity<ApolloConfigNotification>>
deferredResult = controller
.pollNotification(someAppId, someCluster, defaultNamespace, someDataCenter);
.pollNotification(someAppId, someCluster, defaultNamespace, someDataCenter, someClientIp);
String key =
Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR)
......@@ -188,7 +190,7 @@ public class NotificationControllerTest {
DeferredResult<ResponseEntity<ApolloConfigNotification>>
deferredResult = controller
.pollNotification(someAppId, someCluster, somePublicNamespace, someDataCenter);
.pollNotification(someAppId, someCluster, somePublicNamespace, someDataCenter, someClientIp);
String key =
Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR)
......
......@@ -38,8 +38,8 @@ public class ConfigControllerIntegrationTest extends AbstractBaseIntegrationTest
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryConfigWithDefaultClusterAndDefaultNamespaceOK() throws Exception {
ResponseEntity<ApolloConfig> response = restTemplate
.getForEntity("{baseurl}/configs/{appId}/{clusterName}", ApolloConfig.class,
getHostUrl(), someAppId, ConfigConsts.CLUSTER_NAME_DEFAULT);
.getForEntity("{baseurl}/configs/{appId}/{clusterName}/{namespace}", ApolloConfig.class,
getHostUrl(), someAppId, ConfigConsts.CLUSTER_NAME_DEFAULT, ConfigConsts.NAMESPACE_DEFAULT);
ApolloConfig result = response.getBody();
assertEquals(HttpStatus.OK, response.getStatusCode());
......
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