1. 获取eureka服务端的地址
com.netflix.discovery.endpoint.EndpointUtils#getServiceUrlsFromDNS
2. 初始化延时任务
初始化任务位于这个方法中:com.netflix.discovery.DiscoveryClient#initScheduledTasks
把延迟任务封装到这个类里面com.netflix.discovery.TimedSupervisorTask#TimedSupervisorTask,
使得每个定时任务运行结束后根据运行时间动态调整下次启动任务的时间,既起到了定时任务的作用,又起到了动态调整启动任务的时间。
public void run() {
Future future = null;
try {
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.error("task supervisor timed out", e);
timeoutCounter.increment();
//如果当前任务超时,获取超时时间*2或者最大延时时间中的最大值,作为下次的延时启动时间。
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.error("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");