/** * Registers a new instance with a given duration. * * @see com.netflix.eureka.lease.LeaseManager#register(java.lang.Object, int, boolean) */ publicvoidregister(InstanceInfo registrant, int leaseDuration, boolean isReplication) { try { read.lock(); //读锁 // registry是保存所有应⽤实例信息的Map: ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>> // 从registry中获取当前appName的所有实例信息 Map<String, Lease<InstanceInfo>> gMap = registry.get(registrant.getAppName()); REGISTER.increment(isReplication); //注册统计+1 // 如果当前appName实例信息为空,新建Map if (gMap == null) { final ConcurrentHashMap<String, Lease<InstanceInfo>> gNewMap = newConcurrentHashMap<String, Lease<InstanceInfo>>(); gMap = registry.putIfAbsent(registrant.getAppName(), gNewMap); if (gMap == null) { gMap = gNewMap; } } // 获取实例的Lease租约信息 Lease<InstanceInfo> existingLease = gMap.get(registrant.getId()); // Retain the last dirty timestamp without overwriting it, if there is already a lease // 如果已经有租约,则保留最后⼀个脏时间戳⽽不覆盖它 // (⽐较当前请求实例租约 和 已有租约 的LastDirtyTimestamp,选择靠 后的) if (existingLease != null && (existingLease.getHolder() != null)) { LongexistingLastDirtyTimestamp= existingLease.getHolder().getLastDirtyTimestamp(); LongregistrationLastDirtyTimestamp= 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 { // The lease does not exist and hence it is a new registration // 如果之前不存在实例的租约,说明是新实例注册 // expectedNumberOfRenewsPerMin期待的每分钟续约数+2(因为 30s⼀个) // 并更新numberOfRenewsPerMinThreshold每分钟续约阀值(85%) synchronized (lock) { if (this.expectedNumberOfRenewsPerMin > 0) { // Since the client wants to cancel it, reduce the threshold // (1 // for 30 seconds, 2 for a minute) this.expectedNumberOfRenewsPerMin = this.expectedNumberOfRenewsPerMin + 2; this.numberOfRenewsPerMinThreshold = (int) (this.expectedNumberOfRenewsPerMin * serverConfig.getRenewalPercentThreshold()); } } logger.debug("No previous lease information found; it is new registration"); } Lease<InstanceInfo> lease = newLease<InstanceInfo> (registrant, leaseDuration); if (existingLease != null) { lease.setServiceUpTimestamp(existingLease.getServiceUpTimestamp()) ; } gMap.put(registrant.getId(), lease); //当前实例信息放到维护注册 信息的Map // 同步维护最近注册队列 synchronized (recentRegisteredQueue) { recentRegisteredQueue.add(newPair<Long, String>( System.currentTimeMillis(), registrant.getAppName() + "(" + registrant.getId() + ")")); } // This is where the initial state transfer of overridden status happens // 如果当前实例已经维护了OverriddenStatus,将其也放到此Eureka Server的overriddenInstanceStatusMap中 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()); } } InstanceStatusoverriddenStatusFromMap= overriddenInstanceStatusMap.get(registrant.getId()); if (overriddenStatusFromMap != null) { logger.info("Storing overridden status {} from map", overriddenStatusFromMap); registrant.setOverriddenStatus(overriddenStatusFromMap); } // Set the status based on the overridden status rules // 根据overridden status规则,设置状态 InstanceStatusoverriddenInstanceStatus = getOverriddenInstanceStatus(registrant, existingLease, isReplication); registrant.setStatusWithoutDirty(overriddenInstanceStatus); // If the lease is registered with UP status, set lease service up timestamp // 如果租约以UP状态注册,设置租赁服务时间戳 if (InstanceStatus.UP.equals(registrant.getStatus())) { lease.serviceUp(); } registrant.setActionType(ActionType.ADDED); //ActionType为 ADD recentlyChangedQueue.add(newRecentlyChangedItem(lease)); //维护recentlyChangedQueue registrant.setLastUpdatedTimestamp(); //更新最后更新时间 // 使当前应⽤的ResponseCache失效 invalidateCache(registrant.getAppName(), registrant.getVIPAddress(), registrant.getSecureVipAddress()); logger.info("Registered instance {}/{} with status {} (replication={})", registrant.getAppName(), registrant.getId(), registrant.getStatus(), isReplication); } finally { read.unlock(); //读锁 } }