zoukankan      html  css  js  c++  java
  • eureka client服务续约源码分析

    必备知识:

    1.定时任务 ScheduledExecutorService

    public class demo {
        public static void main(String[] args){
            ScheduledExecutorService ses = Executors.newScheduledThreadPool(5);
    
    //初始化时间
    int initDelay = 10;
    //线程间隔的时间
    long period1 = 1; long period5 = 5; long period10 = 10; 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(), 2); 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=30
  • 相关阅读:
    翻转单词顺序列
    和为S的两个数字
    单例模式
    python利用pyinstaller打包常用打包命令
    python 3.8 使用pymssql 向SQL Server插入数据不成功原因
    PyQt5(designer)入门教程
    PyQt5中文教程
    scrapy 图片爬取 多层多页 保存不同的文件夹 重命名full文件夹
    安装Python + PyCharm + PyQt5配套设置
    python用pymysql模块操作数据库MySQL,实现查增删改
  • 原文地址:https://www.cnblogs.com/mutong1228/p/10156682.html
Copyright © 2011-2022 走看看