Spring的Eureka续约(心跳检测)详解

 更新时间:2023年11月21日 09:37:51   作者:dalianpai  
这篇文章主要介绍了Spring的Eureka续约(心跳检测)详解,eureka client每隔一定的时间,会给eureka server发送心跳,保持心跳,让eureka server知道自己还活着,需要的朋友可以参考下

Eureka 续约 (心跳检测)

心跳,eureka client每隔一定的时间,会给eureka server发送心跳,保持心跳,让eureka server知道自己还活着,lease renewal,续约,心跳.

Eureka-Client 向 Eureka-Server 发起注册应用实例成功后获得租约 ( Lease )。 Eureka-Client 固定间隔向 Eureka-Server 发起续租( renew ),避免租约过期。

默认情况下,租约有效期为 90 秒,续租频率为 30 秒。两者比例为 1 : 3 ,保证在网络异常等情况下,有三次重试的机会。

(1)DiscoveryClient初始化的时候,会去调度一堆定时任务,其中有一个就是HeartbeatThread,心跳线程

image-20211011172001382

(2)在这里可以看到,默认是每隔30秒去发送一次心跳,每隔30秒执行一次HeartbeatTHread线程的逻辑,发送心跳

(3)这边的话就是去发送这个心跳,走的是EurekaHttpClient的sendHeartbeat()方法,//localhost:8080/v2/apps/ServiceA/i-000000-1,走的是put请求

/**
     * The heartbeat task that renews the lease in the given intervals.
     */
    private class HeartbeatThread implements Runnable {
 
        public void run() {
            if (renew()) {
                lastSuccessfulHeartbeatTimestamp = System.currentTimeMillis();
            }
        }
    }
 
    /**
     * Renew with the eureka service by making the appropriate REST call
     */
    boolean renew() {
        EurekaHttpResponse<InstanceInfo> httpResponse;
        try {
            httpResponse = eurekaTransport.registrationClient.sendHeartBeat(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo, null);
            logger.debug(PREFIX + "{} - Heartbeat status: {}", appPathIdentifier, httpResponse.getStatusCode());
            if (httpResponse.getStatusCode() == Status.NOT_FOUND.getStatusCode()) {
                REREGISTER_COUNTER.increment();
                logger.info(PREFIX + "{} - Re-registering apps/{}", appPathIdentifier, instanceInfo.getAppName());
                long timestamp = instanceInfo.setIsDirtyWithTime();
                boolean success = register();
                if (success) {
                    instanceInfo.unsetIsDirty(timestamp);
                }
                return success;
            }
            return httpResponse.getStatusCode() == Status.OK.getStatusCode();
        } catch (Throwable e) {
            logger.error(PREFIX + "{} - was unable to send heartbeat!", appPathIdentifier, e);
            return false;
        }
    }

(4)负责承接服务实例的心跳相关的这些操作的,是ApplicationsResource,服务相关的controller。找到ApplicationResource,再次找到InstanceResource,通过PUT请求,可以找到renewLease方法。

@PUT
    public Response renewLease(
            @HeaderParam(PeerEurekaNode.HEADER_REPLICATION) String isReplication,
            @QueryParam("overriddenstatus") String overriddenStatus,
            @QueryParam("status") String status,
            @QueryParam("lastDirtyTimestamp") String lastDirtyTimestamp) {
        boolean isFromReplicaNode = "true".equals(isReplication);
        boolean isSuccess = registry.renew(app.getName(), id, isFromReplicaNode);
 
        // Not found in the registry, immediately ask for a register
        if (!isSuccess) {
            logger.warn("Not Found (Renew): {} - {}", app.getName(), id);
            return Response.status(Status.NOT_FOUND).build();
        }
        // Check if we need to sync based on dirty time stamp, the client
        // instance might have changed some value
        Response response;
        if (lastDirtyTimestamp != null && serverConfig.shouldSyncWhenTimestampDiffers()) {
            response = this.validateDirtyTimestamp(Long.valueOf(lastDirtyTimestamp), isFromReplicaNode);
            // Store the overridden status since the validation found out the node that replicates wins
            if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode()
                    && (overriddenStatus != null)
                    && !(InstanceStatus.UNKNOWN.name().equals(overriddenStatus))
                    && isFromReplicaNode) {
                registry.storeOverriddenStatusIfRequired(app.getAppName(), id, InstanceStatus.valueOf(overriddenStatus));
            }
        } else {
            response = Response.ok().build();
        }
        logger.debug("Found (Renew): {} - {}; reply status={}", app.getName(), id, response.getStatus());
        return response;
    }

