Search in sources :

Example 16 with ServiceDTO

use of com.ctrip.framework.apollo.core.dto.ServiceDTO in project apollo by ctripcorp.

the class RetryableRestTemplate method exchangeGet.

private <T> ResponseEntity<T> exchangeGet(Env env, String path, ParameterizedTypeReference<T> reference, Object... uriVariables) {
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    String uri = uriTemplateHandler.expand(path, uriVariables).getPath();
    Transaction ct = Tracer.newTransaction("AdminAPI", uri);
    ct.addData("Env", env);
    List<ServiceDTO> services = getAdminServices(env, ct);
    HttpEntity<Void> entity = new HttpEntity<>(assembleExtraHeaders(env));
    for (ServiceDTO serviceDTO : services) {
        try {
            ResponseEntity<T> result = restTemplate.exchange(parseHost(serviceDTO) + path, HttpMethod.GET, entity, reference, uriVariables);
            ct.setStatus(Transaction.SUCCESS);
            ct.complete();
            return result;
        } catch (Throwable t) {
            logger.error("Http request failed, uri: {}, method: {}", uri, HttpMethod.GET, t);
            Tracer.logError(t);
            if (canRetry(t, HttpMethod.GET)) {
                Tracer.logEvent(TracerEventType.API_RETRY, uri);
            } else {
                // biz exception rethrow
                ct.setStatus(t);
                ct.complete();
                throw t;
            }
        }
    }
    // all admin server down
    ServiceException e = new ServiceException(String.format("Admin servers are unresponsive. meta server address: %s, admin servers: %s", portalMetaDomainService.getDomain(env), services));
    ct.setStatus(e);
    ct.complete();
    throw e;
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) HttpEntity(org.springframework.http.HttpEntity) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Example 17 with ServiceDTO

use of com.ctrip.framework.apollo.core.dto.ServiceDTO in project apollo by ctripcorp.

the class RetryableRestTemplate method getAdminServices.

private List<ServiceDTO> getAdminServices(Env env, Transaction ct) {
    List<ServiceDTO> services = adminServiceAddressLocator.getServiceList(env);
    if (CollectionUtils.isEmpty(services)) {
        ServiceException e = new ServiceException(String.format("No available admin server." + " Maybe because of meta server down or all admin server down. " + "Meta server address: %s", portalMetaDomainService.getDomain(env)));
        ct.setStatus(e);
        ct.complete();
        throw e;
    }
    return services;
}
Also used : ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Example 18 with ServiceDTO

use of com.ctrip.framework.apollo.core.dto.ServiceDTO in project apollo by ctripcorp.

the class ConfigServiceLocator method updateConfigServices.

private synchronized void updateConfigServices() {
    String url = assembleMetaServiceUrl();
    HttpRequest request = new HttpRequest(url);
    int maxRetries = 2;
    Throwable exception = null;
    for (int i = 0; i < maxRetries; i++) {
        Transaction transaction = Tracer.newTransaction("Apollo.MetaService", "getConfigService");
        transaction.addData("Url", url);
        try {
            HttpResponse<List<ServiceDTO>> response = m_httpClient.doGet(request, m_responseType);
            transaction.setStatus(Transaction.SUCCESS);
            List<ServiceDTO> services = response.getBody();
            if (services == null || services.isEmpty()) {
                logConfigService("Empty response!");
                continue;
            }
            setConfigServices(services);
            return;
        } catch (Throwable ex) {
            Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
            transaction.setStatus(ex);
            exception = ex;
        } finally {
            transaction.complete();
        }
        try {
            m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(m_configUtil.getOnErrorRetryInterval());
        } catch (InterruptedException ex) {
        // ignore
        }
    }
    throw new ApolloConfigException(String.format("Get config services failed from %s", url), exception);
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) List(java.util.List) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 19 with ServiceDTO

use of com.ctrip.framework.apollo.core.dto.ServiceDTO in project apollo by ctripcorp.

the class NacosDiscoveryService method toServiceDTO.

private ServiceDTO toServiceDTO(Instance instance, String appName) {
    ServiceDTO service = new ServiceDTO();
    service.setAppName(appName);
    service.setInstanceId(instance.getInstanceId());
    String homePageUrl = "http://" + instance.getIp() + ":" + instance.getPort() + "/";
    service.setHomepageUrl(homePageUrl);
    return service;
}
Also used : ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Example 20 with ServiceDTO

use of com.ctrip.framework.apollo.core.dto.ServiceDTO in project apollo by ctripcorp.

the class KubernetesDiscoveryService method assembleServiceDTO.

private List<ServiceDTO> assembleServiceDTO(String serviceId, String directUrl) {
    if (Strings.isNullOrEmpty(directUrl)) {
        return Collections.emptyList();
    }
    List<ServiceDTO> serviceDTOList = Lists.newLinkedList();
    COMMA_SPLITTER.split(directUrl).forEach(url -> {
        ServiceDTO service = new ServiceDTO();
        service.setAppName(serviceId);
        service.setInstanceId(String.format("%s:%s", serviceId, url));
        service.setHomepageUrl(url);
        serviceDTOList.add(service);
    });
    return serviceDTOList;
}
Also used : ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Aggregations

ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)27 Test (org.junit.Test)7 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)5 InstanceInfo (com.netflix.appinfo.InstanceInfo)4 ServiceException (com.ctrip.framework.apollo.common.exception.ServiceException)3 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)3 List (java.util.List)3 Function (java.util.function.Function)3 ServiceInstance (org.springframework.cloud.client.ServiceInstance)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Instance (com.alibaba.nacos.api.naming.pojo.Instance)2 ApolloConfigException (com.ctrip.framework.apollo.exceptions.ApolloConfigException)2 Before (org.junit.Before)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)1 ApolloConfigStatusCodeException (com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException)1 EnvironmentInfo (com.ctrip.framework.apollo.portal.entity.vo.EnvironmentInfo)1 Env (com.ctrip.framework.apollo.portal.environment.Env)1 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)1 HttpResponse (com.ctrip.framework.apollo.util.http.HttpResponse)1