Search in sources :

Example 51 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class ApplicationsXmlBeanSerializer method serializeFields.

@Override
protected void serializeFields(Object bean, JsonGenerator jgen0, SerializerProvider provider) throws IOException {
    super.serializeFields(bean, jgen0, provider);
    Applications applications = (Applications) bean;
    if (applications.getVersion() != null) {
        jgen0.writeStringField(versionKey, Long.toString(applications.getVersion()));
    }
    if (applications.getAppsHashCode() != null) {
        jgen0.writeStringField(appsHashCodeKey, applications.getAppsHashCode());
    }
}
Also used : Applications(com.netflix.discovery.shared.Applications)

Example 52 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class DiscoveryClient method fetchRegistryFromBackup.

/**
     * Fetch the registry information from back up registry if all eureka server
     * urls are unreachable.
     */
private void fetchRegistryFromBackup() {
    try {
        @SuppressWarnings("deprecation") BackupRegistry backupRegistryInstance = newBackupRegistryInstance();
        if (null == backupRegistryInstance) {
            // backward compatibility with the old protected method, in case it is being used.
            backupRegistryInstance = backupRegistryProvider.get();
        }
        if (null != backupRegistryInstance) {
            Applications apps = null;
            if (isFetchingRemoteRegionRegistries()) {
                String remoteRegionsStr = remoteRegionsToFetch.get();
                if (null != remoteRegionsStr) {
                    apps = backupRegistryInstance.fetchRegistry(remoteRegionsStr.split(","));
                }
            } else {
                apps = backupRegistryInstance.fetchRegistry();
            }
            if (apps != null) {
                final Applications applications = this.filterAndShuffle(apps);
                applications.setAppsHashCode(applications.getReconcileHashCode());
                localRegionApps.set(applications);
                logTotalInstances();
                logger.info("Fetched registry successfully from the backup");
            }
        } else {
            logger.warn("No backup registry instance defined & unable to find any discovery servers.");
        }
    } catch (Throwable e) {
        logger.warn("Cannot fetch applications from apps although backup registry was specified", e);
    }
}
Also used : Applications(com.netflix.discovery.shared.Applications)

Example 53 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class DiscoveryClient method reconcileAndLogDifference.

/**
     * Reconcile the eureka server and client registry information and logs the differences if any.
     * When reconciling, the following flow is observed:
     *
     * make a remote call to the server for the full registry
     * calculate and log differences
     * if (update generation have not advanced (due to another thread))
     *   atomically set the registry to the new registry
     * fi
     *
     * @param delta
     *            the last delta registry information received from the eureka
     *            server.
     * @param reconcileHashCode
     *            the hashcode generated by the server for reconciliation.
     * @return ClientResponse the HTTP response object.
     * @throws Throwable
     *             on any error.
     */
private void reconcileAndLogDifference(Applications delta, String reconcileHashCode) throws Throwable {
    logger.debug("The Reconcile hashcodes do not match, client : {}, server : {}. Getting the full registry", reconcileHashCode, delta.getAppsHashCode());
    RECONCILE_HASH_CODES_MISMATCH.increment();
    long currentUpdateGeneration = fetchRegistryGeneration.get();
    EurekaHttpResponse<Applications> httpResponse = clientConfig.getRegistryRefreshSingleVipAddress() == null ? eurekaTransport.queryClient.getApplications(remoteRegionsRef.get()) : eurekaTransport.queryClient.getVip(clientConfig.getRegistryRefreshSingleVipAddress(), remoteRegionsRef.get());
    Applications serverApps = httpResponse.getEntity();
    if (serverApps == null) {
        logger.warn("Cannot fetch full registry from the server; reconciliation failure");
        return;
    }
    if (logger.isDebugEnabled()) {
        try {
            Map<String, List<String>> reconcileDiffMap = getApplications().getReconcileMapDiff(serverApps);
            StringBuilder reconcileBuilder = new StringBuilder("");
            for (Map.Entry<String, List<String>> mapEntry : reconcileDiffMap.entrySet()) {
                reconcileBuilder.append(mapEntry.getKey()).append(": ");
                for (String displayString : mapEntry.getValue()) {
                    reconcileBuilder.append(displayString);
                }
                reconcileBuilder.append('\n');
            }
            String reconcileString = reconcileBuilder.toString();
            logger.debug("The reconcile string is {}", reconcileString);
        } catch (Throwable e) {
            logger.error("Could not calculate reconcile string ", e);
        }
    }
    if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
        localRegionApps.set(this.filterAndShuffle(serverApps));
        getApplications().setVersion(delta.getVersion());
        logger.debug("The Reconcile hashcodes after complete sync up, client : {}, server : {}.", getApplications().getReconcileHashCode(), delta.getAppsHashCode());
    } else {
        logger.warn("Not setting the applications map as another thread has advanced the update generation");
    }
}
Also used : Applications(com.netflix.discovery.shared.Applications) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 54 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class AbstractJersey2EurekaHttpClient method getApplicationsInternal.

private EurekaHttpResponse<Applications> getApplicationsInternal(String urlPath, String[] regions) {
    Response response = null;
    try {
        WebTarget webTarget = jerseyClient.target(serviceUrl).path(urlPath);
        if (regions != null && regions.length > 0) {
            webTarget = webTarget.queryParam("regions", StringUtil.join(regions));
        }
        Builder requestBuilder = webTarget.request();
        addExtraProperties(requestBuilder);
        addExtraHeaders(requestBuilder);
        response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).get();
        Applications applications = null;
        if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) {
            applications = response.readEntity(Applications.class);
        }
        return anEurekaHttpResponse(response.getStatus(), applications).headers(headersOf(response)).build();
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("Jersey2 HTTP GET {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus());
        }
        if (response != null) {
            response.close();
        }
    }
}
Also used : EurekaHttpResponse(com.netflix.discovery.shared.transport.EurekaHttpResponse) EurekaHttpResponse.anEurekaHttpResponse(com.netflix.discovery.shared.transport.EurekaHttpResponse.anEurekaHttpResponse) Response(javax.ws.rs.core.Response) Applications(com.netflix.discovery.shared.Applications) EurekaHttpResponseBuilder(com.netflix.discovery.shared.transport.EurekaHttpResponse.EurekaHttpResponseBuilder) Builder(javax.ws.rs.client.Invocation.Builder) WebTarget(javax.ws.rs.client.WebTarget)

Example 55 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class EurekaEventListenerTest method testCacheRefreshEvent.

@Test
public void testCacheRefreshEvent() throws Exception {
    CapturingEurekaEventListener listener = new CapturingEurekaEventListener();
    Applications initialApps = toApplications(discoveryClientResource.getMyInstanceInfo());
    when(requestHandler.getApplications()).thenReturn(anEurekaHttpResponse(200, initialApps).type(MediaType.APPLICATION_JSON_TYPE).build());
    DiscoveryClient client = (DiscoveryClient) discoveryClientResource.getClient();
    client.registerEventListener(listener);
    client.refreshRegistry();
    assertNotNull(listener.event);
    assertThat(listener.event, is(instanceOf(CacheRefreshedEvent.class)));
}
Also used : Applications(com.netflix.discovery.shared.Applications) EurekaEntityFunctions.toApplications(com.netflix.discovery.util.EurekaEntityFunctions.toApplications) Test(org.junit.Test)

Aggregations

Applications (com.netflix.discovery.shared.Applications)85 Test (org.junit.Test)43 Application (com.netflix.discovery.shared.Application)25 InstanceInfo (com.netflix.appinfo.InstanceInfo)22 EurekaEntityFunctions.toApplications (com.netflix.discovery.util.EurekaEntityFunctions.toApplications)11 EurekaEntityFunctions.mergeApplications (com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications)10 EurekaEntityFunctions.copyApplications (com.netflix.discovery.util.EurekaEntityFunctions.copyApplications)8 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)6 List (java.util.List)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)5 InstanceInfoGenerator (com.netflix.discovery.util.InstanceInfoGenerator)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Response (javax.ws.rs.core.Response)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Map (java.util.Map)3