Commit 17fdc911 by Jason Song

Merge pull request #142 from yiming187/service_refactor

Service refactor
parents 3d0186e1 e355e6ac
...@@ -28,4 +28,3 @@ dev ...@@ -28,4 +28,3 @@ dev
fat fat
uat uat
prd prd
apollo-env.properties
...@@ -9,6 +9,8 @@ import org.springframework.context.annotation.Bean; ...@@ -9,6 +9,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.dianping.cat.Cat;
@Component @Component
@Conditional(TitanCondition.class) @Conditional(TitanCondition.class)
public class TitanEntityManager { public class TitanEntityManager {
...@@ -22,8 +24,10 @@ public class TitanEntityManager { ...@@ -22,8 +24,10 @@ public class TitanEntityManager {
Class clazz = Class.forName("com.ctrip.datasource.configure.DalDataSourceFactory"); Class clazz = Class.forName("com.ctrip.datasource.configure.DalDataSourceFactory");
Object obj = clazz.newInstance(); Object obj = clazz.newInstance();
Method method = clazz.getMethod("createDataSource", new Class[] {String.class, String.class}); Method method = clazz.getMethod("createDataSource", new Class[] {String.class, String.class});
return ((DataSource) method.invoke(obj, DataSource ds = ((DataSource) method.invoke(obj,
new Object[] {settings.getTitanDbname(), settings.getTitanUrl()})); new Object[] {settings.getTitanDbname(), settings.getTitanUrl()}));
Cat.logEvent("Apollo.Datasource.Titan", settings.getTitanDbname());
return ds;
} }
} }
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
<artifactId>apollo-buildtools</artifactId> <artifactId>apollo-buildtools</artifactId>
<name>Apollo BuildTools</name> <name>Apollo BuildTools</name>
<properties> <properties>
<java.version>1.7</java.version>
<github.path>${project.artifactId}</github.path> <github.path>${project.artifactId}</github.path>
</properties> </properties>
<dependencies> <dependencies>
......
...@@ -4,7 +4,6 @@ import com.ctrip.apollo.core.ConfigConsts; ...@@ -4,7 +4,6 @@ import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.internals.ConfigManager; import com.ctrip.apollo.internals.ConfigManager;
import com.ctrip.apollo.spi.ConfigFactory; import com.ctrip.apollo.spi.ConfigFactory;
import com.ctrip.apollo.spi.ConfigRegistry; import com.ctrip.apollo.spi.ConfigRegistry;
import com.ctrip.apollo.util.ConfigUtil;
import com.dianping.cat.Cat; import com.dianping.cat.Cat;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
......
package com.ctrip.apollo.internals; package com.ctrip.apollo.internals;
import com.ctrip.apollo.Config; import com.ctrip.apollo.Config;
import com.ctrip.apollo.ConfigChangeListener;
import com.ctrip.apollo.spi.ConfigFactory; import com.ctrip.apollo.spi.ConfigFactory;
import com.ctrip.apollo.spi.ConfigFactoryManager; import com.ctrip.apollo.spi.ConfigFactoryManager;
......
package com.ctrip.apollo.metaservice.service; package com.ctrip.apollo.metaservice.service;
import com.ctrip.apollo.core.ServiceNameConsts; import com.ctrip.apollo.core.ServiceNameConsts;
import com.dianping.cat.Cat;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application; import com.netflix.discovery.shared.Application;
...@@ -19,16 +20,25 @@ public class DiscoveryService { ...@@ -19,16 +20,25 @@ public class DiscoveryService {
public List<InstanceInfo> getConfigServiceInstances() { public List<InstanceInfo> getConfigServiceInstances() {
Application application = eurekaClient.getApplication(ServiceNameConsts.APOLLO_CONFIGSERVICE); Application application = eurekaClient.getApplication(ServiceNameConsts.APOLLO_CONFIGSERVICE);
if (application == null) {
Cat.logEvent("Apollo.EurekaDiscovery.NotFound", ServiceNameConsts.APOLLO_CONFIGSERVICE);
}
return application != null ? application.getInstances() : new ArrayList<>(); return application != null ? application.getInstances() : new ArrayList<>();
} }
public List<InstanceInfo> getMetaServiceInstances() { public List<InstanceInfo> getMetaServiceInstances() {
Application application = eurekaClient.getApplication(ServiceNameConsts.APOLLO_METASERVICE); Application application = eurekaClient.getApplication(ServiceNameConsts.APOLLO_METASERVICE);
if (application == null) {
Cat.logEvent("Apollo.EurekaDiscovery.NotFound", ServiceNameConsts.APOLLO_METASERVICE);
}
return application != null ? application.getInstances() : new ArrayList<>(); return application != null ? application.getInstances() : new ArrayList<>();
} }
public List<InstanceInfo> getAdminServiceInstances() { public List<InstanceInfo> getAdminServiceInstances() {
Application application = eurekaClient.getApplication(ServiceNameConsts.APOLLO_ADMINSERVICE); Application application = eurekaClient.getApplication(ServiceNameConsts.APOLLO_ADMINSERVICE);
if (application == null) {
Cat.logEvent("Apollo.EurekaDiscovery.NotFound", ServiceNameConsts.APOLLO_ADMINSERVICE);
}
return application != null ? application.getInstances() : new ArrayList<>(); return application != null ? application.getInstances() : new ArrayList<>();
} }
} }
...@@ -6,8 +6,6 @@ import org.springframework.context.annotation.ComponentScan.Filter; ...@@ -6,8 +6,6 @@ import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.FilterType;
import com.ctrip.apollo.common.controller.WebSecurityConfig;
@Configuration @Configuration
@ComponentScan(excludeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE, value = { @ComponentScan(excludeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE, value = {
SampleConfigServiceApplication.class, ConfigServiceApplication.class})}) SampleConfigServiceApplication.class, ConfigServiceApplication.class})})
......
...@@ -10,7 +10,6 @@ import org.junit.Test; ...@@ -10,7 +10,6 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
......
...@@ -41,4 +41,12 @@ ...@@ -41,4 +41,12 @@
</dependency> </dependency>
<!-- end of log --> <!-- end of log -->
</dependencies> </dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project> </project>
...@@ -2,8 +2,6 @@ package com.ctrip.apollo.core.dto; ...@@ -2,8 +2,6 @@ package com.ctrip.apollo.core.dto;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.ctrip.apollo.Apollo;
import java.util.Map; import java.util.Map;
/** /**
......
local.meta=http://localhost:8080
dev.meta=${dev};
fat.meta=${fat};
fws.meta=${fws};
uat.meta=${uat};
lpt.meta=${lpt};
tools.meta=${tools};
pro.meta=${pro};
\ No newline at end of file
...@@ -15,7 +15,6 @@ import com.ctrip.apollo.core.dto.AppDTO; ...@@ -15,7 +15,6 @@ import com.ctrip.apollo.core.dto.AppDTO;
import com.ctrip.apollo.core.enums.Env; import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.exception.BadRequestException; import com.ctrip.apollo.core.exception.BadRequestException;
import com.ctrip.apollo.core.exception.ServiceException; import com.ctrip.apollo.core.exception.ServiceException;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.PortalSettings; import com.ctrip.apollo.portal.PortalSettings;
import com.ctrip.apollo.portal.api.AdminServiceAPI; import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.entity.ClusterNavTree; import com.ctrip.apollo.portal.entity.ClusterNavTree;
......
...@@ -3,15 +3,22 @@ package com.ctrip.apollo.portal.service; ...@@ -3,15 +3,22 @@ package com.ctrip.apollo.portal.service;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.MetaDomainConsts; import com.ctrip.apollo.core.MetaDomainConsts;
import com.ctrip.apollo.core.dto.ServiceDTO; import com.ctrip.apollo.core.dto.ServiceDTO;
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.exception.ServiceException; import com.ctrip.apollo.core.exception.ServiceException;
/** /**
...@@ -22,16 +29,22 @@ public class ServiceLocator { ...@@ -22,16 +29,22 @@ public class ServiceLocator {
private static final Logger logger = LoggerFactory.getLogger(ServiceLocator.class); private static final Logger logger = LoggerFactory.getLogger(ServiceLocator.class);
private static final int DEFAULT_TIMEOUT_MS = 1000;
private RestTemplate restTemplate = new RestTemplate(); private RestTemplate restTemplate = new RestTemplate();
private List<ServiceDTO> serviceCaches = new ArrayList<>(); private Map<Env, List<ServiceDTO>> serviceCaches = new ConcurrentHashMap<Env, List<ServiceDTO>>();
private final AtomicInteger adminCallCounts = new AtomicInteger(0);
private final AtomicInteger configCallCounts = new AtomicInteger(0);
public ServiceDTO getAdminService(Env env) throws ServiceException { public ServiceDTO getAdminService(Env env) throws ServiceException {
List<ServiceDTO> services = getServices(env, "admin"); List<ServiceDTO> services = getServices(env, "admin");
if (services.size() == 0) { if (services.size() == 0) {
throw new ServiceException("No available admin service"); throw new ServiceException("No available admin service");
} }
return services.get(0); return services.get(adminCallCounts.getAndIncrement() % services.size());
} }
public ServiceDTO getConfigService(Env env) throws ServiceException { public ServiceDTO getConfigService(Env env) throws ServiceException {
...@@ -39,23 +52,45 @@ public class ServiceLocator { ...@@ -39,23 +52,45 @@ public class ServiceLocator {
if (services.size() == 0) { if (services.size() == 0) {
throw new ServiceException("No available config service"); throw new ServiceException("No available config service");
} }
return services.get(0); return services.get(configCallCounts.getAndIncrement() % services.size());
} }
private List<ServiceDTO> getServices(Env env, String serviceUrl) { private List<ServiceDTO> getServices(Env env, String serviceUrl) {
String domainName = MetaDomainConsts.getDomain(env); String domainName = MetaDomainConsts.getDomain(env);
String url = domainName + "/services/" + serviceUrl; String url = domainName + "/services/" + serviceUrl;
List<ServiceDTO> serviceDtos = null;
try { try {
ServiceDTO[] services = restTemplate.getForObject(new URI(url), ServiceDTO[].class); ServiceDTO[] services = restTemplate.getForObject(new URI(url), ServiceDTO[].class);
if (services != null && services.length > 0) { if (services != null && services.length > 0) {
serviceCaches.clear(); if (!serviceCaches.containsKey(env)) {
serviceDtos = new ArrayList<ServiceDTO>();
serviceCaches.put(env, serviceDtos);
} else {
serviceDtos = serviceCaches.get(env);
serviceDtos.clear();
}
for (ServiceDTO service : services) { for (ServiceDTO service : services) {
serviceCaches.add(service); serviceDtos.add(service);
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.warn(ex.getMessage()); logger.warn(ex.getMessage());
} }
return serviceCaches; return serviceDtos;
}
@PostConstruct
private void postConstruct() {
if (restTemplate.getRequestFactory() instanceof SimpleClientHttpRequestFactory) {
SimpleClientHttpRequestFactory rf =
(SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setReadTimeout(DEFAULT_TIMEOUT_MS);
rf.setConnectTimeout(DEFAULT_TIMEOUT_MS);
} else if (restTemplate.getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
HttpComponentsClientHttpRequestFactory rf =
(HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setReadTimeout(DEFAULT_TIMEOUT_MS);
rf.setConnectTimeout(DEFAULT_TIMEOUT_MS);
}
} }
} }
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