com.netflix.discovery
Class DiscoveryClient

java.lang.Object
  extended by com.netflix.discovery.DiscoveryClient
All Implemented Interfaces:
LookupService

public class DiscoveryClient
extends java.lang.Object
implements LookupService

The class that is instrumental for interactions with Eureka Server.

Eureka Client is responsible for a) Registering the instance with Eureka Server b) Renewalof the lease with Eureka Server c) Cancellation of the lease from Eureka Server during shutdown

d) Querying the list of services/instances registered with Eureka Server

Eureka Client needs a configured list of Eureka Server URLs to talk to.These URLs are typically amazon elastic eips which do not change. All of the functions defined above fail-over to other URLs specified in the list in the case of failure.


Nested Class Summary
static class DiscoveryClient.DiscoveryUrlType
           
 
Field Summary
protected static EurekaClientConfig clientConfig
           
 
Method Summary
 java.util.Set<java.lang.String> getAllKnownRegions()
           
 Application getApplication(java.lang.String appName)
          Returns the corresponding Application object which is basically a container of all registered appName InstanceInfos.
 Applications getApplications()
          (non-Javadoc)
 Applications getApplications(java.lang.String serviceUrl)
          Get all applications registered with a specific eureka service.
 Applications getApplicationsForARegion(java.lang.String region)
           
 java.util.List<java.lang.String> getDiscoveryServiceUrls(java.lang.String zone)
           
static java.util.Set<java.lang.String> getEC2DiscoveryUrlsFromZone(java.lang.String dnsName, DiscoveryClient.DiscoveryUrlType type)
          Get the list of EC2 URLs given the zone name
static java.util.List<java.lang.String> getEurekaServiceUrlsFromConfig(java.lang.String instanceZone, boolean preferSameZone)
          Get the list of all eureka service urls from properties file for the eureka client to talk to
 java.util.List<InstanceInfo> getInstancesById(java.lang.String id)
          Returns the List of InstanceInfos matching the the passed in id.
 java.util.List<InstanceInfo> getInstancesByVipAddress(java.lang.String vipAddress, boolean secure)
          Gets the list of instances matching the given VIP Address.
 java.util.List<InstanceInfo> getInstancesByVipAddress(java.lang.String vipAddress, boolean secure, java.lang.String region)
          Gets the list of instances matching the given VIP Address in the passed region.
 java.util.List<InstanceInfo> getInstancesByVipAddressAndAppName(java.lang.String vipAddress, java.lang.String appName, boolean secure)
          Gets the list of instances matching the given VIP Address and the given application name if both of them are not null.
 InstanceInfo getNextServerFromEureka(java.lang.String virtualHostname, boolean secure)
          Gets the next possible server to process the requests from the registry information received from eureka.
static java.lang.String getRegion()
          Get the region that this particular instance is in.
 java.util.List<java.lang.String> getServiceUrlsFromDNS(java.lang.String instanceZone, boolean preferSameZone)
          Get the list of all eureka service urls from DNS for the eureka client to talk to.
static java.lang.String getZone(InstanceInfo myInfo)
          Get the zone that a particular instance is in.
protected  BackupRegistry newBackupRegistryInstance()
           
 void registerHealthCheckCallback(HealthCheckCallback callback)
          Register HealthCheckCallback with the eureka client.
 void shutdown()
          Shuts down Eureka Client.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clientConfig

protected static EurekaClientConfig clientConfig
Method Detail

getApplication

public Application getApplication(java.lang.String appName)
Description copied from interface: LookupService
Returns the corresponding Application object which is basically a container of all registered appName InstanceInfos.

Specified by:
getApplication in interface LookupService
Returns:
a Application or null if we couldn't locate any app of the requested appName

getApplications

public Applications getApplications()
(non-Javadoc)

Specified by:
getApplications in interface LookupService
Returns:
Applications
See Also:
LookupService.getApplications()

getApplicationsForARegion

public Applications getApplicationsForARegion(@Nullable
                                              java.lang.String region)

getAllKnownRegions

public java.util.Set<java.lang.String> getAllKnownRegions()

getInstancesById

public java.util.List<InstanceInfo> getInstancesById(java.lang.String id)
Description copied from interface: LookupService
Returns the List of InstanceInfos matching the the passed in id. A single InstanceInfo can possibly be registered w/ more than one Applications

Specified by:
getInstancesById in interface LookupService
Returns:
List of InstanceInfos or Collections.emptyList()

registerHealthCheckCallback

public void registerHealthCheckCallback(HealthCheckCallback callback)
Register HealthCheckCallback with the eureka client. Once registered, the eureka client will invoke the HealthCheckCallback in intervals specified by EurekaClientConfig.getInstanceInfoReplicationIntervalSeconds().

Parameters:
callback - -- app specific healthcheck.

getInstancesByVipAddress

public java.util.List<InstanceInfo> getInstancesByVipAddress(java.lang.String vipAddress,
                                                             boolean secure)
