// 初始化各种定时任务
private void initScheduledTasks() {
// 从EurekaServer拉取注册信息
// 从eureka.client.fetch-registry参数设置进来,默认为ture
if (clientConfig.shouldFetchRegistry()) {
// registry cache refresh timer
// 从EurekaServer拉取注册信息的频率,默认30秒
int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();
int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
// 30秒后延时执行TimedSupervisorTask
// 实际是拉取并刷新注册信息的定时任务,后面会有详细解析
scheduler.schedule(
new TimedSupervisorTask(
"cacheRefresh",
scheduler,
cacheRefreshExecutor,
registryFetchIntervalSeconds,
TimeUnit.SECONDS,
expBackOffBound,
// new一个刷新注册信息线程(全量/差别)
new CacheRefreshThread()
),
registryFetchIntervalSeconds, TimeUnit.SECONDS);
}
// 服务续约(心跳)
// 参数eureka.registration.enabled为true
if (clientConfig.shouldRegisterWithEureka()) {
// 10秒
int expBackOffBound = clientConfig.getHeartbeatExecutorExponentialBackOffBound();
// 心跳(续约)频率,默认30秒
int renewalIntervalInSecs = instanceInfo.getLeaseInfo().getRenewalIntervalInSecs();
logger.info("Starting heartbeat executor: " + "renew interval is: {}", renewalIntervalInSecs);
// Heartbeat timer
// 30秒后延时执行TimedSupervisorTask
// 实际是一个心跳(续约)定时任务,后面会有详细解析
scheduler.schedule(
new TimedSupervisorTask(
"heartbeat",
scheduler,
heartbeatExecutor,
renewalIntervalInSecs,
TimeUnit.SECONDS,
expBackOffBound,
// new一个续约线程,最终调用到renew()方法
new HeartbeatThread()
),
renewalIntervalInSecs, TimeUnit.SECONDS);
// InstanceInfo replicator
// 创建实例信息复制器,其实InstanceInfoReplicator继承了Runnable
// 详细操作看一下其中的run()方法,定时进行注册/刷新操作
instanceInfoReplicator = new InstanceInfoReplicator(
this,
instanceInfo,
// 默认30秒
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的run()方法发起注册/刷新
instanceInfoReplicator.onDemandUpdate();
}
};
if (clientConfig.shouldOnDemandUpdateStatusChange()) {
applicationInfoManager.registerStatusChangeListener(statusChangeListener);
}
instanceInfoReplicator.start(clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());
} else {
logger.info("Not registering with Eureka server per configuration");
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/yuu1009/article/details/80752936
内容来源于网络,如有侵权,请联系作者删除!