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;
}
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));
}
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));
}
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));
}
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());
}
Aggregations