必备知识:

1.定时任务 ScheduledExecutorService

public class demo {
public static void main(String[] args){
ScheduledExecutorService ses = Executors.newScheduledThreadPool();

//初始化时间
int initDelay = ;
//线程间隔的时间
long period1 = ;
long period5 = ;
long period10 = ;
ses.scheduleAtFixedRate(new MyScheduledExcutor("job1"),initDelay,period1, TimeUnit.SECONDS);
ses.scheduleAtFixedRate(new MyScheduledExcutor("job2"),initDelay,period5, TimeUnit.SECONDS);
ses.scheduleAtFixedRate(new MyScheduledExcutor("job3"),initDelay,period10, TimeUnit.SECONDS);
}
}
public class MyScheduledExcutor implements Runnable {
private String job; public MyScheduledExcutor(String job){
this.job = job;
} @Override
public void run() {
System.out.println("execute job name:" + job);
}
}

简单的说明,实现runnable接口在建立对象的时候启动一个新的线程,建立线程池包含五个线程。

进入正题 initScheduledTasks()


@Singleton
public class DiscoveryClient implements EurekaClient {
    @Inject
DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, AbstractDiscoveryClientOptionalArgs args, Provider<BackupRegistry> backupRegistryProvider) {
//设置定时器
this.initScheduledTasks();
}
}
private void initScheduledTasks() {
int renewalIntervalInSecs;
int expBackOffBound;
//shouldFetchRegistry默认是true 第一次启动,本地缓存为空
if (this.clientConfig.shouldFetchRegistry()) {
//默认是30秒 每30秒刷新一次 看下面的application.properties
renewalIntervalInSecs = this.clientConfig.getRegistryFetchIntervalSeconds();
expBackOffBound = this.clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
this.scheduler.schedule(new TimedSupervisorTask("cacheRefresh", this.scheduler, this.cacheRefreshExecutor, renewalIntervalInSecs, TimeUnit.SECONDS, expBackOffBound, new DiscoveryClient.CacheRefreshThread()), (long)renewalIntervalInSecs, TimeUnit.SECONDS);
} if (this.clientConfig.shouldRegisterWithEureka()) {
renewalIntervalInSecs = this.instanceInfo.getLeaseInfo().getRenewalIntervalInSecs();
expBackOffBound = this.clientConfig.getHeartbeatExecutorExponentialBackOffBound();
logger.info("Starting heartbeat executor: renew interval is: {}", renewalIntervalInSecs);
this.scheduler.schedule(new TimedSupervisorTask("heartbeat", this.scheduler, this.heartbeatExecutor, renewalIntervalInSecs, TimeUnit.SECONDS, expBackOffBound, new DiscoveryClient.HeartbeatThread()), (long)renewalIntervalInSecs, TimeUnit.SECONDS);
this.instanceInfoReplicator = new InstanceInfoReplicator(this, this.instanceInfo, this.clientConfig.getInstanceInfoReplicationIntervalSeconds(), );
this.statusChangeListener = new StatusChangeListener() {
public String getId() {
return "statusChangeListener";
} public void notify(StatusChangeEvent statusChangeEvent) {
if (InstanceStatus.DOWN != statusChangeEvent.getStatus() && InstanceStatus.DOWN != statusChangeEvent.getPreviousStatus()) {
DiscoveryClient.logger.info("Saw local status change event {}", statusChangeEvent);
} else {
DiscoveryClient.logger.warn("Saw local status change event {}", statusChangeEvent);
} DiscoveryClient.this.instanceInfoReplicator.onDemandUpdate();
}
};
if (this.clientConfig.shouldOnDemandUpdateStatusChange()) {
this.applicationInfoManager.registerStatusChangeListener(this.statusChangeListener);
} this.instanceInfoReplicator.start(this.clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());
} else {
logger.info("Not registering with Eureka server per configuration");
} }

application.properties

#配置服务中心注册地址
eureka.client.service-url.defaultZone=http://localhost:8091/eureka/
#每隔30秒发送一次,证明自己的存在
eureka.instance.lease-renewal-interval-in-seconds=