(5)通过注册表的renew()方法,进去完成服务续约,实际进入AbstractInstanceRegistry的renew()方法

/**
     * Marks the given instance of the given app name as renewed, and also marks whether it originated from
     * replication.
     *
     * @see com.netflix.eureka.lease.LeaseManager#renew(java.lang.String, java.lang.String, boolean)
     */
    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(),
                                    overriddenInstanceStatus.name(),
                                    instanceInfo.getId());
                    instanceInfo.setStatusWithoutDirty(overriddenInstanceStatus);
 
                }
            }
            // 新增 续租每分钟次数
            renewsLastMin.increment();
            // 设置 租约最后更新时间(续租)
            leaseToRenew.renew();
            return true;
        }
    }

实际的服务续约的逻辑,其实就是在Lease对象中,更新一下lastUpdateTimestamp这个时间戳,每次续约,就更新一下,duration这里加了一遍,也是eureka的bug,但是官网并不做修改。

public void renew() {
    lastUpdateTimestamp = System.currentTimeMillis() + duration;
}

到此这篇关于Spring的Eureka续约(心跳检测)详解的文章就介绍到这了,更多相关Eureka续约内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot使用GTS的示例详解

    SpringBoot使用GTS的示例详解

    这篇文章主要介绍了SpringBoot使用GTS的示例详解,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • 详谈jvm--Java中init和clinit的区别

    详谈jvm--Java中init和clinit的区别

    下面小编就为大家带来一篇详谈jvm--Java中init和clinit的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Java continue break制作简单聊天室程序

    Java continue break制作简单聊天室程序

    这篇文章主要为大家详细介绍了Java continue break制作简单聊天室程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Springboot一个注解搞定返回参数key转换功能

    Springboot一个注解搞定返回参数key转换功能

    平时在搬砖的时候,大家有没有遇到过这样场景,由于各种因素导致,一个接口返回的数据里面的key是A, 但是客户端(前端)要求返回的key不叫A叫Aa 。也就是返回的值不变,就是key换了。本文将通过一个注解搞定返回参数key转换,需要的可以了解一下
    2022-10-10
  • 使用BufferedReader读取本地文件的操作

    使用BufferedReader读取本地文件的操作

    这篇文章主要介绍了使用BufferedReader读取本地文件的操作,具有很好的参考价值,希望对大家有所帮助。
    2021-07-07
  • 详解Spring Boot 部署jar和war的区别

    详解Spring Boot 部署jar和war的区别

    本篇文章主要介绍了详解Spring Boot 部署jar和war的区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • 分布式医疗挂号系统SpringCache与Redis为数据字典添加缓存

    分布式医疗挂号系统SpringCache与Redis为数据字典添加缓存

    这篇文章主要为大家介绍了分布式医疗挂号系统SpringCache与Redis为数据字典添加缓存,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-04-04
  • 使用springcloud+oauth2携带token去请求其他服务

    使用springcloud+oauth2携带token去请求其他服务

    这篇文章主要介绍了使用springcloud+oauth2携带token去请求其他服务方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 基于序列化存取实现java对象深度克隆的方法详解

    基于序列化存取实现java对象深度克隆的方法详解

    本篇文章是对序列化存取实现java对象深度克隆的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Javadoc 具体使用详解

    Javadoc 具体使用详解

    这篇文章主要介绍了Javadoc 具体使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08

最新评论