Spring-Cloud学习(四)服务注册中心Eureka-Client端原理分析

一、spring-boot自动加载Eureka-Client端入口

二、EurekaClientAutoConfiguration类做了那些准备工作

  • 2.1 初始化EurekaClientConfig接口的实现类EurekaClientConfigBean

  • 2.2 初始化EurekaClient接口的实现类DiscoveryClient

三、DiscoveryClient类@Inject注解标识的方法

@Inject
com.netflix.discovery.DiscoveryClient#DiscoveryClient(
	com.netflix.appinfo.ApplicationInfoManager, 
	com.netflix.discovery.EurekaClientConfig, 
	com.netflix.discovery.AbstractDiscoveryClientOptionalArgs, 
	javax.inject.Provider<com.netflix.discovery.BackupRegistry>)
//用来创建延迟指定时间后执行某个任务的操作,一次性执行任务,执行完成后结束。
//command 等待被执行的任务 
//delay 任务执行延迟时间
//unit 时间单位
public ScheduledFuture<?> schedule(Runnable command,
                                       long delay,
                                       TimeUnit unit) 

 @Override
    public void run() {
        Future<?> future = null;
        try {
			//提交定时任务,1 定时更新服务注册表 2 定时维持服务续约
            future = executor.submit(task);
            threadPoolLevelGauge.set((long) executor.getActiveCount());
			// 获取定时任务执行结果,如果超时,则捕获超时异常
            future.get(timeoutMillis, TimeUnit.MILLISECONDS);  // block until done or timeout
			// 恢复定时任务的超时时间
            delay.set(timeoutMillis);
            threadPoolLevelGauge.set((long) executor.getActiveCount());
        } catch (TimeoutException e) {
            logger.warn("task supervisor timed out", e);
            timeoutCounter.increment();
			//延长定时任务时间,保证可以获取到定时任务的执行结果
            long currentDelay = delay.get();
            long newDelay = Math.min(maxDelay, currentDelay * 2);
            delay.compareAndSet(currentDelay, newDelay);

        } catch (RejectedExecutionException e) {
            if (executor.isShutdown() || scheduler.isShutdown()) {
                logger.warn("task supervisor shutting down, reject the task", e);
            } else {
                logger.warn("task supervisor rejected the task", e);
            }

            rejectedCounter.increment();
        } catch (Throwable e) {
            if (executor.isShutdown() || scheduler.isShutdown()) {
                logger.warn("task supervisor shutting down, can't accept the task");
            } else {
                logger.warn("task supervisor threw an exception", e);
            }

            throwableCounter.increment();
        } finally {
            if (future != null) {
                future.cancel(true);
            }
			//如果定时器没有关闭,即Eureka Client端没有关闭,则该任务继续执行
            if (!scheduler.isShutdown()) {
                scheduler.schedule(this, delay.get(), TimeUnit.MILLISECONDS);
            }
        }
    }

4、分析定时更新服务注册线程 

#定时任务的时间默认是30s,可以修改其值来改变定时任务的时间
eureka.client.registry-fetch-interval-seconds=30
#如果定时任务在30s内没有执行完成,需要扩大执行时间  30 * 2 --> 30 * 2 * 2 --> ...
#下面的值是上限的超时时间的,即定时任务的执行时间不能超过下面的值
#指定的倍率 即 30 * 10 = 300
eureka.client.cache-refresh-executor-exponential-back-off-bound=10
#是否禁用增量更新,如果禁用全部使用全量更新注册表信息
eureka.client.disable-delta=false

5、分析定时进行服务续约流程

//定时任务的默认时间是30s,可以通过配置下面的属性来修改
eureka.instance.leaseRenewallIntervalInSeconds = 30

6、服务注册流程分析

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值