eureka client服务续约源码分析的更多相关文章

  1. 【一起学源码-微服务】Nexflix Eureka 源码九:服务续约源码分析

    前言 前情回顾 上一讲 我们讲解了服务发现的相关逻辑,所谓服务发现 其实就是注册表抓取,服务实例默认每隔30s去注册中心抓取一下注册表增量数据,然后合并本地注册表数据,最后有个hash对比的操作. 本 ...

  2. Netty 心跳服务之 IdleStateHandler 源码分析

    前言:Netty 提供的心跳介绍 Netty 作为一个网络框架,提供了诸多功能,比如我们之前说的编解码,Netty 准备很多现成的编解码器,同时,Netty 还为我们准备了网络中,非常重要的一个服务- ...

  3. SpringCloud(4)---Ribbon服务调用,源码分析

    SpringCloud(4)---Ribbon 本篇模拟订单服务调用商品服务,同时商品服务采用集群部署. 注册中心服务端口号7001,订单服务端口号9001,商品集群端口号:8001.8002.800 ...

  4. Netty之旅三:Netty服务端启动源码分析,一梭子带走!

    Netty服务端启动流程源码分析 前记 哈喽,自从上篇<Netty之旅二:口口相传的高性能Netty到底是什么?>后,迟迟两周才开启今天的Netty源码系列.源码分析的第一篇文章,下一篇我 ...

  5. Netty源码分析 (三)----- 服务端启动源码分析

    本文接着前两篇文章来讲,主要讲服务端类剩下的部分,我们还是来先看看服务端的代码 /** * Created by chenhao on 2019/9/4. */ public final class ...

  6. Netty源码分析 (十二)----- 心跳服务之 IdleStateHandler 源码分析

    什么是心跳机制? 心跳说的是在客户端和服务端在互相建立ESTABLISH状态的时候,如何通过发送一个最简单的包来保持连接的存活,还有监控另一边服务的可用性等. 心跳包的作用 保活Q:为什么说心跳机制能 ...

  7. Eureka服务下线(Cancel)源码分析

    Cancel(服务下线) 在Service Provider服务shut down的时候,需要及时通知Eureka Server把自己剔除,从而避免其它客户端调用已经下线的服务,导致服务不可用. co ...

  8. ActivityManagerService服务线程启动源码分析【转】

    本文转载自:http://blog.csdn.net/yangwen123/article/details/8177702 Android系统服务线程都驻留在SystemServer进程中,由Syst ...

  9. 【springcloud】1.微服务之springcloud-》eureka源码分析之请叫我灵魂画师。。。

随机推荐

  1. 深入理解JVM——对象

    对象的创建 虚拟机遇到一条new指令时,首先检查指令的参数能否在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已经被加载.解析和初始化过.如果没有,必须先执行相应的类加载过程. 接下 ...

  2. LVOOP设计模式在路上(二)-- 策略模式

    前言 最近工作还挺忙的,连着好些周都是单休了,今天休息在家就来写写关于策略模式的理解和labivew的实现. 正文 1.什么是策略模式 定义是这样描述的:它定义了算法家族,分别封装起来,让它们之间可以 ...

  3. canvas-2rect.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. SQL Anywhere5.5: Metadata

    http://dcx.sybase.com/1101/en/dbprogramming_en11/ianywhere-data-sqlanywhere-saconnection-getschem633 ...

  5. 项目启动时发生NOT found

    一直想记录一下这个小问题 情景: 我昨晚美滋滋的做完功能,测了测没bug提交到git上之后就屁颠屁颠的回家了,结果今天早上来就失了智,git pull拉了一下代码后,一运行,我去,我的页面呢,页面上直 ...

  6. SoapUI 访问代理设置

    SoapUI 访问代理设置 by:授客 QQ:1033553122 问题描述: 运行SoapUI时,发现接口访问不通,如下图,提示"Connection to http://127.0.0. ...

  7. [Android] ubuntu 下不识别 Android 设备

    之前的android手机给家人用了,手里现在有一个旧手机,调试过程又出现不识别的问题,这次要记录一下. 首先,需要把手机开发者选项打开,在设置里对着android版本或者型号多点几次,就会打开. 原文 ...

  8. eclipse下载教程

    Eclipse 是一个开放源代码的.基于 Java 的可扩展开发平台. Eclipse 是 Java 的集成开发环境(IDE),当然 Eclipse 也可以作为其他开发语言的集成开发环境,如C,C++ ...

  9. [20170824]11G备库启用DRCP连接.txt

    [20170824]11G备库启用DRCP连接.txt --//参考链接:http://blog.itpub.net/267265/viewspace-2099397/blogs.oracle.com ...

  10. 统计分页一些sql

    USE [QPTreasureDB] GO /****** Object: StoredProcedure [dbo].[GameStatistics] Script Date: 2018/8/16 ...