Search in sources :

Example 11 with ServiceDTO

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

the class RemoteConfigLongPollService method doLongPollingRefresh.

private void doLongPollingRefresh(String appId, String cluster, String dataCenter, String secret) {
    final Random random = new Random();
    ServiceDTO lastServiceDto = null;
    while (!m_longPollingStopped.get() && !Thread.currentThread().isInterrupted()) {
        if (!m_longPollRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) {
            // wait at most 5 seconds
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
            }
        }
        Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "pollNotification");
        String url = null;
        try {
            if (lastServiceDto == null) {
                List<ServiceDTO> configServices = getConfigServices();
                lastServiceDto = configServices.get(random.nextInt(configServices.size()));
            }
            url = assembleLongPollRefreshUrl(lastServiceDto.getHomepageUrl(), appId, cluster, dataCenter, m_notifications);
            logger.debug("Long polling from {}", url);
            HttpRequest request = new HttpRequest(url);
            request.setReadTimeout(LONG_POLLING_READ_TIMEOUT);
            if (!StringUtils.isBlank(secret)) {
                Map<String, String> headers = Signature.buildHttpHeaders(url, appId, secret);
                request.setHeaders(headers);
            }
            transaction.addData("Url", url);
            final HttpResponse<List<ApolloConfigNotification>> response = m_httpClient.doGet(request, m_responseType);
            logger.debug("Long polling response: {}, url: {}", response.getStatusCode(), url);
            if (response.getStatusCode() == 200 && response.getBody() != null) {
                updateNotifications(response.getBody());
                updateRemoteNotifications(response.getBody());
                transaction.addData("Result", response.getBody().toString());
                notify(lastServiceDto, response.getBody());
            }
            // try to load balance
            if (response.getStatusCode() == 304 && random.nextBoolean()) {
                lastServiceDto = null;
            }
            m_longPollFailSchedulePolicyInSecond.success();
            transaction.addData("StatusCode", response.getStatusCode());
            transaction.setStatus(Transaction.SUCCESS);
        } catch (Throwable ex) {
            lastServiceDto = null;
            Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
            transaction.setStatus(ex);
            long sleepTimeInSecond = m_longPollFailSchedulePolicyInSecond.fail();
            logger.warn("Long polling failed, will retry in {} seconds. appId: {}, cluster: {}, namespaces: {}, long polling url: {}, reason: {}", sleepTimeInSecond, appId, cluster, assembleNamespaces(), url, ExceptionUtil.getDetailMessage(ex));
            try {
                TimeUnit.SECONDS.sleep(sleepTimeInSecond);
            } catch (InterruptedException ie) {
            // ignore
            }
        } finally {
            transaction.complete();
        }
    }
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) Random(java.util.Random) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) List(java.util.List)

Example 12 with ServiceDTO

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

the class RemoteConfigRepository method loadApolloConfig.

private ApolloConfig loadApolloConfig() {
    if (!m_loadConfigRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) {
        // wait at most 5 seconds
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
        }
    }
    String appId = m_configUtil.getAppId();
    String cluster = m_configUtil.getCluster();
    String dataCenter = m_configUtil.getDataCenter();
    String secret = m_configUtil.getAccessKeySecret();
    Tracer.logEvent("Apollo.Client.ConfigMeta", STRING_JOINER.join(appId, cluster, m_namespace));
    int maxRetries = m_configNeedForceRefresh.get() ? 2 : 1;
    // 0 means no sleep
    long onErrorSleepTime = 0;
    Throwable exception = null;
    List<ServiceDTO> configServices = getConfigServices();
    String url = null;
    retryLoopLabel: for (int i = 0; i < maxRetries; i++) {
        List<ServiceDTO> randomConfigServices = Lists.newLinkedList(configServices);
        Collections.shuffle(randomConfigServices);
        // Access the server which notifies the client first
        if (m_longPollServiceDto.get() != null) {
            randomConfigServices.add(0, m_longPollServiceDto.getAndSet(null));
        }
        for (ServiceDTO configService : randomConfigServices) {
            if (onErrorSleepTime > 0) {
                logger.warn("Load config failed, will retry in {} {}. appId: {}, cluster: {}, namespaces: {}", onErrorSleepTime, m_configUtil.getOnErrorRetryIntervalTimeUnit(), appId, cluster, m_namespace);
                try {
                    m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(onErrorSleepTime);
                } catch (InterruptedException e) {
                // ignore
                }
            }
            url = assembleQueryConfigUrl(configService.getHomepageUrl(), appId, cluster, m_namespace, dataCenter, m_remoteMessages.get(), m_configCache.get());
            logger.debug("Loading config from {}", url);
            HttpRequest request = new HttpRequest(url);
            if (!StringUtils.isBlank(secret)) {
                Map<String, String> headers = Signature.buildHttpHeaders(url, appId, secret);
                request.setHeaders(headers);
            }
            Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "queryConfig");
            transaction.addData("Url", url);
            try {
                HttpResponse<ApolloConfig> response = m_httpClient.doGet(request, ApolloConfig.class);
                m_configNeedForceRefresh.set(false);
                m_loadConfigFailSchedulePolicy.success();
                transaction.addData("StatusCode", response.getStatusCode());
                transaction.setStatus(Transaction.SUCCESS);
                if (response.getStatusCode() == 304) {
                    logger.debug("Config server responds with 304 HTTP status code.");
                    return m_configCache.get();
                }
                ApolloConfig result = response.getBody();
                logger.debug("Loaded config for {}: {}", m_namespace, result);
                return result;
            } catch (ApolloConfigStatusCodeException ex) {
                ApolloConfigStatusCodeException statusCodeException = ex;
                // config not found
                if (ex.getStatusCode() == 404) {
                    String message = String.format("Could not find config for namespace - appId: %s, cluster: %s, namespace: %s, " + "please check whether the configs are released in Apollo!", appId, cluster, m_namespace);
                    statusCodeException = new ApolloConfigStatusCodeException(ex.getStatusCode(), message);
                }
                Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(statusCodeException));
                transaction.setStatus(statusCodeException);
                exception = statusCodeException;
                if (ex.getStatusCode() == 404) {
                    break retryLoopLabel;
                }
            } catch (Throwable ex) {
                Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
                transaction.setStatus(ex);
                exception = ex;
            } finally {
                transaction.complete();
            }
            // if force refresh, do normal sleep, if normal config load, do exponential sleep
            onErrorSleepTime = m_configNeedForceRefresh.get() ? m_configUtil.getOnErrorRetryInterval() : m_loadConfigFailSchedulePolicy.fail();
        }
    }
    String message = String.format("Load Apollo Config failed - appId: %s, cluster: %s, namespace: %s, url: %s", appId, cluster, m_namespace, url);
    throw new ApolloConfigException(message, exception);
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) List(java.util.List) ApolloConfigStatusCodeException(com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException) Map(java.util.Map) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 13 with ServiceDTO

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

