必备知识:

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. 【Java基础】11、java方法中只有值传递,没有引用传递

    public class Example { String testString = new String("good"); char[] testCharArray = {'a' ...

  2. jsp-servlet 的相关请求路径问题 —url

    jsp-servlet 的相关请求路径问题  —url 本文章主要解决的几方面问题如下: 常见涉及路径元素: jsp页面请求和servlet请求转发.重定向的关系 如何避免下一步请求受上一步请求在UR ...

  3. webpack4 系列教程(七): SCSS提取和懒加载

    教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步>>> (原文)webpack4 系列教程(七): SCSS 提取和懒加载. 个人技术小站: https://god ...

  4. JavaScript一团乱,这是好事

    译者按: JavaScript从简单变复杂了,作者从另一个角度看待这个问题. 原文: JavaScript’s a mess – and that’s a good thing 译者: Fundebu ...

  5. The server quit without updating PID file (data mysql.pid)

     (1)mysql的安装路径和运行路径 # whereis mysqld   (2)PATH变量指定的路径中,搜索mysql的信息    #  which mysqld  (3)查看配置文件   # ...

  6. Spring Boot Oauth2缓存UserDetails到Ehcache

    在Spring中有一个类CachingUserDetailsService实现了UserDetailsService接口,该类使用静态代理模式为UserDetailsService提供缓存功能.该类源 ...

  7. 【读书笔记】iOS-微信公众平台开发最佳实践

    一,微信是由腾讯公司广州研发中心产品团队开发,该团队经理张小龙被称为“微信之父”,公司总裁马化腾确定该产品名称为“微信”. 二,常见问题及解决方案. 1,请求URL超时. 这种情况一般是由于服务器网速 ...

  8. Button's four click events

    第一种:内部类的方式 1 package com.example.phonedialer; 2 3 import com.example.click2.R; 4 5 import android.ne ...

  9. 【第一篇】SAP ABAP7.5x新语法之预定义数据结构

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:SAP ABAP7.5x系列之预定义数据结构 前 ...

  10. Vim命令图解及快捷键讲解

    快捷键详解