Search in sources :

Example 1 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);
    MockInjector.reset();
    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);
    httpUtil = spy(new MockHttpUtil());
    MockInjector.setInstance(HttpUtil.class, httpUtil);
    remoteConfigLongPollService = new RemoteConfigLongPollService();
    MockInjector.setInstance(RemoteConfigLongPollService.class, remoteConfigLongPollService);
}
Also used : ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) Before(org.junit.Before)

Example 2 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) {
    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);
            transaction.addData("Url", url);
            final HttpResponse<List<ApolloConfigNotification>> response = m_httpUtil.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 3 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();
    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;
    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);
            Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "queryConfig");
            transaction.addData("Url", url);
            try {
                HttpResponse<ApolloConfig> response = m_httpUtil.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;
            } 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) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ApolloConfigStatusCodeException(com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 4 with ServiceDTO

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

the class ServiceController method getAdminService.

@RequestMapping("/admin")
public List<ServiceDTO> getAdminService() {
    List<InstanceInfo> instances = discoveryService.getAdminServiceInstances();
    List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {

        @Override
        public ServiceDTO apply(InstanceInfo instance) {
            ServiceDTO service = new ServiceDTO();
            service.setAppName(instance.getAppName());
            service.setInstanceId(instance.getInstanceId());
            service.setHomepageUrl(instance.getHomePageUrl());
            return service;
        }
    }).collect(Collectors.toList());
    return result;
}
Also used : Function(java.util.function.Function) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) InstanceInfo(com.netflix.appinfo.InstanceInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ServiceDTO

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

the class ServiceController method getConfigService.

@RequestMapping("/config")
public List<ServiceDTO> getConfigService(@RequestParam(value = "appId", defaultValue = "") String appId, @RequestParam(value = "ip", required = false) String clientIp) {
    List<InstanceInfo> instances = discoveryService.getConfigServiceInstances();
    List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {

        @Override
        public ServiceDTO apply(InstanceInfo instance) {
            ServiceDTO service = new ServiceDTO();
            service.setAppName(instance.getAppName());
            service.setInstanceId(instance.getInstanceId());
            service.setHomepageUrl(instance.getHomePageUrl());
            return service;
        }
    }).collect(Collectors.toList());
    return result;
}
Also used : Function(java.util.function.Function) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) InstanceInfo(com.netflix.appinfo.InstanceInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)13 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)5 ServiceException (com.ctrip.framework.apollo.common.exception.ServiceException)3 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)3 InstanceInfo (com.netflix.appinfo.InstanceInfo)3 Function (java.util.function.Function)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ApolloConfigException (com.ctrip.framework.apollo.exceptions.ApolloConfigException)2 List (java.util.List)2 Before (org.junit.Before)2 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)1 ApolloConfigStatusCodeException (com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException)1 IOException (java.io.IOException)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Request (org.eclipse.jetty.server.Request)1 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)1