Commit 1af04408 by Yiming Liu

Integrate service discovery in portal

parent bebe9de4
package com.ctrip.apollo.portal.entity; package com.ctrip.apollo.portal.entity;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.dto.ConfigItemDTO; import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.portal.enums.Env;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
......
package com.ctrip.apollo.portal.enums;
public enum Env {
DEV("dev"), FWS("fws"), FAT("fat"), UAT("uat"), LPT("lpt"), PROD("prod"), TOOLS("tools"), UN_KNOW(
"");
private String value;
Env(String value) {
this.value = value;
}
public static Env valueFrom(String env) {
if (env == null || "".equals(env)) {
return UN_KNOW;
} else if ("dev".equals(env)) {
return DEV;
} else if ("fws".equals(env)) {
return FWS;
} else if ("fat".equals(env)) {
return FAT;
} else if ("uat".equals(env)) {
return UAT;
} else if ("prod".equals(env)) {
return PROD;
} else if ("tools".equals(env)) {
return TOOLS;
} else {
return UN_KNOW;
}
}
}
package com.ctrip.apollo.portal.service;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.MetaDomainConsts;
import com.ctrip.apollo.core.serivce.ApolloService;
/**
*
* @author liuym
*
*/
@Service
public class ServiceLocator {
private static final Logger logger = LoggerFactory.getLogger(ServiceLocator.class);
private RestTemplate restTemplate = new RestTemplate();
private List<ApolloService> serviceCaches = new ArrayList<>();
public List<ApolloService> getAdminServices(Env env) {
return getServices(env, "admin");
}
public List<ApolloService> getConfigServices(Env env) {
return getServices(env, "config");
}
private List<ApolloService> getServices(Env env, String serviceUrl) {
String domainName = MetaDomainConsts.getDomain(env);
String url = domainName + "/services/" + serviceUrl;
try {
ApolloService[] services = restTemplate.getForObject(new URI(url), ApolloService[].class);
if (services != null && services.length > 0) {
serviceCaches.clear();
for (ApolloService service : services) {
serviceCaches.add(service);
}
}
} catch (Exception ex) {
logger.warn(ex.getMessage());
}
return serviceCaches;
}
}
package com.ctrip.apollo.portal.service.impl;
import com.google.common.collect.Maps;
import com.ctrip.apollo.core.Constants;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.RestUtils;
import com.ctrip.apollo.portal.constants.PortalConstants;
import com.ctrip.apollo.portal.entity.AppConfigVO;
import com.ctrip.apollo.portal.service.ConfigService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@Service
public class ConfigServiceImpl implements ConfigService {
public static final String ADMIN_SERVICE_HOST = "http://localhost:8090";
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public AppConfigVO loadReleaseConfig(long appId, long versionId) {
if (appId <= 0 || versionId <= 0) {
return null;
}
long releaseId = getReleaseIdFromVersionId(versionId);
ReleaseSnapshotDTO[] releaseSnapShots = RestUtils.exchangeInGET(
ADMIN_SERVICE_HOST + "/configs/release/" + releaseId, ReleaseSnapshotDTO[].class);
if (releaseSnapShots == null || releaseSnapShots.length == 0) {
return null;
}
AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, versionId);
for (ReleaseSnapshotDTO snapShot : releaseSnapShots) {
//default cluster
if (Constants.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) {
collectDefaultClusterConfigs(appId, snapShot, appConfigVO);
} else {//cluster special configs
collectSpecialClusterConfigs(appId, snapShot, appConfigVO);
}
}
return appConfigVO;
}
private long getReleaseIdFromVersionId(long versionId) {
VersionDTO version =
RestUtils.exchangeInGET(ADMIN_SERVICE_HOST + "/version/" + versionId, VersionDTO.class);
if (version == null) {
return -1;
}
return version.getReleaseId();
}
private void collectDefaultClusterConfigs(long appId, ReleaseSnapshotDTO snapShot,
AppConfigVO appConfigVO) {
Map<Long, List<ConfigItemDTO>> groupedConfigs =
groupConfigsByApp(snapShot.getConfigurations());
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs =
appConfigVO.getOverrideAppConfigs();
for (Map.Entry<Long, List<ConfigItemDTO>> entry : groupedConfigs.entrySet()) {
long configAppId = entry.getKey();
List<ConfigItemDTO> kvs = entry.getValue();
if (configAppId == appId) {
appConfigVO.setDefaultClusterConfigs(kvs);
} else {
AppConfigVO.OverrideAppConfig overrideAppConfig =
new AppConfigVO.OverrideAppConfig();
overrideAppConfig.setAppId(configAppId);
overrideAppConfig.setConfigs(kvs);
overrideAppConfigs.add(overrideAppConfig);
}
}
}
/**
* appId -> List<KV>
*/
private Map<Long, List<ConfigItemDTO>> groupConfigsByApp(String configJson) {
if (configJson == null || "".equals(configJson)) {
return Maps.newHashMap();
}
Map<Long, List<ConfigItemDTO>> appIdMapKVs = new HashMap<>();
String key;
Object value;
Map<String, String> kvMaps = null;
try {
kvMaps = objectMapper.readValue(configJson, Map.class);
} catch (IOException e) {
//todo log
}
for (Map.Entry<String, String> entry : kvMaps.entrySet()) {
key = entry.getKey();
value = entry.getValue();
Long appId = getAppIdFromKey(key);
List<ConfigItemDTO> kvs = appIdMapKVs.get(appId);
if (kvs == null) {
kvs = new LinkedList<>();
appIdMapKVs.put(appId, kvs);
}
kvs.add(new ConfigItemDTO(key, value.toString()));
}
return appIdMapKVs;
}
private Long getAppIdFromKey(String key) {
return Long.valueOf(key.substring(0, key.indexOf(".")));
}
private void collectSpecialClusterConfigs(long appId, ReleaseSnapshotDTO snapShot,
AppConfigVO appConfigVO) {
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs =
appConfigVO.getOverrideClusterConfigs();
AppConfigVO.OverrideClusterConfig overrideClusterConfig =
new AppConfigVO.OverrideClusterConfig();
overrideClusterConfig.setClusterName(snapShot.getClusterName());
//todo step1: cluster special config can't override other app config
overrideClusterConfig.setConfigs(groupConfigsByApp(snapShot.getConfigurations()).get(appId));
overrideClusterConfigs.add(overrideClusterConfig);
}
@Override
public AppConfigVO loadLatestConfig(long appId) {
if (appId <= 0) {
return null;
}
ClusterDTO[] clusters = RestUtils.exchangeInGET(
ADMIN_SERVICE_HOST + "/cluster/app/" + appId, ClusterDTO[].class);
if (clusters == null || clusters.length == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
for (ClusterDTO cluster : clusters) {
sb.append(cluster.getId()).append(",");
}
ConfigItemDTO[] configItems = RestUtils.exchangeInGET(
ADMIN_SERVICE_HOST + "/configs/latest?clusterIds=" + sb.substring(0,
sb.length() - 1), ConfigItemDTO[].class);
return buildAPPConfigVO(appId, Arrays.asList(configItems));
}
private AppConfigVO buildAPPConfigVO(long appId, List<ConfigItemDTO> configItems) {
if (configItems == null || configItems.size() == 0) {
return null;
}
Map<String, List<ConfigItemDTO>> groupedClusterConfigs = groupConfigByCluster(configItems);
AppConfigVO appConfigVO =
AppConfigVO.newInstance(appId, PortalConstants.LASTEST_VERSION_ID);
groupConfigByAppAndEnrichDTO(groupedClusterConfigs, appConfigVO);
return appConfigVO;
}
private Map<String, List<ConfigItemDTO>> groupConfigByCluster(List<ConfigItemDTO> configItems) {
Map<String, List<ConfigItemDTO>> groupedClusterConfigs = new HashMap<>();
String clusterName;
for (ConfigItemDTO configItem : configItems) {
clusterName = configItem.getClusterName();
List<ConfigItemDTO> clusterConfigs = groupedClusterConfigs.get(clusterName);
if (clusterConfigs == null) {
clusterConfigs = new LinkedList<>();
groupedClusterConfigs.put(clusterName, clusterConfigs);
}
clusterConfigs.add(configItem);
}
return groupedClusterConfigs;
}
private void groupConfigByAppAndEnrichDTO(Map<String, List<ConfigItemDTO>> groupedClusterConfigs,
AppConfigVO appConfigVO) {
long appId = appConfigVO.getAppId();
List<ConfigItemDTO> defaultClusterConfigs = appConfigVO.getDefaultClusterConfigs();
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs =
appConfigVO.getOverrideAppConfigs();
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs =
appConfigVO.getOverrideClusterConfigs();
String clusterName;
List<ConfigItemDTO> clusterConfigs;
for (Map.Entry<String, List<ConfigItemDTO>> entry : groupedClusterConfigs.entrySet()) {
clusterName = entry.getKey();
clusterConfigs = entry.getValue();
if (Constants.DEFAULT_CLUSTER_NAME.equals(clusterName)) {
//default cluster configs
collectDefaultClusterConfigs(appId, clusterConfigs, defaultClusterConfigs,
overrideAppConfigs);
} else {
//override cluster configs
collectSpecialClusterConfigs(clusterName, clusterConfigs, overrideClusterConfigs);
}
}
}
private void collectDefaultClusterConfigs(long appId, List<ConfigItemDTO> clusterConfigs,
List<ConfigItemDTO> defaultClusterConfigs,
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs) {
Map<Long, AppConfigVO.OverrideAppConfig> appIdMapOverrideAppConfig = null;
for (ConfigItemDTO config : clusterConfigs) {
long targetAppId = config.getAppId();
if (appId == targetAppId) {//app self's configs
defaultClusterConfigs.add(config);
} else {//override other app configs
if (appIdMapOverrideAppConfig == null) {
appIdMapOverrideAppConfig = new HashMap<>();
}
AppConfigVO.OverrideAppConfig overrideAppConfig =
appIdMapOverrideAppConfig.get(targetAppId);
if (overrideAppConfig == null) {
overrideAppConfig = new AppConfigVO.OverrideAppConfig();
appIdMapOverrideAppConfig.put(targetAppId, overrideAppConfig);
overrideAppConfigs.add(overrideAppConfig);
}
overrideAppConfig.setAppId(targetAppId);
overrideAppConfig.addConfig(config);
}
}
}
private void collectSpecialClusterConfigs(String clusterName, List<ConfigItemDTO> clusterConfigs,
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs) {
AppConfigVO.OverrideClusterConfig overrideClusterConfig =
new AppConfigVO.OverrideClusterConfig();
overrideClusterConfig.setClusterName(clusterName);
overrideClusterConfig.setConfigs(clusterConfigs);
overrideClusterConfigs.add(overrideClusterConfig);
}
}
...@@ -164,7 +164,7 @@ ...@@ -164,7 +164,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId> <artifactId>maven-war-plugin</artifactId>
<version>2.3</version> <version>2.6</version>
<configuration> <configuration>
<failOnMissingWebXml>false</failOnMissingWebXml> <failOnMissingWebXml>false</failOnMissingWebXml>
</configuration> </configuration>
......
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