首页 > 其他分享 >微服务笔记之Eureka03

微服务笔记之Eureka03

时间:2022-11-13 14:22:33浏览次数:44  
标签:info 服务 笔记 isReplication getId Eureka03 null registrant lease

服务注册接口源码分析:
com.netflix.eureka.resources.ApplicationResource#addInstance

public Response addInstance(InstanceInfo info,
                                @HeaderParam(PeerEurekaNode.HEADER_REPLICATION) String isReplication) {
	//注册
        registry.register(info, "true".equals(isReplication));
        return Response.status(204).build();  // 204 to be backwards compatible
}

com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl#register

public void register(final InstanceInfo info, final boolean isReplication) {
	// 心跳检测周期默认90s
        int leaseDuration = Lease.DEFAULT_DURATION_IN_SECS;
    	// 配置文件设置该时间,就用配置文件的设置时间     
	if (info.getLeaseInfo() != null && info.getLeaseInfo().getDurationInSecs() > 0) {
            leaseDuration = info.getLeaseInfo().getDurationInSecs();
        }
	// 调用父方法
        super.register(info, leaseDuration, isReplication);
    	// 复制到集群其他节点
	replicateToPeers(Action.Register, info.getAppName(), info.getId(), info, null, isReplication);
}

com.netflix.eureka.registry.AbstractInstanceRegistry#register

public void register(InstanceInfo registrant, int leaseDuration, boolean isReplication) {
        try {
            read.lock();
	    // 从注册表中获取实例信息
            Map<String, Lease<InstanceInfo>> gMap = registry.get(registrant.getAppName());
            REGISTER.increment(isReplication);
	    // 第一次注册时,gMap是null
            if (gMap == null) {
		// 定义一个Map
                final ConcurrentHashMap<String, Lease<InstanceInfo>> gNewMap = new ConcurrentHashMap<String, Lease<InstanceInfo>>();
		// 进行put操作
                gMap = registry.putIfAbsent(registrant.getAppName(), gNewMap);
                if (gMap == null) {
                    gMap = gNewMap;
                }
            }
	    // 根据实例ID获取注册信息,第一次注册获取为空
            Lease<InstanceInfo> existingLease = gMap.get(registrant.getId());
            // 不为空,说明已注册但实例信息发生变化
            if (existingLease != null && (existingLease.getHolder() != null)) {
                Long existingLastDirtyTimestamp = existingLease.getHolder().getLastDirtyTimestamp();
                Long registrationLastDirtyTimestamp = registrant.getLastDirtyTimestamp();
                logger.debug("Existing lease found (existing={}, provided={}", existingLastDirtyTimestamp, registrationLastDirtyTimestamp);

                // 
                if (existingLastDirtyTimestamp > registrationLastDirtyTimestamp) {
                    logger.warn("There is an existing lease and the existing lease's dirty timestamp {} is greater" +
                            " than the one that is being registered {}", existingLastDirtyTimestamp, registrationLastDirtyTimestamp);
                    logger.warn("Using the existing instanceInfo instead of the new instanceInfo as the registrant");
                    registrant = existingLease.getHolder();
                }
            } else {
                // 该租约不存在,因此它是一个新的注册
                synchronized (lock) {
                    if (this.expectedNumberOfClientsSendingRenews > 0) {
                        // 因为客户机想要注册它,所以要增加发送更新的客户机的数量
                        this.expectedNumberOfClientsSendingRenews = this.expectedNumberOfClientsSendingRenews + 1;
                        updateRenewsPerMinThreshold();
                    }
                }
                logger.debug("No previous lease information found; it is new registration");
            }
            Lease<InstanceInfo> lease = new Lease<InstanceInfo>(registrant, leaseDuration);
            if (existingLease != null) {
                lease.setServiceUpTimestamp(existingLease.getServiceUpTimestamp());
            }
	    // 将实例信息加入map中
            gMap.put(registrant.getId(), lease);
            synchronized (recentRegisteredQueue) {
                recentRegisteredQueue.add(new Pair<Long, String>(
                        System.currentTimeMillis(),
                        registrant.getAppName() + "(" + registrant.getId() + ")"));
            }
            // 更新状态信息
            if (!InstanceStatus.UNKNOWN.equals(registrant.getOverriddenStatus())) {
                logger.debug("Found overridden status {} for instance {}. Checking to see if needs to be add to the "
                                + "overrides", registrant.getOverriddenStatus(), registrant.getId());
                if (!overriddenInstanceStatusMap.containsKey(registrant.getId())) {
                    logger.info("Not found overridden id {} and hence adding it", registrant.getId());
                    overriddenInstanceStatusMap.put(registrant.getId(), registrant.getOverriddenStatus());
                }
            }
            InstanceStatus overriddenStatusFromMap = overriddenInstanceStatusMap.get(registrant.getId());
            if (overriddenStatusFromMap != null) {
                logger.info("Storing overridden status {} from map", overriddenStatusFromMap);
                registrant.setOverriddenStatus(overriddenStatusFromMap);
            }

            
            InstanceStatus overriddenInstanceStatus = getOverriddenInstanceStatus(registrant, existingLease, isReplication);
            registrant.setStatusWithoutDirty(overriddenInstanceStatus);

            
            if (InstanceStatus.UP.equals(registrant.getStatus())) {
                lease.serviceUp();
            }
            registrant.setActionType(ActionType.ADDED);
            recentlyChangedQueue.add(new RecentlyChangedItem(lease));
            registrant.setLastUpdatedTimestamp();
            invalidateCache(registrant.getAppName(), registrant.getVIPAddress(), registrant.getSecureVipAddress());
            logger.info("Registered instance {}/{} with status {} (replication={})",
                    registrant.getAppName(), registrant.getId(), registrant.getStatus(), isReplication);
        } finally {
            read.unlock();
        }
    }

