javascript
SpringCloud-Eureka-服务注册是如何发起的
原文:https://xsxy007.github.io
Spring Cloud環境下,服務提供者和消費者啟動后都會將自身注冊到Eureka
一、將服務注冊到Eureka
一個SpringBoot應用如果要注冊到Spring Cloud環境(Greenwich.SR3版本),步驟很簡單:
pom.xml中添加啟動器:spring-cloud-starter-netflix-eureka-client;
增加配置:eureka.client.serviceUrl.defaultZone: http://localhost:8100/eureka/;
啟動應用;
如果注冊中心正常,此時就能在注冊中心發現這個應用了,如下圖紅框所示:
按照spring.factories中的配置,EurekaClientAutoConfiguration中的配置都會生效,包括下面這段代碼返回的bean:
- @Bean public DiscoveryClient discoveryClient(EurekaInstanceConfig config, EurekaClient client) {return new EurekaDiscoveryClient(config, client); }
spring容器初始化時會實例化所有單例bean,就會執行EurekaClientAutoConfiguration的discoveryClient方法獲取這個bean實例,于是就構造了一個EurekaDiscoveryClient對象;
注意EurekaDiscoveryClient的構造方法,第二個入參是com.netflix.discovery.EurekaClient類型,此對象同樣來自EurekaClientAutoConfiguration類,如下方法:
- @Bean(destroyMethod = "shutdown") @ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT) @org.springframework.cloud.context.config.annotation.RefreshScope @Lazy public EurekaClient eurekaClient(ApplicationInfoManager manager, EurekaClientConfig config, EurekaInstanceConfig instance) {manager.getInfo(); // force initializationreturn new CloudEurekaClient(manager, config, this.optionalArgs,this.context); }
CloudEurekaClient的父類com.netflix.discovery.DiscoveryClient來自netflix發布的eureka-client包中,所以可以這么理解:EurekaDiscoveryClient類是個代理身份,真正的服務注冊發現是委托給netflix的開源包來完成的,我們可以專心的使用SpringCloud提供的服務注冊發現功能,只需要知道EurekaDiscoveryClient即可,真正的服務是eureka-client來完成的;
接下來需要關注com.netflix.discovery.DiscoveryClient的構造方法,因為這里面有服務注冊的邏輯,整個構造方法內容太多,無需都細看,只看關鍵代碼即可;
DiscoveryClient的構造方法中,最熟悉的應該是下圖紅框中這段日志輸出的了:
對應的應用啟動日志中就有這段日志輸出,如下圖紅框:
紅框中的”us-east-1”,是默認的region,來自配置類EurekaClientConfigBean,這里面有各種eureka相關的配置信息,以及默認配置,如下圖:
繼續看DiscoveryClient的構造方法,服務注冊相關的initScheduledTasks方法在此被調用,如下圖:
initScheduledTasks方法的內容如下,請注意中文注釋:
private void initScheduledTasks() {//獲取服務注冊列表信息if (clientConfig.shouldFetchRegistry()) {//服務注冊列表更新的周期時間int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();//定時更新服務注冊列表scheduler.schedule(new TimedSupervisorTask("cacheRefresh",scheduler,cacheRefreshExecutor,registryFetchIntervalSeconds,TimeUnit.SECONDS,expBackOffBound,new CacheRefreshThread() //該線程執行更新的具體邏輯),registryFetchIntervalSeconds, TimeUnit.SECONDS);}if (clientConfig.shouldRegisterWithEureka()) {//服務續約的周期時間int renewalIntervalInSecs = instanceInfo.getLeaseInfo().getRenewalIntervalInSecs();int expBackOffBound = clientConfig.getHeartbeatExecutorExponentialBackOffBound();//應用啟動可見此日志,內容是:Starting heartbeat executor: renew interval is: 30logger.info("Starting heartbeat executor: " + "renew interval is: " + renewalIntervalInSecs);// 定時續約scheduler.schedule(new TimedSupervisorTask("heartbeat",scheduler,heartbeatExecutor,renewalIntervalInSecs,TimeUnit.SECONDS,expBackOffBound,new HeartbeatThread() //該線程執行續約的具體邏輯),renewalIntervalInSecs, TimeUnit.SECONDS);//這個Runable中含有服務注冊的邏輯instanceInfoReplicator = new InstanceInfoReplicator(this,instanceInfo,clientConfig.getInstanceInfoReplicationIntervalSeconds(),2); // burstSizestatusChangeListener = new ApplicationInfoManager.StatusChangeListener() {@Overridepublic String getId() {return "statusChangeListener";}@Overridepublic void notify(StatusChangeEvent statusChangeEvent) {if (InstanceStatus.DOWN == statusChangeEvent.getStatus() ||InstanceStatus.DOWN == statusChangeEvent.getPreviousStatus()) {// log at warn level if DOWN was involvedlogger.warn("Saw local status change event {}", statusChangeEvent);} else {logger.info("Saw local status change event {}", statusChangeEvent);}instanceInfoReplicator.onDemandUpdate();}};if (clientConfig.shouldOnDemandUpdateStatusChange()) {applicationInfoManager.registerStatusChangeListener(statusChangeListener);}//服務注冊instanceInfoReplicator.start(clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());} else {logger.info("Not registering with Eureka server per configuration");}} 上述代碼中有幾處需要注意,這些關鍵點在后面的章節將繼續展開: a. 周期性更新服務列表; b. 周期性服務續約;c. 服務注冊邏輯被放入Runnable實現類InstanceInfoReplicator之中,在新線程中執行;
總結
以上是生活随笔為你收集整理的SpringCloud-Eureka-服务注册是如何发起的的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud-Eureka-P
- 下一篇: Springcloud中的region和