Class DiscoveryClient
- java.lang.Object
-
- com.netflix.discovery.DiscoveryClient
-
- All Implemented Interfaces:
EurekaClient,LookupService
@Singleton public class DiscoveryClient extends java.lang.Object implements EurekaClient
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.TheseURLs are typically amazon elastic eips which do not change. All of the functions defined above fail-over to otherURLs specified in the list in the case of failure.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classDiscoveryClient.StatsStats is used to track useful attributes of the DiscoveryClient.
-
Field Summary
Fields Modifier and Type Field Description protected EurekaClientConfigclientConfigstatic java.lang.StringHTTP_X_DISCOVERY_ALLOW_REDIRECTprotected TransportClientFactoriestransportClientFactoriesprotected EurekaTransportConfigtransportConfig
-
Constructor Summary
Constructors Constructor Description DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, TransportClientFactories transportClientFactories)DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, TransportClientFactories transportClientFactories, AbstractDiscoveryClientOptionalArgs args)DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, TransportClientFactories transportClientFactories, AbstractDiscoveryClientOptionalArgs args, EndpointRandomizer randomizer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidfireEvent(EurekaEvent event)Send the given event on the EventBus if one is availablejava.util.Set<java.lang.String>getAllKnownRegions()ApplicationgetApplication(java.lang.String appName)Returns the correspondingApplicationobject which is basically a container of all registeredappNameInstanceInfos.ApplicationInfoManagergetApplicationInfoManager()ApplicationsgetApplications()Returns theApplicationsobject which is basically a container of all currently registeredApplications.ApplicationsgetApplications(java.lang.String serviceUrl)Get all applications registered with a specific eureka service.ApplicationsgetApplicationsForARegion(java.lang.String region)java.util.List<java.lang.String>getDiscoveryServiceUrls(java.lang.String zone)Deprecated.see replacement inEndpointUtilsstatic java.util.Set<java.lang.String>getEC2DiscoveryUrlsFromZone(java.lang.String dnsName, EndpointUtils.DiscoveryUrlType type)Deprecated.see replacement inEndpointUtilsGet the list of EC2 URLs given the zone name.EurekaClientConfiggetEurekaClientConfig()static java.util.List<java.lang.String>getEurekaServiceUrlsFromConfig(java.lang.String instanceZone, boolean preferSameZone)Deprecated.usegetServiceUrlsFromConfig(String, boolean)instead.HealthCheckHandlergetHealthCheckHandler()InstanceInfo.InstanceStatusgetInstanceRemoteStatus()java.util.List<InstanceInfo>getInstancesById(java.lang.String id)Returns theListofInstanceInfos 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.longgetLastSuccessfulHeartbeatTimePeriod()longgetLastSuccessfulRegistryFetchTimePeriod()InstanceInfogetNextServerFromEureka(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.StringgetRegion()Deprecated.see replacement inEndpointUtilsGet the region that this particular instance is in.java.util.List<java.lang.String>getServiceUrlsFromConfig(java.lang.String instanceZone, boolean preferSameZone)Deprecated.see replacement inEndpointUtilsGet the list of all eureka service urls from properties file for the eureka client to talk to.java.util.List<java.lang.String>getServiceUrlsFromDNS(java.lang.String instanceZone, boolean preferSameZone)Deprecated.see replacement inEndpointUtilsGet the list of all eureka service urls from DNS for the eureka client to talk to.DiscoveryClient.StatsgetStats()Gets stats for the DiscoveryClient.static java.lang.StringgetZone(InstanceInfo myInfo)Deprecated.seeInstanceInfo.getZone(String[], com.netflix.appinfo.InstanceInfo)Get the zone that a particular instance is in.protected BackupRegistrynewBackupRegistryInstance()Deprecated.Use injection to provideBackupRegistryimplementation.protected voidonCacheRefreshed()Invoked every time the local registry cache is refreshed (whether changes have been detected or not).protected voidonRemoteStatusChanged(InstanceInfo.InstanceStatus oldStatus, InstanceInfo.InstanceStatus newStatus)Invoked when the remote status of this client has changed.voidregisterEventListener(EurekaEventListener eventListener)RegisterEurekaEventListenerwith the eureka client.voidregisterHealthCheck(HealthCheckHandler healthCheckHandler)RegisterHealthCheckHandlerwith the eureka client.voidregisterHealthCheckCallback(HealthCheckCallback callback)Deprecated.Usevoidshutdown()Shuts down Eureka Client.booleanunregisterEventListener(EurekaEventListener eventListener)Unregister aEurekaEventListenerprevious registered withEurekaClient.registerEventListener(com.netflix.discovery.EurekaEventListener)or injected into the constructor ofDiscoveryClient
-
-
-
Field Detail
-
HTTP_X_DISCOVERY_ALLOW_REDIRECT
public static final java.lang.String HTTP_X_DISCOVERY_ALLOW_REDIRECT
- See Also:
- Constant Field Values
-
clientConfig
protected final EurekaClientConfig clientConfig
-
transportConfig
protected final EurekaTransportConfig transportConfig
-
transportClientFactories
protected final TransportClientFactories transportClientFactories
-
-
Constructor Detail
-
DiscoveryClient
public DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, TransportClientFactories transportClientFactories)
-
DiscoveryClient
public DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, TransportClientFactories transportClientFactories, AbstractDiscoveryClientOptionalArgs args)
-
DiscoveryClient
public DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, TransportClientFactories transportClientFactories, AbstractDiscoveryClientOptionalArgs args, EndpointRandomizer randomizer)
-
-
Method Detail
-
getEurekaClientConfig
public EurekaClientConfig getEurekaClientConfig()
- Specified by:
getEurekaClientConfigin interfaceEurekaClient- Returns:
- the configuration of this eureka client
-
getApplicationInfoManager
public ApplicationInfoManager getApplicationInfoManager()
- Specified by:
getApplicationInfoManagerin interfaceEurekaClient- Returns:
- the application info manager of this eureka client
-
getApplication
public Application getApplication(java.lang.String appName)
Description copied from interface:LookupServiceReturns the correspondingApplicationobject which is basically a container of all registeredappNameInstanceInfos.- Specified by:
getApplicationin interfaceLookupService- Returns:
- a
Applicationor null if we couldn't locate any app of the requested appName
-
getApplications
public Applications getApplications()
Description copied from interface:LookupServiceReturns theApplicationsobject which is basically a container of all currently registeredApplications.- Specified by:
getApplicationsin interfaceLookupService- Returns:
Applications
-
getApplicationsForARegion
public Applications getApplicationsForARegion(@Nullable java.lang.String region)
- Specified by:
getApplicationsForARegionin interfaceEurekaClient- Parameters:
region- the region that the Applications reside in- Returns:
- an
Applicationsfor the matching region. a Null value is treated as the local region.
-
getAllKnownRegions
public java.util.Set<java.lang.String> getAllKnownRegions()
- Specified by:
getAllKnownRegionsin interfaceEurekaClient- Returns:
- in String form all regions (local + remote) that can be accessed by this client
-
getInstancesById
public java.util.List<InstanceInfo> getInstancesById(java.lang.String id)
Description copied from interface:LookupServiceReturns theListofInstanceInfos matching the the passed in id. A singleInstanceInfocan possibly be registered w/ more than oneApplications- Specified by:
getInstancesByIdin interfaceLookupService- Returns:
ListofInstanceInfos orCollections.emptyList()
-
registerHealthCheckCallback
@Deprecated public void registerHealthCheckCallback(HealthCheckCallback callback)
Deprecated.UseRegisterHealthCheckCallbackwith the eureka client. Once registered, the eureka client will invoke theHealthCheckCallbackin intervals specified byEurekaClientConfig.getInstanceInfoReplicationIntervalSeconds().- Specified by:
registerHealthCheckCallbackin interfaceEurekaClient- Parameters:
callback- app specific healthcheck.
-
registerHealthCheck
public void registerHealthCheck(HealthCheckHandler healthCheckHandler)
Description copied from interface:EurekaClientRegisterHealthCheckHandlerwith the eureka client. Once registered, the eureka client will first make an onDemand update of the registering instanceInfo by calling the newly registered healthcheck handler, and subsequently invoke theHealthCheckHandlerin intervals specified byEurekaClientConfig.getInstanceInfoReplicationIntervalSeconds().- Specified by:
registerHealthCheckin interfaceEurekaClient- Parameters:
healthCheckHandler- app specific healthcheck handler.
-
registerEventListener
public void registerEventListener(EurekaEventListener eventListener)
Description copied from interface:EurekaClientRegisterEurekaEventListenerwith the eureka client. Once registered, the eureka client will invokeEurekaEventListener.onEvent(com.netflix.discovery.EurekaEvent)whenever there is a change in eureka client's internal state. Use this instead of polling the client for changes.EurekaEventListener.onEvent(com.netflix.discovery.EurekaEvent)is called from the context of an internal thread and must therefore return as quickly as possible without blocking.- Specified by:
registerEventListenerin interfaceEurekaClient
-
unregisterEventListener
public boolean unregisterEventListener(EurekaEventListener eventListener)
Description copied from interface:EurekaClientUnregister aEurekaEventListenerprevious registered withEurekaClient.registerEventListener(com.netflix.discovery.EurekaEventListener)or injected into the constructor ofDiscoveryClient- Specified by:
unregisterEventListenerin interfaceEurekaClient- Returns:
- True if removed otherwise false if the listener was never registered.
-
getInstancesByVipAddress
public java.util.List<InstanceInfo> getInstancesByVipAddress(java.lang.String vipAddress, boolean secure)
Gets the list of instances matching the given VIP Address.- Specified by:
getInstancesByVipAddressin interfaceEurekaClient- 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
InstanceInfoobjects 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.- Specified by:
getInstancesByVipAddressin interfaceEurekaClient- Parameters:
vipAddress- - The VIP address to match the instances for.secure- - true if it is a secure vip address, false otherwiseregion- - region from which the instances are to be fetched. Ifnullthen local region is assumed.- Returns:
- - The list of
InstanceInfoobjects 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.- Specified by:
getInstancesByVipAddressAndAppNamein interfaceEurekaClient- 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
InstanceInfoobjects matching the criteria.
-
getNextServerFromEureka
public InstanceInfo getNextServerFromEureka(java.lang.String virtualHostname, boolean secure)
Description copied from interface:LookupServiceGets 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.UPstatus. This configuration can be controlled by overriding theEurekaClientConfig.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:
getNextServerFromEurekain interfaceLookupService- 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
InstanceInfoinformation 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.- Specified by:
getApplicationsin interfaceEurekaClient- Parameters:
serviceUrl- - The string representation of the service url.- Returns:
- - The registry information containing all applications.
-
getServiceUrlsFromConfig
@Deprecated public java.util.List<java.lang.String> getServiceUrlsFromConfig(java.lang.String instanceZone, boolean preferSameZone)Deprecated.see replacement inEndpointUtilsGet the list of all eureka service urls from properties file for the eureka client to talk to.- Specified by:
getServiceUrlsFromConfigin interfaceEurekaClient- Parameters:
instanceZone- The zone in which the client residespreferSameZone- 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
@PreDestroy public void shutdown()
Shuts down Eureka Client. Also sends a deregistration request to the eureka server.- Specified by:
shutdownin interfaceEurekaClient
-
getInstanceRemoteStatus
public InstanceInfo.InstanceStatus getInstanceRemoteStatus()
- Specified by:
getInstanceRemoteStatusin interfaceEurekaClient- Returns:
- Return he current instance status as seen on the Eureka server.
-
getServiceUrlsFromDNS
@Deprecated public java.util.List<java.lang.String> getServiceUrlsFromDNS(java.lang.String instanceZone, boolean preferSameZone)Deprecated.see replacement inEndpointUtilsGet 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.- Specified by:
getServiceUrlsFromDNSin interfaceEurekaClient- 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
@Deprecated public java.util.List<java.lang.String> getDiscoveryServiceUrls(java.lang.String zone)
Deprecated.see replacement inEndpointUtils- Specified by:
getDiscoveryServiceUrlsin interfaceEurekaClient- Parameters:
zone- the zone in which the client resides- Returns:
- The list of all eureka service urls for the eureka client to talk to.
-
getEC2DiscoveryUrlsFromZone
@Deprecated public static java.util.Set<java.lang.String> getEC2DiscoveryUrlsFromZone(java.lang.String dnsName, EndpointUtils.DiscoveryUrlType type)Deprecated.see replacement inEndpointUtilsGet the list of EC2 URLs given the zone name.- Parameters:
dnsName- The dns name of the zone-specific CNAMEtype- CNAME or EIP that needs to be retrieved- Returns:
- The list of EC2 URLs associated with the dns name
-
getHealthCheckHandler
public HealthCheckHandler getHealthCheckHandler()
- Specified by:
getHealthCheckHandlerin interfaceEurekaClient- Returns:
- the current registered healthcheck handler
-
newBackupRegistryInstance
@Deprecated @Nullable protected BackupRegistry newBackupRegistryInstance() throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException
Deprecated.Use injection to provideBackupRegistryimplementation.- Throws:
java.lang.ClassNotFoundExceptionjava.lang.IllegalAccessExceptionjava.lang.InstantiationException
-
onRemoteStatusChanged
protected void onRemoteStatusChanged(InstanceInfo.InstanceStatus oldStatus, InstanceInfo.InstanceStatus newStatus)
Invoked when the remote status of this client has changed. Subclasses may override this method to implement custom behavior if needed.- Parameters:
oldStatus- the previous remoteInstanceInfo.InstanceStatusnewStatus- the new remoteInstanceInfo.InstanceStatus
-
onCacheRefreshed
protected void onCacheRefreshed()
Invoked every time the local registry cache is refreshed (whether changes have been detected or not). Subclasses may override this method to implement custom behavior if needed.
-
fireEvent
protected void fireEvent(EurekaEvent event)
Send the given event on the EventBus if one is available- Parameters:
event- the event to send on the eventBus
-
getZone
@Deprecated public static java.lang.String getZone(InstanceInfo myInfo)
Deprecated.seeInstanceInfo.getZone(String[], com.netflix.appinfo.InstanceInfo)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
@Deprecated public static java.lang.String getRegion()
Deprecated.see replacement inEndpointUtilsGet the region that this particular instance is in.- Returns:
- - The region in which the particular instance belongs to.
-
getEurekaServiceUrlsFromConfig
@Deprecated public static java.util.List<java.lang.String> getEurekaServiceUrlsFromConfig(java.lang.String instanceZone, boolean preferSameZone)Deprecated.usegetServiceUrlsFromConfig(String, boolean)instead.
-
getLastSuccessfulHeartbeatTimePeriod
public long getLastSuccessfulHeartbeatTimePeriod()
-
getLastSuccessfulRegistryFetchTimePeriod
public long getLastSuccessfulRegistryFetchTimePeriod()
-
getStats
public DiscoveryClient.Stats getStats()
Gets stats for the DiscoveryClient.- Returns:
- The DiscoveryClientStats instance.
-
-