一、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