Spring-cloud学习笔记— Eureka源码剖析之服务续约接⼝(接受客户端续约)
1. 因为续约是针对实例的,所以续约在InstanceResource
的里面
InstanceResource
的renewLease
方法表示更新续期
的方法- 其中这个方法中的
boolean isSuccess = registry.renew(app.getName(), id, isFromReplicaNode);
的renew
是核心方法
- 点击
InstanceResource
的renewLease
方法后,点击renew
进入com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl
的renew
方法
- 点击
renew
方法中的renew
方法之后,会有一个leaseToRenew.renew();
调用,表示对最后更新的时间戳进行更新(倒数第二行代码)
public boolean renew(String appName, String id, boolean isReplication) { RENEW.increment(isReplication); Map<String, Lease<InstanceInfo>> gMap = registry.get(appName); Lease<InstanceInfo> leaseToRenew = null; if (gMap != null) { leaseToRenew = gMap.get(id); } if (leaseToRenew == null) { RENEW_NOT_FOUND.increment(isReplication); logger.warn("DS: Registry: lease doesn't exist, registering resource: {} - {}", appName, id); return false; } else { InstanceInfo instanceInfo = leaseToRenew.getHolder(); if (instanceInfo != null) { // touchASGCache(instanceInfo.getASGName()); InstanceStatus overriddenInstanceStatus = this.getOverriddenInstanceStatus( instanceInfo, leaseToRenew, isReplication); if (overriddenInstanceStatus == InstanceStatus.UNKNOWN) { logger.info("Instance status UNKNOWN possibly due to deleted override for instance {}" + "; re-register required", instanceInfo.getId()); RENEW_NOT_FOUND.increment(isReplication); return false; } if (!instanceInfo.getStatus().equals(overriddenInstanceStatus)) { logger.info( "The instance status {} is different from overridden instance status {} for instance {}. " + "Hence setting the status to overridden status", instanceInfo.getStatus().name(), instanceInfo.getOverriddenStatus().name(), instanceInfo.getId()); instanceInfo.setStatusWithoutDirty(overriddenInstanceStatus); } } renewsLastMin.increment(); //表示对最后更新的时间戳进行更新 leaseToRenew.renew(); return true; } }
- 点击
renew
方法中的replicateInstanceActionsToPeers()
方法,复制Instance实例操作到其它节点private void replicateToPeers(Action action, String appName, String id, InstanceInfo info /* optional */, InstanceStatus newStatus /* optional */, boolean isReplication) { Stopwatch tracer = action.getTimer().start(); try { if (isReplication) { numberOfReplicationsLastMin.increment(); } // If it is a replication already, do not replicate again as this will create a poison replication if (peerEurekaNodes == Collections.EMPTY_LIST || isReplication) { return; } for (final PeerEurekaNode node : peerEurekaNodes.getPeerEurekaNodes()) { // If the url represents this host, do not replicate to yourself. if (peerEurekaNodes.isThisMyUrl(node.getServiceUrl())) { continue; } replicateInstanceActionsToPeers(action, appName, id, info, newStatus, node); } } finally { tracer.stop(); } }
- 再点击上面代码的
leaseToRenew.renew();
的renew
方法public void renew() { lastUpdateTimestamp = System.currentTimeMillis() + duration; }