How does Eureka work?
That Eureka client how to send the registration information of the local service to the remote registered server Eureka server. through the following source analysis, we can see that the Eureka client's scheduled task calls the Eureka server's reset interface, and Eureka receives the call request to process the registration of the service and the synchronization of data in the Eureka server.
Service Registration
Source analysis, see Service registration can be considered as Eureka client self-completion, do not need the service itself to care.
Eureka Client's scheduled task calls Eureka server's provider Interface
When the com.netflix.discovery.DiscoveryClient is started, a timed task is initialized, and the local service configuration information, that is, the service information that needs to be registered to the remote, is automatically refreshed to the registration server.
First look at the Eureka code, in the Spring-cloud-netflix-eureka-server project can find this dependency Eureka-client-1.4.11.jar View code can be seen,
1240 rows in Com.netflix.discovery.DiscoveryClient.java can be seen initializes all scheduled tasks, in 1277 rows, You can see Instanceinforeplicator scheduled tasks
Initializing a instanceinforeplicator in discoveryclient is actually encapsulated with timed tasks.
/*** Initializes all scheduled tasks. */ Private voidinitscheduledtasks () {if(Clientconfig.shouldfetchregistry ()) {//Registry Cache Refresh Timer intRegistryfetchintervalseconds =clientconfig.getregistryfetchintervalseconds (); intExpbackoffbound =Clientconfig.getcacherefreshexecutorexponentialbackoffbound (); Scheduler.schedule (NewTimedsupervisortask ("Cacherefresh", Scheduler, Cacherefreshexecutor, re Gistryfetchintervalseconds, Timeunit.seconds, Expbackoffbound, NewCacherefreshthread ()), Registryfetchintervalseconds, timeunit.seconds); } if(Clientconfig.shouldregisterwitheureka ()) {intRenewalintervalinsecs =instanceinfo.getleaseinfo (). Getrenewalintervalinsecs (); intExpbackoffbound =Clientconfig.getheartbeatexecutorexponentialbackoffbound (); Logger.info ("Starting Heartbeat Executor:" + "Renew interval is:" +renewalintervalinsecs); //Heartbeat TimerScheduler.schedule (NewTimedsupervisortask ("Heartbeat", Scheduler, Heartbeatexecutor, renew Alintervalinsecs, Timeunit.seconds, Expbackoffbound, NewHeartbeatthread ()), Renewalintervalinsecs, timeunit.seconds); //Instanceinfo Replicator /**The ************************ encapsulates a timed task **********************************/Instanceinforeplicator=NewInstanceinforeplicator ( This, Instanceinfo, Clientconfig.getinstanceinforeplicationintervalseconds (), 2);//burstsizeStatuschangelistener =NewApplicationinfomanager.statuschangelistener () {@Override PublicString getId () {return"Statuschangelistener"; } @Override Public voidNotify (Statuschangeevent statuschangeevent) {if(Instancestatus.down = = Statuschangeevent.getstatus () | |Instancestatus.down==Statuschangeevent.getpreviousstatus ()) { //log at warn level if off was involvedLogger.warn ("Saw Local status Change event {}", statuschangeevent); } Else{logger.info ("Saw Local status Change event {}", statuschangeevent); } instanceinforeplicator.ondemandupdate (); } }; if(Clientconfig.shouldondemandupdatestatuschange ()) {Applicationinfomanager.registerstatuschangelistene R (Statuschangelistener); } //Click to view the Start methodInstanceinforeplicator.start (Clientconfig.getinitialinstanceinforeplicationintervalseconds ()); } Else{logger.info ("Not registering with Eureka server per configuration"); } }
Eurekahttpclient: Provides a Eureka Client API approach
Spring Cloud Eureka Service Registry Source analysis