Commit 6e6f814f by lepdou

admin service discovery

parent 7bc734b7
package com.ctrip.apollo.portal.controller; package com.ctrip.apollo.portal.controller;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.portal.constants.PortalConstants; import com.ctrip.apollo.portal.constants.PortalConstants;
import com.ctrip.apollo.portal.entity.AppConfigVO; import com.ctrip.apollo.portal.entity.AppConfigVO;
import com.ctrip.apollo.portal.exception.NotFoundException; import com.ctrip.apollo.portal.exception.NotFoundException;
...@@ -26,9 +27,9 @@ public class ConfigController { ...@@ -26,9 +27,9 @@ public class ConfigController {
} }
if (versionId == PortalConstants.LASTEST_VERSION_ID) { if (versionId == PortalConstants.LASTEST_VERSION_ID) {
return configService.loadLatestConfig(appId); return configService.loadLatestConfig(Apollo.Env.DEV, appId);
} else if (versionId > 0) { } else if (versionId > 0) {
return configService.loadReleaseConfig(appId, versionId); return configService.loadReleaseConfig(Apollo.Env.DEV, appId, versionId);
} else { } else {
throw new NotFoundException(); throw new NotFoundException();
} }
......
package com.ctrip.apollo.portal.controller; package com.ctrip.apollo.portal.controller;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.VersionDTO; import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.service.VersionService; import com.ctrip.apollo.portal.service.VersionService;
...@@ -19,6 +20,6 @@ public class VersionController { ...@@ -19,6 +20,6 @@ public class VersionController {
@RequestMapping("/{appId}/{env}") @RequestMapping("/{appId}/{env}")
public List<VersionDTO> versions(@PathVariable long appId, @PathVariable String env) { public List<VersionDTO> versions(@PathVariable long appId, @PathVariable String env) {
return versionService.findVersionsByApp(appId, env); return versionService.findVersionsByApp(Apollo.Env.DEV, appId);
} }
} }
...@@ -31,23 +31,16 @@ public class ConfigService { ...@@ -31,23 +31,16 @@ public class ConfigService {
private ObjectMapper objectMapper = new ObjectMapper(); private ObjectMapper objectMapper = new ObjectMapper();
public String getAdminServiceUrl() { public AppConfigVO loadReleaseConfig(Env env, long appId, long versionId) {
List<ApolloService> services = serviceLocator.getAdminServices(Env.DEV);
if (services.size() == 0) {
throw new RuntimeException("No available admin service");
}
return services.get(0).getHomepageUrl();
}
public AppConfigVO loadReleaseConfig(long appId, long versionId) {
if (appId <= 0 || versionId <= 0) { if (appId <= 0 || versionId <= 0) {
return null; return null;
} }
long releaseId = getReleaseIdFromVersionId(versionId); long releaseId = getReleaseIdFromVersionId(env, versionId);
String serviceHost = serviceLocator.getAdminService(env);
String serviceHost = getAdminServiceUrl();
ReleaseSnapshotDTO[] releaseSnapShots = RestUtils ReleaseSnapshotDTO[] releaseSnapShots = RestUtils
.exchangeInGET(serviceHost + "/configs/release/" + releaseId, ReleaseSnapshotDTO[].class); .exchangeInGET(serviceHost + "/configs/release/" + releaseId, ReleaseSnapshotDTO[].class);
if (releaseSnapShots == null || releaseSnapShots.length == 0) { if (releaseSnapShots == null || releaseSnapShots.length == 0) {
...@@ -69,8 +62,8 @@ public class ConfigService { ...@@ -69,8 +62,8 @@ public class ConfigService {
return appConfigVO; return appConfigVO;
} }
private long getReleaseIdFromVersionId(long versionId) { private long getReleaseIdFromVersionId(Env env, long versionId) {
String serviceHost = getAdminServiceUrl(); String serviceHost = serviceLocator.getAdminService(env);
VersionDTO version = VersionDTO version =
RestUtils.exchangeInGET(serviceHost + "/version/" + versionId, VersionDTO.class); RestUtils.exchangeInGET(serviceHost + "/version/" + versionId, VersionDTO.class);
if (version == null) { if (version == null) {
...@@ -154,12 +147,12 @@ public class ConfigService { ...@@ -154,12 +147,12 @@ public class ConfigService {
overrideClusterConfigs.add(overrideClusterConfig); overrideClusterConfigs.add(overrideClusterConfig);
} }
public AppConfigVO loadLatestConfig(long appId) { public AppConfigVO loadLatestConfig(Env env, long appId) {
if (appId <= 0) { if (appId <= 0) {
return null; return null;
} }
String serviceHost = getAdminServiceUrl(); String serviceHost = serviceLocator.getAdminService(env);
ClusterDTO[] clusters = ClusterDTO[] clusters =
RestUtils.exchangeInGET(serviceHost + "/cluster/app/" + appId, ClusterDTO[].class); RestUtils.exchangeInGET(serviceHost + "/cluster/app/" + appId, ClusterDTO[].class);
if (clusters == null || clusters.length == 0) { if (clusters == null || clusters.length == 0) {
......
...@@ -14,9 +14,7 @@ import com.ctrip.apollo.core.MetaDomainConsts; ...@@ -14,9 +14,7 @@ import com.ctrip.apollo.core.MetaDomainConsts;
import com.ctrip.apollo.core.serivce.ApolloService; import com.ctrip.apollo.core.serivce.ApolloService;
/** /**
*
* @author liuym * @author liuym
*
*/ */
@Service @Service
public class ServiceLocator { public class ServiceLocator {
...@@ -31,6 +29,16 @@ public class ServiceLocator { ...@@ -31,6 +29,16 @@ public class ServiceLocator {
return getServices(env, "admin"); return getServices(env, "admin");
} }
public String getAdminService(Env env) {
//本地测试用
// return "http://localhost:8090";
List<ApolloService> services = getAdminServices(env);
if (services.size() == 0) {
throw new RuntimeException("No available admin service");
}
return services.get(0).getHomepageUrl();
}
public List<ApolloService> getConfigServices(Env env) { public List<ApolloService> getConfigServices(Env env) {
return getServices(env, "config"); return getServices(env, "config");
} }
......
package com.ctrip.apollo.portal.service; package com.ctrip.apollo.portal.service;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.VersionDTO; import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.RestUtils; import com.ctrip.apollo.portal.RestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
...@@ -10,7 +12,12 @@ import java.util.List; ...@@ -10,7 +12,12 @@ import java.util.List;
@Service @Service
public class VersionService { public class VersionService {
public List<VersionDTO> findVersionsByApp(long appId, String env) { @Autowired
return RestUtils.exchangeInGET("http://localhost:8090/version/app/" + appId, List.class); private ServiceLocator serviceLocator;
public List<VersionDTO> findVersionsByApp(Apollo.Env env, long appId) {
return RestUtils
.exchangeInGET(serviceLocator.getAdminService(env) + "/version/app/" + appId, List.class);
} }
} }
...@@ -57,11 +57,8 @@ public class ConfigServiceTest { ...@@ -57,11 +57,8 @@ public class ConfigServiceTest {
@Before @Before
public void setUp() { public void setUp() {
ReflectionTestUtils.setField(RestUtils.class, "restTemplate", restTemplate); ReflectionTestUtils.setField(RestUtils.class, "restTemplate", restTemplate);
ApolloService defaultAdminService = new ApolloService(); String defaultAdminService = "http://localhost:8090";
defaultAdminService.setHomepageUrl("http://localhost:8090"); Mockito.doReturn(defaultAdminService).when(serviceLocator).getAdminService(Env.DEV);
List<ApolloService> services = new ArrayList<>();
services.add(defaultAdminService);
Mockito.doReturn(services).when(serviceLocator).getAdminServices(Env.DEV);
} }
@Test @Test
...@@ -73,13 +70,13 @@ public class ConfigServiceTest { ...@@ -73,13 +70,13 @@ public class ConfigServiceTest {
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId); VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = assembleReleaseSnapShots(); ReleaseSnapshotDTO[] someReleaseSnapShots = assembleReleaseSnapShots();
restInvoke(configService.getAdminServiceUrl() + "/configs/release/" + releaseId, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/configs/release/" + releaseId,
ReleaseSnapshotDTO[].class, someReleaseSnapShots, releaseSnapShotResponse); ReleaseSnapshotDTO[].class, someReleaseSnapShots, releaseSnapShotResponse);
restInvoke(configService.getAdminServiceUrl() + "/version/" + versionId, VersionDTO.class, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/version/" + versionId, VersionDTO.class,
someVersion, versionResponse); someVersion, versionResponse);
AppConfigVO appConfigVO = configService.loadReleaseConfig(appId, versionId); AppConfigVO appConfigVO = configService.loadReleaseConfig(Env.DEV, appId, versionId);
assertEquals(appConfigVO.getAppId(), appId); assertEquals(appConfigVO.getAppId(), appId);
assertEquals(appConfigVO.getVersionId(), versionId); assertEquals(appConfigVO.getVersionId(), versionId);
...@@ -99,13 +96,13 @@ public class ConfigServiceTest { ...@@ -99,13 +96,13 @@ public class ConfigServiceTest {
releaseSnapShots[0] = assembleReleaseSnapShot(11111, "default-cluster-name", releaseSnapShots[0] = assembleReleaseSnapShot(11111, "default-cluster-name",
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}"); "{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
restInvoke(configService.getAdminServiceUrl() + "/configs/release/" + releaseId, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/configs/release/" + releaseId,
ReleaseSnapshotDTO[].class, releaseSnapShots, releaseSnapShotResponse); ReleaseSnapshotDTO[].class, releaseSnapShots, releaseSnapShotResponse);
restInvoke(configService.getAdminServiceUrl() + "/version/" + versionId, VersionDTO.class, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/version/" + versionId, VersionDTO.class,
someVersion, versionResponse); someVersion, versionResponse);
AppConfigVO appConfigVO = configService.loadReleaseConfig(appId, versionId); AppConfigVO appConfigVO = configService.loadReleaseConfig(Env.DEV, appId, versionId);
assertEquals(appConfigVO.getAppId(), appId); assertEquals(appConfigVO.getAppId(), appId);
assertEquals(appConfigVO.getVersionId(), versionId); assertEquals(appConfigVO.getVersionId(), versionId);
...@@ -124,13 +121,13 @@ public class ConfigServiceTest { ...@@ -124,13 +121,13 @@ public class ConfigServiceTest {
releaseSnapShots[0] = assembleReleaseSnapShot(11111, "default-cluster-name", releaseSnapShots[0] = assembleReleaseSnapShot(11111, "default-cluster-name",
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\", \"5555.bar\":\"demo2\", \"22.bar\":\"demo2\"}"); "{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\", \"5555.bar\":\"demo2\", \"22.bar\":\"demo2\"}");
restInvoke(configService.getAdminServiceUrl() + "/configs/release/" + releaseId, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/configs/release/" + releaseId,
ReleaseSnapshotDTO[].class, releaseSnapShots, releaseSnapShotResponse); ReleaseSnapshotDTO[].class, releaseSnapShots, releaseSnapShotResponse);
restInvoke(configService.getAdminServiceUrl() + "/version/" + versionId, VersionDTO.class, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/version/" + versionId, VersionDTO.class,
someVersion, versionResponse); someVersion, versionResponse);
AppConfigVO appConfigVO = configService.loadReleaseConfig(appId, versionId); AppConfigVO appConfigVO = configService.loadReleaseConfig(Env.DEV, appId, versionId);
assertEquals(appConfigVO.getAppId(), appId); assertEquals(appConfigVO.getAppId(), appId);
assertEquals(appConfigVO.getVersionId(), versionId); assertEquals(appConfigVO.getVersionId(), versionId);
...@@ -151,13 +148,13 @@ public class ConfigServiceTest { ...@@ -151,13 +148,13 @@ public class ConfigServiceTest {
releaseSnapShots[1] = assembleReleaseSnapShot(11112, "cluster1", releaseSnapShots[1] = assembleReleaseSnapShot(11112, "cluster1",
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}"); "{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
restInvoke(configService.getAdminServiceUrl() + "/configs/release/" + releaseId, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/configs/release/" + releaseId,
ReleaseSnapshotDTO[].class, releaseSnapShots, releaseSnapShotResponse); ReleaseSnapshotDTO[].class, releaseSnapShots, releaseSnapShotResponse);
restInvoke(configService.getAdminServiceUrl() + "/version/" + versionId, VersionDTO.class, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/version/" + versionId, VersionDTO.class,
someVersion, versionResponse); someVersion, versionResponse);
AppConfigVO appConfigVO = configService.loadReleaseConfig(appId, versionId); AppConfigVO appConfigVO = configService.loadReleaseConfig(Env.DEV, appId, versionId);
assertEquals(appConfigVO.getAppId(), appId); assertEquals(appConfigVO.getAppId(), appId);
assertEquals(appConfigVO.getVersionId(), versionId); assertEquals(appConfigVO.getVersionId(), versionId);
...@@ -172,13 +169,13 @@ public class ConfigServiceTest { ...@@ -172,13 +169,13 @@ public class ConfigServiceTest {
ClusterDTO[] someClusters = assembleClusters(); ClusterDTO[] someClusters = assembleClusters();
ConfigItemDTO[] someConfigItem = assembleConfigItems(); ConfigItemDTO[] someConfigItem = assembleConfigItems();
restInvoke(configService.getAdminServiceUrl() + "/cluster/app/" + appId, ClusterDTO[].class, restInvoke(serviceLocator.getAdminService(Env.DEV) + "/cluster/app/" + appId, ClusterDTO[].class,
someClusters, clusterResponse); someClusters, clusterResponse);
restInvoke(configService.getAdminServiceUrl() + "/configs/latest?clusterIds=100,101", restInvoke(serviceLocator.getAdminService(Env.DEV) + "/configs/latest?clusterIds=100,101",
ConfigItemDTO[].class, someConfigItem, configItemResponse); ConfigItemDTO[].class, someConfigItem, configItemResponse);
AppConfigVO appConfigVO = configService.loadLatestConfig(appId); AppConfigVO appConfigVO = configService.loadLatestConfig(Env.DEV, appId);
assertEquals(appConfigVO.getAppId(), 6666); assertEquals(appConfigVO.getAppId(), 6666);
assertEquals(appConfigVO.getVersionId(), PortalConstants.LASTEST_VERSION_ID); assertEquals(appConfigVO.getVersionId(), PortalConstants.LASTEST_VERSION_ID);
......
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