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
  • 相关阅读:
    【转载】Perl异常处理方法总结
    (个人)读取A.CSV修改它的某列,写入B.CSV
    (转载)PERL 处理CSV
    (转载)CSV 文件处理 PERL
    string.split()
    反转字符串应该注意的问题
    jQuery取值相加
    ADO.NET的五个主要对象
    Response.Redirect和Server.Transfer
    string和stringBuilder的区别
  • 原文地址:https://www.cnblogs.com/mutong1228/p/10156682.html
Copyright © 2011-2022 走看看