the class SystemInfoController method checkHealth.

@PreAuthorize(value = "@permissionValidator.isSuperAdmin()")
@GetMapping(value = "/health")
public Health checkHealth(@RequestParam String instanceId) {
    List<Env> allEnvs = portalSettings.getAllEnvs();
    ServiceDTO service = null;
    for (final Env env : allEnvs) {
        EnvironmentInfo envInfo = adaptEnv2EnvironmentInfo(env);
        if (envInfo.getAdminServices() != null) {
            for (final ServiceDTO s : envInfo.getAdminServices()) {
                if (instanceId.equals(s.getInstanceId())) {
                    service = s;
                    break;
                }
            }
        }
        if (envInfo.getConfigServices() != null) {
            for (final ServiceDTO s : envInfo.getConfigServices()) {
                if (instanceId.equals(s.getInstanceId())) {
                    service = s;
                    break;
                }
            }
        }
    }
    if (service == null) {
        throw new IllegalArgumentException("No such instance of instanceId: " + instanceId);
    }
    return restTemplate.getForObject(service.getHomepageUrl() + "/health", Health.class);
}
Also used : EnvironmentInfo(com.ctrip.framework.apollo.portal.entity.vo.EnvironmentInfo) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) Env(com.ctrip.framework.apollo.portal.environment.Env) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 14 with ServiceDTO

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

the class RemoteConfigRepositoryTest method setUp.

@Before
public void setUp() throws Exception {
    someNamespace = "someName";
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_NOT_MODIFIED);
    configUtil = new MockConfigUtil();
    MockInjector.setInstance(ConfigUtil.class, configUtil);
    someServerUrl = "http://someServer";
    ServiceDTO serviceDTO = mock(ServiceDTO.class);
    when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl);
    when(configServiceLocator.getConfigServices()).thenReturn(Lists.newArrayList(serviceDTO));
    MockInjector.setInstance(ConfigServiceLocator.class, configServiceLocator);
    httpClient = spy(new MockHttpClient());
    MockInjector.setInstance(HttpClient.class, httpClient);
    remoteConfigLongPollService = new RemoteConfigLongPollService();
    MockInjector.setInstance(RemoteConfigLongPollService.class, remoteConfigLongPollService);
    when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {

        @Override
        public Properties answer(InvocationOnMock invocation) {
            return new Properties();
        }
    });
    MockInjector.setInstance(PropertiesFactory.class, propertiesFactory);
    someAppId = "someAppId";
    someCluster = "someCluster";
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) Before(org.junit.Before)

Example 15 with ServiceDTO

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

the class NacosDiscoveryService method getServiceInstances.

@Override
public List<ServiceDTO> getServiceInstances(String serviceId) {
    try {
        List<Instance> instances = namingService.selectInstances(serviceId, true);
        List<ServiceDTO> serviceDTOList = Lists.newLinkedList();
        instances.forEach(instance -> {
            ServiceDTO serviceDTO = this.toServiceDTO(instance, serviceId);
            serviceDTOList.add(serviceDTO);
        });
        return serviceDTOList;
    } catch (NacosException ex) {
        logger.error(ex.getMessage(), ex);
    }
    return Collections.emptyList();
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) NacosException(com.alibaba.nacos.api.exception.NacosException)

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