提交 c46f561c 编写于 作者: J Jason Song

Support reading Envs in Portal

package com.ctrip.apollo.biz.utils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
......@@ -48,11 +50,23 @@ public class ApolloBeanUtils {
} catch (Exception e) {
throw new RuntimeException(e);
}
org.springframework.beans.BeanUtils.copyProperties(src, instance, ConverterUtils
.getNullPropertyNames(src));
org.springframework.beans.BeanUtils.copyProperties(src, instance, getNullPropertyNames(src));
return instance;
}
static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
/**
* 用于将一个列表转换为列表中的对象的某个属性映射到列表中的对象
*
......@@ -92,7 +106,7 @@ public class ApolloBeanUtils {
*/
public static <K, V> Map<K, List<V>> aggByKeyToList(String key, List list) {
Map<K, List<V>> map = new HashMap<K, List<V>>();
if (CollectionUtils.isEmpty(list)) {//防止外面传入空list
if (CollectionUtils.isEmpty(list)) {// 防止外面传入空list
return map;
}
try {
......@@ -122,7 +136,7 @@ public class ApolloBeanUtils {
*/
public static Set toPropertySet(String key, List list) {
Set set = new HashSet();
if (CollectionUtils.isEmpty(list)) {//防止外面传入空list
if (CollectionUtils.isEmpty(list)) {// 防止外面传入空list
return set;
}
try {
......@@ -168,7 +182,7 @@ public class ApolloBeanUtils {
return field.get(obj);
}
} catch (Exception e) {
//ig
// ig
}
return null;
}
......@@ -184,7 +198,7 @@ public class ApolloBeanUtils {
field.set(obj, value);
}
} catch (Exception e) {
//ig
// ig
}
}
......
package com.ctrip.apollo.biz.utils;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
public class ConverterUtils {
static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
}
package com.ctrip.apollo.client.loader;
import com.ctrip.apollo.client.env.ClientEnvironment;
import com.ctrip.apollo.core.serivce.ApolloService;
import com.ctrip.apollo.core.dto.ServiceDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -17,17 +17,17 @@ public class ConfigServiceLocator {
private RestTemplate restTemplate = new RestTemplate();
private List<ApolloService> serviceCaches = new ArrayList<>();
private List<ServiceDTO> serviceCaches = new ArrayList<>();
public List<ApolloService> getConfigServices() {
public List<ServiceDTO> getConfigServices() {
ClientEnvironment env = ClientEnvironment.getInstance();
String domainName = env.getMetaServerDomainName();
String url = domainName + "/services/config";
try {
ApolloService[] services = restTemplate.getForObject(new URI(url), ApolloService[].class);
ServiceDTO[] services = restTemplate.getForObject(new URI(url), ServiceDTO[].class);
if (services != null && services.length > 0) {
serviceCaches.clear();
for (ApolloService service : services) {
for (ServiceDTO service : services) {
serviceCaches.add(service);
}
}
......
......@@ -6,7 +6,7 @@ import com.ctrip.apollo.client.loader.ConfigServiceLocator;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.ctrip.apollo.core.serivce.ApolloService;
import com.ctrip.apollo.core.dto.ServiceDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -96,7 +96,7 @@ public class RemoteConfigLoader extends AbstractConfigLoader {
}
private String getConfigServiceUrl() {
List<ApolloService> services = serviceLocator.getConfigServices();
List<ServiceDTO> services = serviceLocator.getConfigServices();
if (services.size() == 0) {
throw new RuntimeException("No available config service");
}
......
......@@ -4,7 +4,7 @@ import com.ctrip.apollo.client.loader.ConfigServiceLocator;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.ctrip.apollo.core.serivce.ApolloService;
import com.ctrip.apollo.core.dto.ServiceDTO;
import org.junit.Before;
import org.junit.Test;
......@@ -60,9 +60,9 @@ public class RemoteConfigLoaderTest {
ApolloRegistry apolloRegistry = assembleSomeApolloRegistry(someAppId, "someVersion");
ApolloConfig previousConfig = null;
ApolloService someService = new ApolloService();
ServiceDTO someService = new ServiceDTO();
someService.setHomepageUrl(someServerUrl);
List<ApolloService> someServices = new ArrayList<>();
List<ServiceDTO> someServices = new ArrayList<>();
someServices.add(someService);
when(serviceLocater.getConfigServices()).thenReturn(someServices);
when(configUtil.getCluster()).thenReturn(someCluster);
......
package com.ctrip.apollo.metaservice.controller;
import com.ctrip.apollo.core.serivce.ApolloService;
import com.ctrip.apollo.core.dto.ServiceDTO;
import com.ctrip.apollo.metaservice.service.DiscoveryService;
import com.netflix.appinfo.InstanceInfo;
......@@ -20,11 +20,11 @@ public class ServiceController {
@RequestMapping("/meta")
public List<ApolloService> getMetaService() {
public List<ServiceDTO> getMetaService() {
List<InstanceInfo> instances = discoveryService.getMetaServiceInstances();
List<ApolloService> result = new ArrayList<ApolloService>();
List<ServiceDTO> result = new ArrayList<ServiceDTO>();
for (InstanceInfo instance : instances) {
ApolloService service = new ApolloService();
ServiceDTO service = new ServiceDTO();
service.setAppName(instance.getAppName());
service.setInstanceId(instance.getInstanceId());
service.setHomepageUrl(instance.getHomePageUrl());
......@@ -34,11 +34,11 @@ public class ServiceController {
}
@RequestMapping("/config")
public List<ApolloService> getConfigService() {
public List<ServiceDTO> getConfigService() {
List<InstanceInfo> instances = discoveryService.getConfigServiceInstances();
List<ApolloService> result = new ArrayList<ApolloService>();
List<ServiceDTO> result = new ArrayList<ServiceDTO>();
for (InstanceInfo instance : instances) {
ApolloService service = new ApolloService();
ServiceDTO service = new ServiceDTO();
service.setAppName(instance.getAppName());
service.setInstanceId(instance.getInstanceId());
service.setHomepageUrl(instance.getHomePageUrl());
......@@ -48,11 +48,11 @@ public class ServiceController {
}
@RequestMapping("/admin")
public List<ApolloService> getAdminService() {
public List<ServiceDTO> getAdminService() {
List<InstanceInfo> instances = discoveryService.getAdminServiceInstances();
List<ApolloService> result = new ArrayList<ApolloService>();
List<ServiceDTO> result = new ArrayList<ServiceDTO>();
for (InstanceInfo instance : instances) {
ApolloService service = new ApolloService();
ServiceDTO service = new ServiceDTO();
service.setAppName(instance.getAppName());
service.setInstanceId(instance.getInstanceId());
service.setHomepageUrl(instance.getHomePageUrl());
......
package com.ctrip.apollo.core;
public interface Constants {
public interface ConfigConsts {
String DEFAULT_CLUSTER_NAME = "default";
......
package com.ctrip.apollo.core;
public class ServiceNameConsts {
public interface ServiceNameConsts {
public static final String APOLLO_METASERVICE = "apollo-metaservice";
final String APOLLO_METASERVICE = "apollo-metaservice";
public static final String APOLLO_CONFIGSERVICE = "apollo-configservice";
final String APOLLO_CONFIGSERVICE = "apollo-configservice";
public static final String APOLLO_ADMINSERVICE = "apollo-adminservice";
final String APOLLO_ADMINSERVICE = "apollo-adminservice";
public static final String APOLLO_PORTAL = "apollo-portal";
final String APOLLO_PORTAL = "apollo-portal";
}
package com.ctrip.apollo.core.serivce;
package com.ctrip.apollo.core.dto;
public class ApolloService {
public class ServiceDTO {
private String appName;
......
package com.ctrip.apollo.core.exception;
public class ServiceException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public ServiceException(String str) {
super(str);
}
}
......@@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory;
public class ResourceUtils {
private static final Logger logger = LoggerFactory.getLogger(ResourceUtils.class);
@SuppressWarnings("unchecked")
public static Properties readConfigFile(String configPath, Properties defaults) {
InputStream in = ClassLoader.getSystemResourceAsStream(configPath);
......@@ -28,6 +28,8 @@ public class ResourceUtils {
if (Files.isReadable(path)) {
in = new FileInputStream(path.toFile());
logger.info("Reading config from file {} ", path);
} else {
logger.info("Could not find available config file");
}
}
if (defaults != null) {
......@@ -51,12 +53,16 @@ public class ResourceUtils {
}
StringBuilder sb = new StringBuilder();
for (Enumeration<String> e = (Enumeration<String>) props.propertyNames(); e
.hasMoreElements(); ) {
.hasMoreElements();) {
String key = e.nextElement();
String val = (String) props.getProperty(key);
sb.append(key).append('=').append(val).append('\n');
}
logger.info("Reading properties: \n" + sb.toString());
if (sb.length() > 0) {
logger.info("Reading properties: \n" + sb.toString());
} else {
logger.info("No available properties");
}
return props;
}
}
local.meta=http://localhost:8090
\ No newline at end of file
local.meta=http://localhost:8080
\ No newline at end of file
package com.ctrip.apollo.core;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.util.Assert;
import com.ctrip.apollo.Apollo.Env;
public class MetaDomainTest {
@Test
public void testDefaultDomain() {
Assert.notNull(MetaDomainConsts.getDomain(Env.LOCAL));
Assert.isNull(MetaDomainConsts.getDomain(Env.PRO));
public void testGetMetaDomain() {
Assert.assertEquals("http://localhost:8090", MetaDomainConsts.getDomain(Env.LOCAL));
Assert.assertEquals("http://dev:8080", MetaDomainConsts.getDomain(Env.DEV));
Assert.assertNull(MetaDomainConsts.getDomain(Env.PRO));
}
}
local.meta=http://localhost:8090
\ No newline at end of file
local.meta=http://localhost:8090
dev.meta=http://dev:8080
\ No newline at end of file
package com.ctrip.apollo.portal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ctrip.apollo.Apollo.Env;
@Component
public class PortalSettings {
@Value("#{'${apollo.portal.env}'.split(',')}")
private List<String> env;
private List<Env> envs = new ArrayList<Env>();
@PostConstruct
private void postConstruct() {
for (String e : env) {
envs.add(Env.valueOf(e.toUpperCase()));
}
}
public List<Env> getEnvs() {
return envs;
}
}
package com.ctrip.apollo.portal.api;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.exception.ServiceException;
import com.ctrip.apollo.portal.service.ServiceLocator;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -13,9 +14,14 @@ public class API {
protected RestTemplate restTemplate = new RestTemplate();
public String getAdminServiceHost(Apollo.Env env){
//本地测试用
// return "http://localhost:8090";
return serviceLocator.getAdminService(env);
public String getAdminServiceHost(Apollo.Env env) {
// 本地测试用
// return "http://localhost:8090";
try {
return serviceLocator.getAdminService(env).getHomepageUrl();
} catch (ServiceException e) {
e.printStackTrace();
}
return "";
}
}
......@@ -14,7 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.Constants;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
......@@ -61,7 +61,7 @@ public class ConfigService {
for (ReleaseSnapshotDTO snapShot : releaseSnapShots) {
// default cluster
if (Constants.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) {
if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) {
collectDefaultClusterConfigs(appId, snapShot, appConfigVO);
......@@ -223,7 +223,7 @@ public class ConfigService {
clusterName = entry.getKey();
clusterConfigs = entry.getValue();
if (Constants.DEFAULT_CLUSTER_NAME.equals(clusterName)) {
if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(clusterName)) {
// default cluster configs
collectDefaultClusterConfigs(appId, clusterConfigs, defaultClusterConfigs,
overrideAppConfigs);
......
......@@ -11,7 +11,8 @@ 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;
import com.ctrip.apollo.core.dto.ServiceDTO;
import com.ctrip.apollo.core.exception.ServiceException;
/**
* @author liuym
......@@ -23,32 +24,32 @@ public class ServiceLocator {
private RestTemplate restTemplate = new RestTemplate();
private List<ApolloService> serviceCaches = new ArrayList<>();
private List<ServiceDTO> serviceCaches = new ArrayList<>();
public List<ApolloService> getAdminServices(Env env) {
return getServices(env, "admin");
}
public String getAdminService(Env env) {
List<ApolloService> services = getAdminServices(env);
public ServiceDTO getAdminService(Env env) throws ServiceException {
List<ServiceDTO> services = getServices(env, "admin");
if (services.size() == 0) {
throw new RuntimeException("No available admin service");
throw new ServiceException("No available admin service");
}
return services.get(0).getHomepageUrl();
return services.get(0);
}
public List<ApolloService> getConfigServices(Env env) {
return getServices(env, "config");
public ServiceDTO getConfigService(Env env) throws ServiceException {
List<ServiceDTO> services = getServices(env, "config");
if (services.size() == 0) {
throw new ServiceException("No available config service");
}
return services.get(0);
}
private List<ApolloService> getServices(Env env, String serviceUrl) {
private List<ServiceDTO> 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);
ServiceDTO[] services = restTemplate.getForObject(new URI(url), ServiceDTO[].class);
if (services != null && services.length > 0) {
serviceCaches.clear();
for (ApolloService service : services) {
for (ServiceDTO service : services) {
serviceCaches.add(service);
}
}
......
......@@ -17,3 +17,7 @@ logging:
ctrip:
appid: 100003173
apollo:
portal:
env: dev,fws,uat
......@@ -15,11 +15,13 @@ import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.Constants;
import com.ctrip.apollo.core.ConfigConsts;
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.ServiceDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.core.exception.ServiceException;
import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.constants.PortalConstants;
import com.ctrip.apollo.portal.entity.AppConfigVO;
......@@ -47,7 +49,7 @@ public class ConfigServiceTest {
@Before
public void setUp() {
public void setUp() throws ServiceException {
ReflectionTestUtils.setField(versionAPI, "restTemplate", restTemplate);
ReflectionTestUtils.setField(clusterAPI, "restTemplate", restTemplate);
ReflectionTestUtils.setField(configAPI, "restTemplate", restTemplate);
......@@ -57,7 +59,9 @@ public class ConfigServiceTest {
ReflectionTestUtils.setField(configAPI, "serviceLocator", serviceLocator);
String defaultAdminService = "http://localhost:8090";
Mockito.doReturn(defaultAdminService).when(serviceLocator).getAdminService(Env.DEV);
ServiceDTO service = new ServiceDTO();
service.setHomepageUrl(defaultAdminService);
Mockito.doReturn(service).when(serviceLocator).getAdminService(Env.DEV);
}
@Test
......@@ -89,7 +93,7 @@ public class ConfigServiceTest {
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[1];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, Constants.DEFAULT_CLUSTER_NAME,
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
when(versionAPI.getVersionById(Env.DEV, versionId)).thenReturn(someVersion);
......@@ -111,7 +115,7 @@ public class ConfigServiceTest {
long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[1];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, Constants.DEFAULT_CLUSTER_NAME,
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\", \"5555.bar\":\"demo2\", \"22.bar\":\"demo2\"}");
when(versionAPI.getVersionById(Env.DEV, versionId)).thenReturn(someVersion);
......@@ -133,7 +137,7 @@ public class ConfigServiceTest {
long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[2];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, Constants.DEFAULT_CLUSTER_NAME,
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
someReleaseSnapShots[1] = assembleReleaseSnapShot(11112, "cluster1",
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
......@@ -179,7 +183,7 @@ public class ConfigServiceTest {
private ReleaseSnapshotDTO[] assembleReleaseSnapShots() {
ReleaseSnapshotDTO[] releaseSnapShots = new ReleaseSnapshotDTO[3];
releaseSnapShots[0] = assembleReleaseSnapShot(11111, Constants.DEFAULT_CLUSTER_NAME,
releaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\",\"3333.foo\":\"1008\",\"4444.bar\":\"99901\"}");
releaseSnapShots[1] = assembleReleaseSnapShot(11111, "cluster1", "{\"6666.foo\":\"demo1\"}");
releaseSnapShots[2] = assembleReleaseSnapShot(11111, "cluster2", "{\"6666.bar\":\"bar2222\"}");
......@@ -197,7 +201,7 @@ public class ConfigServiceTest {
private ClusterDTO[] assembleClusters() {
ClusterDTO[] clusters = new ClusterDTO[2];
clusters[0] = assembleCluster(100, "6666", Constants.DEFAULT_CLUSTER_NAME);
clusters[0] = assembleCluster(100, "6666", ConfigConsts.DEFAULT_CLUSTER_NAME);
clusters[1] = assembleCluster(101, "6666", "cluster1");
return clusters;
}
......@@ -212,10 +216,10 @@ public class ConfigServiceTest {
private ConfigItemDTO[] assembleConfigItems() {
ConfigItemDTO[] configItems = new ConfigItemDTO[5];
configItems[0] = assembleConfigItem(100, Constants.DEFAULT_CLUSTER_NAME, "6666", "6666.k1", "6666.v1");
configItems[1] = assembleConfigItem(100, Constants.DEFAULT_CLUSTER_NAME, "6666", "6666.k2", "6666.v2");
configItems[2] = assembleConfigItem(100, Constants.DEFAULT_CLUSTER_NAME, "6666", "6666.k3", "6666.v3");
configItems[3] = assembleConfigItem(100, Constants.DEFAULT_CLUSTER_NAME, "5555", "5555.k1", "5555.v1");
configItems[0] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k1", "6666.v1");
configItems[1] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k2", "6666.v2");
configItems[2] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k3", "6666.v3");
configItems[3] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "5555", "5555.k1", "5555.v1");
configItems[4] = assembleConfigItem(101, "cluster1", "6666", "6666.k1", "6666.v1");
return configItems;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册