欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud Eureka 服務(wù)注冊實現(xiàn)過程

 更新時間:2019年10月16日 09:42:11   作者:巡山小妖N  
這篇文章主要介紹了SpringCloud Eureka 服務(wù)注冊實現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

一、將服務(wù)注冊到Eureka

一個SpringBoot應(yīng)用如果要注冊到Spring Cloud環(huán)境(Greenwich.SR3版本),步驟很簡單:

pom.xml中添加啟動器:spring-cloud-starter-netflix-eureka-client;

增加配置:eureka.client.serviceUrl.defaultZone: http://localhost:8100/eureka/;

啟動應(yīng)用;

如果注冊中心正常,此時就能在注冊中心發(fā)現(xiàn)這個應(yīng)用了,如下圖紅框所示:

按照spring.factories中的配置,EurekaClientAutoConfiguration中的配置都會生效,包括下面這段代碼返回的bean:

@Bean
public DiscoveryClient discoveryClient(EurekaInstanceConfig config, EurekaClient client) {
  return new EurekaDiscoveryClient(config, client);
}

spring容器初始化時會實例化所有單例bean,就會執(zhí)行EurekaClientAutoConfiguration的discoveryClient方法獲取這個bean實例,于是就構(gòu)造了一個EurekaDiscoveryClient對象;

注意EurekaDiscoveryClient的構(gòu)造方法,第二個入?yún)⑹莄om.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 initialization
  return new CloudEurekaClient(manager, config, this.optionalArgs,this.context);
}

CloudEurekaClient的父類com.netflix.discovery.DiscoveryClient來自netflix發(fā)布的eureka-client包中,所以可以這么理解:EurekaDiscoveryClient類是個代理身份,真正的服務(wù)注冊發(fā)現(xiàn)是委托給netflix的開源包來完成的,我們可以專心的使用SpringCloud提供的服務(wù)注冊發(fā)現(xiàn)功能,只需要知道EurekaDiscoveryClient即可,真正的服務(wù)是eureka-client來完成的;

接下來需要關(guān)注com.netflix.discovery.DiscoveryClient的構(gòu)造方法,因為這里面有服務(wù)注冊的邏輯,整個構(gòu)造方法內(nèi)容太多,無需都細(xì)看,只看關(guān)鍵代碼即可;

DiscoveryClient的構(gòu)造方法中,最熟悉的應(yīng)該是下圖紅框中這段日志輸出的了:

對應(yīng)的應(yīng)用啟動日志中就有這段日志輸出,如下圖紅框:

紅框中的”us-east-1”,是默認(rèn)的region,來自配置類EurekaClientConfigBean,這里面有各種eureka相關(guān)的配置信息,以及默認(rèn)配置,如下圖:

繼續(xù)看DiscoveryClient的構(gòu)造方法,服務(wù)注冊相關(guān)的initScheduledTasks方法在此被調(diào)用,如下圖:

initScheduledTasks方法的內(nèi)容如下,請注意中文注釋:

  private void initScheduledTasks() {
    //獲取服務(wù)注冊列表信息
    if (clientConfig.shouldFetchRegistry()) {
      //服務(wù)注冊列表更新的周期時間
      int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();
      int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
      //定時更新服務(wù)注冊列表
      scheduler.schedule(
          new TimedSupervisorTask(
              "cacheRefresh",
              scheduler,
              cacheRefreshExecutor,
              registryFetchIntervalSeconds,
              TimeUnit.SECONDS,
              expBackOffBound,
              new CacheRefreshThread() //該線程執(zhí)行更新的具體邏輯
          ),
          registryFetchIntervalSeconds, TimeUnit.SECONDS);
    }
    if (clientConfig.shouldRegisterWithEureka()) {
      //服務(wù)續(xù)約的周期時間
      int renewalIntervalInSecs = instanceInfo.getLeaseInfo().getRenewalIntervalInSecs();
      int expBackOffBound = clientConfig.getHeartbeatExecutorExponentialBackOffBound();
      //應(yīng)用啟動可見此日志,內(nèi)容是:Starting heartbeat executor: renew interval is: 30
      logger.info("Starting heartbeat executor: " + "renew interval is: " + renewalIntervalInSecs);
      // 定時續(xù)約
      scheduler.schedule(
          new TimedSupervisorTask(
              "heartbeat",
              scheduler,
              heartbeatExecutor,
              renewalIntervalInSecs,
              TimeUnit.SECONDS,
              expBackOffBound,
              new HeartbeatThread() //該線程執(zhí)行續(xù)約的具體邏輯
          ),
          renewalIntervalInSecs, TimeUnit.SECONDS);

      //這個Runable中含有服務(wù)注冊的邏輯
      instanceInfoReplicator = new InstanceInfoReplicator(
          this,
          instanceInfo,
          clientConfig.getInstanceInfoReplicationIntervalSeconds(),
          2); // burstSize

      statusChangeListener = new ApplicationInfoManager.StatusChangeListener() {
        @Override
        public String getId() {
          return "statusChangeListener";
        }

        @Override
        public void notify(StatusChangeEvent statusChangeEvent) {
          if (InstanceStatus.DOWN == statusChangeEvent.getStatus() ||
              InstanceStatus.DOWN == statusChangeEvent.getPreviousStatus()) {
            // log at warn level if DOWN was involved
            logger.warn("Saw local status change event {}", statusChangeEvent);
          } else {
            logger.info("Saw local status change event {}", statusChangeEvent);
          }
          instanceInfoReplicator.onDemandUpdate();
        }
      };

      if (clientConfig.shouldOnDemandUpdateStatusChange()) {
        applicationInfoManager.registerStatusChangeListener(statusChangeListener);
      }
      //服務(wù)注冊
      instanceInfoReplicator.start(clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());
    } else {
      logger.info("Not registering with Eureka server per configuration");
    }
  }

上述代碼中有幾處需要注意,這些關(guān)鍵點在后面的章節(jié)將繼續(xù)展開:

a. 周期性更新服務(wù)列表;

b. 周期性服務(wù)續(xù)約;

c. 服務(wù)注冊邏輯被放入Runnable實現(xiàn)類InstanceInfoReplicator之中,在新線程中執(zhí)行;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論