zoukankan      html  css  js  c++  java
  • spring cloud学习--eureka 02

    开启eureka client的注解@EnableDiscoveryClient的功能类DiscoveryClient梳理图

    获取server url位于类EndpointUtils的getServiceUrlsMapFromConfig方法上

    public static Map<String, List<String>> getServiceUrlsMapFromConfig(EurekaClientConfig clientConfig, String instanceZone, boolean preferSameZone) {
            Map<String, List<String>> orderedUrls = new LinkedHashMap();
            //获取regin,一个微服务应用只有一个region
            String region = getRegion(clientConfig);
            //获取zone,一个微服务可以有多个zone
            String[] availZones = clientConfig.getAvailabilityZones(clientConfig.getRegion());
            if (availZones == null || availZones.length == 0) {
                //如果zone为null,就设置为default
                availZones = new String[]{"default"};
            }
    
            logger.debug("The availability zone for the given region {} are {}", region, Arrays.toString(availZones));
            int myZoneOffset = getZoneOffset(instanceZone, preferSameZone, availZones);
            String zone = availZones[myZoneOffset];
            //根据region和zone得出server url, 并且可以配置多个server url
            List<String> serviceUrls = clientConfig.getEurekaServerServiceUrls(zone);
            if (serviceUrls != null) {
                orderedUrls.put(zone, serviceUrls);
            }
    
            int currentOffset = myZoneOffset == availZones.length - 1 ? 0 : myZoneOffset + 1;
    
            while(currentOffset != myZoneOffset) {
                zone = availZones[currentOffset];
                serviceUrls = clientConfig.getEurekaServerServiceUrls(zone);
                if (serviceUrls != null) {
                    orderedUrls.put(zone, serviceUrls);
                }
    
                if (currentOffset == availZones.length - 1) {
                    currentOffset = 0;
                } else {
                    ++currentOffset;
                }
            }
    
            if (orderedUrls.size() < 1) {
                throw new IllegalArgumentException("DiscoveryClient: invalid serviceUrl specified!");
            } else {
                return orderedUrls;
            }
        }
    

    服务注册DiscoveryClient initScheduledTasks方法

     private void initScheduledTasks() {
            int renewalIntervalInSecs;
            int expBackOffBound;
            if (this.clientConfig.shouldFetchRegistry()) {
                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");
            }
    
        }
    

    上面代码中 this.instanceInfoReplicator.start(this.clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());开启了一个任务,该任务的代码如下

    public void run() {
            boolean var6 = false;
    
            ScheduledFuture next;
            label53: {
                try {
                    var6 = true;
                    this.discoveryClient.refreshInstanceInfo();
                    Long dirtyTimestamp = this.instanceInfo.isDirtyWithTime();
                    if (dirtyTimestamp != null) {
                        //真正触发了注册
                        this.discoveryClient.register();
                        this.instanceInfo.unsetIsDirty(dirtyTimestamp);
                        var6 = false;
                    } else {
                        var6 = false;
                    }
                    break label53;
                } catch (Throwable var7) {
                    logger.warn("There was a problem with the instance info replicator", var7);
                    var6 = false;
                } finally {
                    if (var6) {
                        ScheduledFuture next = this.scheduler.schedule(this, (long)this.replicationIntervalSeconds, TimeUnit.SECONDS);
                        this.scheduledPeriodicRef.set(next);
                    }
                }
    
                next = this.scheduler.schedule(this, (long)this.replicationIntervalSeconds, TimeUnit.SECONDS);
                this.scheduledPeriodicRef.set(next);
                return;
            }
    
            next = this.scheduler.schedule(this, (long)this.replicationIntervalSeconds, TimeUnit.SECONDS);
            this.scheduledPeriodicRef.set(next);
        }
    

    真正触发了注册的代码为this.discoveryClient.register();
    注册的代码为:

    boolean register() throws Throwable {
            logger.info("DiscoveryClient_" + this.appPathIdentifier + ": registering service...");
    
            EurekaHttpResponse httpResponse;
            try {
                //通过REST请求进行注册
                httpResponse = this.eurekaTransport.registrationClient.register(this.instanceInfo);
            } catch (Exception var3) {
                logger.warn("{} - registration failed {}", new Object[]{"DiscoveryClient_" + this.appPathIdentifier, var3.getMessage(), var3});
                throw var3;
            }
    
            if (logger.isInfoEnabled()) {
                logger.info("{} - registration status: {}", "DiscoveryClient_" + this.appPathIdentifier, httpResponse.getStatusCode());
            }
    
            return httpResponse.getStatusCode() == 204;
        }
    
  • 相关阅读:
    win7开启Administrator账户
    二叉树遍历
    使用NAnt提高工作效率(二)
    系统服务的最简单实现
    右键附加启动命令行
    C#开发奇技淫巧二:根据dll文件加载C++或者Delphi插件
    百度原CTO李一男经典语录
    Sql开发技巧
    使用NAnt提高工作效率(一)
    对获取config文件的appSettings节点简单封装
  • 原文地址:https://www.cnblogs.com/nwu-edu/p/9601842.html
Copyright © 2011-2022 走看看