eureka client服务续约源码分析
必备知识:
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服务续约源码分析的更多相关文章
- 【一起学源码-微服务】Nexflix Eureka 源码九:服务续约源码分析
前言 前情回顾 上一讲 我们讲解了服务发现的相关逻辑,所谓服务发现 其实就是注册表抓取,服务实例默认每隔30s去注册中心抓取一下注册表增量数据,然后合并本地注册表数据,最后有个hash对比的操作. 本 ...
- Netty 心跳服务之 IdleStateHandler 源码分析
前言:Netty 提供的心跳介绍 Netty 作为一个网络框架,提供了诸多功能,比如我们之前说的编解码,Netty 准备很多现成的编解码器,同时,Netty 还为我们准备了网络中,非常重要的一个服务- ...
- SpringCloud(4)---Ribbon服务调用,源码分析
SpringCloud(4)---Ribbon 本篇模拟订单服务调用商品服务,同时商品服务采用集群部署. 注册中心服务端口号7001,订单服务端口号9001,商品集群端口号:8001.8002.800 ...
- Netty之旅三:Netty服务端启动源码分析,一梭子带走!
Netty服务端启动流程源码分析 前记 哈喽,自从上篇<Netty之旅二:口口相传的高性能Netty到底是什么?>后,迟迟两周才开启今天的Netty源码系列.源码分析的第一篇文章,下一篇我 ...
- Netty源码分析 (三)----- 服务端启动源码分析
本文接着前两篇文章来讲,主要讲服务端类剩下的部分,我们还是来先看看服务端的代码 /** * Created by chenhao on 2019/9/4. */ public final class ...
- Netty源码分析 (十二)----- 心跳服务之 IdleStateHandler 源码分析
什么是心跳机制? 心跳说的是在客户端和服务端在互相建立ESTABLISH状态的时候,如何通过发送一个最简单的包来保持连接的存活,还有监控另一边服务的可用性等. 心跳包的作用 保活Q:为什么说心跳机制能 ...
- Eureka服务下线(Cancel)源码分析
Cancel(服务下线) 在Service Provider服务shut down的时候,需要及时通知Eureka Server把自己剔除,从而避免其它客户端调用已经下线的服务,导致服务不可用. co ...
- ActivityManagerService服务线程启动源码分析【转】
本文转载自:http://blog.csdn.net/yangwen123/article/details/8177702 Android系统服务线程都驻留在SystemServer进程中,由Syst ...
- 【springcloud】1.微服务之springcloud-》eureka源码分析之请叫我灵魂画师。。。
随机推荐
- C#中的out、ref、params详解
out参数: 如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组.但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候,我们可以考虑使用out参数.out参数就侧重于 ...
- php中一个字符占用几个字节?
先看看字符与字节有什么区别: (一)“字节”的定义 字节(Byte)是一种计量单位,表示数据量多少,它是计算机信息技术用于计量存储容量的一种计量单位. (二)“字符”的定义 字符是指计算机中使用的文字 ...
- Django Rest framework 之 解析器
RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...
- 关于<checkbox>checked默认问题
这个问题困扰我有一段时间了,今天终于解决了. 接手现在的项目半个月了,我看之前的人写的<checkbox checked="false" />一直没有生效,但是需求上没 ...
- npm 全局执行 update 、 outdated 出现 npm-debug.log 404 错误的问题
想要执行一次全局更新,发现屡次报错: # npm update -g 提示的错误信息包含如下内容: npm ERR! code E404 npm ERR! 404 Registry returned ...
- pycharm如何新项目如何不默认创建虚拟环境(吐槽)
最近因为工作上的需要,琢磨了一下python,装了pycharm这个号称史上最好的编辑器,还没开始玩,就被整崩溃了. 因为我是刚开始玩这个,写了很多hello world,所以新建项目的时候很多,不知 ...
- 使用过AsyncTask、EventBus、Volley以及Retrofit,必须好好了解handler运行机制
我们都知道在UI线程中不能进行耗时操作,例如数据读写.网络请求.Android 4.0开始,在主线程中进行网络请求甚至会抛出Android.os.NetworkOnMainThreadExceptio ...
- Django中ORM介绍和字段及字段参数 Object Relational Mapping(ORM)
Django中ORM介绍和字段及字段参数 Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简 ...
- github版本控制相关
Git版本控制: 安装Github http://blog.csdn.net/huangyuan_xuan/article/details/49125597 Git本地版本控制 http://blog ...
- C++ 获取当前正在执行的函数的相关信息
(我的运行环境:win10x64+vs2015通过, 有的环境KUbuntu 8.04.1 x64 g++ 4.2.3也通过了)主要通过宏来实现:(注意,开头和结尾都是两个下划线) 1. __PRET ...