Commit 6e6f814f by lepdou

admin service discovery

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