标签:info,服务,笔记,isReplication,getId,Eureka03,null,registrant,lease
From: https://www.cnblogs.com/du001011/p/16885909.html

相关文章

  • systemctl命令列出所有服务
    systemctl命令列出所有服务systemctl是Systemd的主命令,可用于管理系统。列出所有已经加载的systemdunitssystemctlsystemctl|grepdocker.service12列出所有service......
  • 服务注册模板
    主程序exe或者jar编写配置文件  注册为系统服务cmd文件运行程序目录(最好将运行程序名改为端口号)WinSW.NET4.exeinstall    ......
  • 深入浅出学习透析Nginx服务器的基本原理和配置指南「进阶实践篇」
    proxy_pass的疑问?很多小伙伴们跟我沟通说之前章节的介绍的proxy_pass介绍的并不是很详细和清晰,那么我们就针对于Nginxproxy_pass使用在进行复习回顾一下。proxy_pass的使......
  • Goravel 让微服务间通讯如此简单,省出来的时间想干点啥干点啥吧
    Goravel升级v1.4,使微服务间请求更加简单一体化。Nobb,showthecode.Goravel是一个功能完备、具有良好扩展能力的GolangWeb应用程序框架。风格与Laravel保持一......
  • CDGA数据治理工程师认证教程视频笔记真题答案下载 CDGA培训视频考试题库备考2023必过
     一、前言 1、关于DAMA国际数据管理协会(DAMA国际)是一个全球性的专业组织(类似于PMP证书的PMI项目管理协会),由数据管理和相关的专业人士组成,协会自1980年成立以来,一直致力......
  • 日ip过万用什么服务器?
    日ip过万用什么服务器?没真正用过的别瞎哔哔,错误博客日均万ip的实例告诉大家一万ip服务器配置到底应该怎么选。https://sh.tiancebbs.cn/mayi-info.xml https://sh.tianceb......
  • Gerrit 不使用代理服务的搭建配置
    1、前言Gerrit名声已很大了,我也就不在过多阐述介绍了,凡是需要安装搭建Gerrit服务器的,网上一搜索都是大把精彩教程。但是,基本都是基于代理服务的搭建安装,一种是基于apach......
  • 多线程编程学习笔记文章目录
     多线程编程学习笔记-基础(一)多线程编程学习笔记-基础(二)多线程编程学习笔记-基础(三) 多线程编程学习笔记——线程同步(一)多线程编程学习笔记——线程同步(二) 多线......
  • MySQL基础笔记
    目录创建和查看数据库2修改和查看数据库字符集2创建表createtable+表名5删除表droptable+表名6展示创建表格showcreatetablecustomer;8复制表格create......
  • 白嫖永久服务器1668309535005
    阿贝云服务器注册免费领取1核1g内存5m宽带10g内存的云服务器,对于个人来说完全够用了。还有免费备案和虚拟主机,免备案对于搭建个人博客就很方便,部署了小项目上去,运行流畅不......