Gets the list of instances matching the given VIP Address.

Parameters:
vipAddress - - The VIP address to match the instances for.
secure - - true if it is a secure vip address, false otherwise
Returns:
- The list of InstanceInfo objects matching the criteria

getInstancesByVipAddress

public java.util.List<InstanceInfo> getInstancesByVipAddress(java.lang.String vipAddress,
                                                             boolean secure,
                                                             @Nullable
                                                             java.lang.String region)
Gets the list of instances matching the given VIP Address in the passed region.

Parameters:
vipAddress - - The VIP address to match the instances for.
secure - - true if it is a secure vip address, false otherwise
region - - region from which the instances are to be fetched. If null then local region is assumed.
Returns:
- The list of InstanceInfo objects matching the criteria, empty list if not instances found.

getInstancesByVipAddressAndAppName

public java.util.List<InstanceInfo> getInstancesByVipAddressAndAppName(java.lang.String vipAddress,
                                                                       java.lang.String appName,
                                                                       boolean secure)
Gets the list of instances matching the given VIP Address and the given application name if both of them are not null. If one of them is null, then that criterion is completely ignored for matching instances.

Parameters:
vipAddress - - The VIP address to match the instances for.
appName - - The applicationName to match the instances for.
secure - - true if it is a secure vip address, false otherwise.
Returns:
- The list of InstanceInfo objects matching the criteria.

getNextServerFromEureka

public InstanceInfo getNextServerFromEureka(java.lang.String virtualHostname,
                                            boolean secure)
Description copied from interface: LookupService
Gets the next possible server to process the requests from the registry information received from eureka.

The next server is picked on a round-robin fashion.By default, this method just returns the servers that are currently with InstanceInfo.InstanceStatus.UP status. This configuration can be controlled by overriding the EurekaClientConfig.shouldFilterOnlyUpInstances(). Note that in some cases (Eureka emergency mode situation), the instances that are returned may not be unreachable, it is solely up to the client at that point to timeout quickly and retry the next server.

Specified by:
getNextServerFromEureka in interface LookupService
Parameters:
virtualHostname - the virtual host name that is associated to the servers.
secure - indicates whether this is a HTTP or a HTTPS request - secure means HTTPS.
Returns:
the InstanceInfo information which contains the public host name of the next server in line to process the request based on the round-robin algorithm.

getApplications

public Applications getApplications(java.lang.String serviceUrl)
Get all applications registered with a specific eureka service.

Parameters:
serviceUrl - - The string representation of the service url.
Returns:
- The registry information containing all applications.

getEurekaServiceUrlsFromConfig

public static java.util.List<java.lang.String> getEurekaServiceUrlsFromConfig(java.lang.String instanceZone,
                                                                              boolean preferSameZone)
Get the list of all eureka service urls from properties file for the eureka client to talk to

Parameters:
instanceZone - - The zone in which the client resides
preferSameZone - - true if we have to prefer the same zone as the client, false otherwise
Returns:
- The list of all eureka service urls for the eureka client to talk to

shutdown

public void shutdown()
Shuts down Eureka Client. Also sends a deregistration request to the eureka server.


getServiceUrlsFromDNS

public java.util.List<java.lang.String> getServiceUrlsFromDNS(java.lang.String instanceZone,
                                                              boolean preferSameZone)
Get the list of all eureka service urls from DNS for the eureka client to talk to. The client picks up the service url from its zone and then fails over to other zones randomly. If there are multiple servers in the same zone, the client once again picks one randomly. This way the traffic will be distributed in the case of failures.

Parameters:
instanceZone - - The zone in which the client resides.
preferSameZone - - true if we have to prefer the same zone as the client, false otherwise.
Returns:
- The list of all eureka service urls for the eureka client to talk to.

getDiscoveryServiceUrls

public java.util.List<java.lang.String> getDiscoveryServiceUrls(java.lang.String zone)

getZone

public static java.lang.String getZone(InstanceInfo myInfo)
Get the zone that a particular instance is in.

Parameters:
myInfo - - The InstanceInfo object of the instance.
Returns:
- The zone in which the particular instance belongs to.

getRegion

public static java.lang.String getRegion()
Get the region that this particular instance is in.

Returns:
- The region in which the particular instance belongs to.

getEC2DiscoveryUrlsFromZone

public static java.util.Set<java.lang.String> getEC2DiscoveryUrlsFromZone(java.lang.String dnsName,
                                                                          DiscoveryClient.DiscoveryUrlType type)
Get the list of EC2 URLs given the zone name

Parameters:
dnsName - - The dns name of the zone-specific CNAME
type - - CNAME or EIP that needs to be retrieved
Returns:
- The list of EC2 URLs associated with the dns name

newBackupRegistryInstance

@Nullable
protected BackupRegistry newBackupRegistryInstance()
                                            throws java.lang.ClassNotFoundException,
                                                   java.lang.IllegalAccessException,
                                                   java.lang.InstantiationException
Throws:
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException