Search in sources :

Example 31 with Applications

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

the class AbstractInstanceRegistry method getApplicationsFromMultipleRegions.

/**
     * This method will return applications with instances from all passed remote regions as well as the current region.
     * Thus, this gives a union view of instances from multiple regions. <br/>
     * The application instances for which this union will be done can be restricted to the names returned by
     * {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)} for every region. In case, there is no whitelist
     * defined for a region, this method will also look for a global whitelist by passing <code>null</code> to the
     * method {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)} <br/>
     * If you are not selectively requesting for a remote region, use {@link #getApplicationsFromAllRemoteRegions()}
     * or {@link #getApplicationsFromLocalRegionOnly()}
     *
     * @param remoteRegions The remote regions for which the instances are to be queried. The instances may be limited
     *                      by a whitelist as explained above. If <code>null</code> or empty no remote regions are
     *                      included.
     *
     * @return The applications with instances from the passed remote regions as well as local region. The instances
     * from remote regions can be only for certain whitelisted apps as explained above.
     */
public Applications getApplicationsFromMultipleRegions(String[] remoteRegions) {
    boolean includeRemoteRegion = null != remoteRegions && remoteRegions.length != 0;
    logger.debug("Fetching applications registry with remote regions: {}, Regions argument {}", includeRemoteRegion, Arrays.toString(remoteRegions));
    if (includeRemoteRegion) {
        GET_ALL_WITH_REMOTE_REGIONS_CACHE_MISS.increment();
    } else {
        GET_ALL_CACHE_MISS.increment();
    }
    Applications apps = new Applications();
    apps.setVersion(1L);
    for (Entry<String, Map<String, Lease<InstanceInfo>>> entry : registry.entrySet()) {
        Application app = null;
        if (entry.getValue() != null) {
            for (Entry<String, Lease<InstanceInfo>> stringLeaseEntry : entry.getValue().entrySet()) {
                Lease<InstanceInfo> lease = stringLeaseEntry.getValue();
                if (app == null) {
                    app = new Application(lease.getHolder().getAppName());
                }
                app.addInstance(decorateInstanceInfo(lease));
            }
        }
        if (app != null) {
            apps.addApplication(app);
        }
    }
    if (includeRemoteRegion) {
        for (String remoteRegion : remoteRegions) {
            RemoteRegionRegistry remoteRegistry = regionNameVSRemoteRegistry.get(remoteRegion);
            if (null != remoteRegistry) {
                Applications remoteApps = remoteRegistry.getApplications();
                for (Application application : remoteApps.getRegisteredApplications()) {
                    if (shouldFetchFromRemoteRegistry(application.getName(), remoteRegion)) {
                        logger.info("Application {}  fetched from the remote region {}", application.getName(), remoteRegion);
                        Application appInstanceTillNow = apps.getRegisteredApplications(application.getName());
                        if (appInstanceTillNow == null) {
                            appInstanceTillNow = new Application(application.getName());
                            apps.addApplication(appInstanceTillNow);
                        }
                        for (InstanceInfo instanceInfo : application.getInstances()) {
                            appInstanceTillNow.addInstance(instanceInfo);
                        }
                    } else {
                        logger.debug("Application {} not fetched from the remote region {} as there exists a " + "whitelist and this app is not in the whitelist.", application.getName(), remoteRegion);
                    }
                }
            } else {
                logger.warn("No remote registry available for the remote region {}", remoteRegion);
            }
        }
    }
    apps.setAppsHashCode(apps.getReconcileHashCode());
    return apps;
}
Also used : Applications(com.netflix.discovery.shared.Applications) Lease(com.netflix.eureka.lease.Lease) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InstanceInfo(com.netflix.appinfo.InstanceInfo) Application(com.netflix.discovery.shared.Application)

Example 32 with Applications

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

the class XmlCodecTest method testEncodingDecodingWithMetaData.

@Test
public void testEncodingDecodingWithMetaData() throws Exception {
    Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(true).build().toApplications();
    XStream xstream = XmlXStream.getInstance();
    String xmlDocument = xstream.toXML(applications);
    Applications decodedApplications = (Applications) xstream.fromXML(xmlDocument);
    assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true));
}
Also used : Applications(com.netflix.discovery.shared.Applications) XStream(com.thoughtworks.xstream.XStream) Test(org.junit.Test)

Example 33 with Applications

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

the class XmlCodecTest method testEncodingDecodingWithoutMetaData.

@Test
public void testEncodingDecodingWithoutMetaData() throws Exception {
    Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(false).build().toApplications();
    XStream xstream = XmlXStream.getInstance();
    String xmlDocument = xstream.toXML(applications);
    Applications decodedApplications = (Applications) xstream.fromXML(xmlDocument);
    assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true));
}
Also used : Applications(com.netflix.discovery.shared.Applications) XStream(com.thoughtworks.xstream.XStream) Test(org.junit.Test)

Example 34 with Applications

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

the class EurekaJsonAndXmlJacksonCodecTest method doApplicationsEncodeDecode.

private void doApplicationsEncodeDecode(AbstractEurekaJacksonCodec codec) throws Exception {
    Applications applications = infoGenerator.takeDelta(2);
    String encodedString = codec.getObjectMapper(Applications.class).writeValueAsString(applications);
    Applications decodedValue = codec.getObjectMapper(Applications.class).readValue(encodedString, Applications.class);
    assertThat(EurekaEntityComparators.equal(applications, decodedValue), is(true));
}
Also used : Applications(com.netflix.discovery.shared.Applications)

Example 35 with Applications

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

the class InstanceRegistryTest method testGetAppsFromBothRegions.

@Test
public void testGetAppsFromBothRegions() throws Exception {
    registerInstanceLocally(createRemoteInstance(LOCAL_REGION_INSTANCE_2_HOSTNAME));
    registerInstanceLocally(createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME));
    Applications apps = registry.getApplicationsFromAllRemoteRegions();
    List<Application> registeredApplications = apps.getRegisteredApplications();
    Assert.assertEquals("Apps size from both regions do not match", 2, registeredApplications.size());
    Application locaApplication = null;
    Application remApplication = null;
    for (Application registeredApplication : registeredApplications) {
        if (registeredApplication.getName().equalsIgnoreCase(LOCAL_REGION_APP_NAME)) {
            locaApplication = registeredApplication;
        }
        if (registeredApplication.getName().equalsIgnoreCase(REMOTE_REGION_APP_NAME)) {
            remApplication = registeredApplication;
        }
    }
    Assert.assertNotNull("Did not find local registry app", locaApplication);
    Assert.assertEquals("Local registry app instance count not as expected.", 1, locaApplication.getInstances().size());
    Assert.assertNotNull("Did not find remote registry app", remApplication);
    Assert.assertEquals("Remote registry app instance count not as expected.", 2, remApplication.getInstances().size());
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) 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