Eureka source code service acquisition and service renewal

tags: Eureka

First look at the initScheduledTasks function of DiscoveryClient, you can see that there are two timed tasks, namely "service acquisition" and "service renewal".

private void initScheduledTasks() {

        if (clientConfig.shouldFetchRegistry()) {

            // registry cache refresh timer

            int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();

            int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();

            scheduler.schedule(

                    new TimedSupervisorTask(

                            "cacheRefresh",

                            scheduler,

                            cacheRefreshExecutor,

                            registryFetchIntervalSeconds,

                            TimeUnit.SECONDS,

                            expBackOffBound,

                            new CacheRefreshThread()

                    ),

                    registryFetchIntervalSeconds, TimeUnit.SECONDS);

        }



        if (clientConfig.shouldRegisterWithEureka()) {

            int renewalIntervalInSecs = instanceInfo.getLeaseInfo().getRenewalIntervalInSecs();

            int expBackOffBound = clientConfig.getHeartbeatExecutorExponentialBackOffBound();

            logger.info("Starting heartbeat executor: " + "renew interval is: {}", renewalIntervalInSecs);



            // Heartbeat timer

            scheduler.schedule(

                    new TimedSupervisorTask(

                            "heartbeat",

                            scheduler,

                            heartbeatExecutor,

                            renewalIntervalInSecs,

                            TimeUnit.SECONDS,

                            expBackOffBound,

                            new HeartbeatThread()

                    ),

                    renewalIntervalInSecs, TimeUnit.SECONDS);



            ...

    }

From the source code, it can be seen that the "service acquisition" task is more independent than the "service renewal" and "service registration" tasks. "Service renewal" and "service registration" are in the same if logic. This is not difficult to understand. After the service is registered with Eureka Server, it naturally needs a heartbeat to renew the contract to prevent it from being deleted, so they appear together. From the source code, we see more clearly the time control parameters related to service renewal mentioned earlier:

    public static final int DEFAULT_LEASE_RENEWAL_INTERVAL = 30;

    public static final int DEFAULT_LEASE_DURATION = 90;

The logic of "service acquisition" is in an independent if judgment, which is based on the eureka.client.fetch-registery = true parameter, which is true by default. In most cases, we do not need to care. In order to regularly update the client's service list to ensure that the client can access a truly healthy appendix example, the "service acquisition" request is not limited to service startup, but a scheduled task. From the source code, we can see that the task is running The registryFetchIntervalSeconds parameter corresponds to the eureka.client.reegistry-fetch-internal-seconds = 30 configuration parameter, which defaults to 30 seconds.

The implementation of "service renewal" is relatively simple. Renew the contract directly by REST request. The relevant codes are as follows:

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;

        }

    }

The "service acquisition" is more complicated, and will initiate different REST requests and corresponding processing according to whether it is the first acquisition.

 

Intelligent Recommendation

Spring Cloud Eureka Source Code Resolution Service Renewal (4)

EurekaClient EUREKA Client starts a timed task that sends a heartbeat every 30s to Eureka Server to Eureka Server If the renewable response status code is 404, you need to register again. Eureka Serve...

Eureka Server source code analysis (service renewal process)

It is not easy, please indicate the source for reprinting Articles directory Foreword 1. Source code analysis 2. Facilities Summarize Foreword The service renewal can be understood as a heartbeat. The...

Completely understand Eureka——A study on the source code of service renewal

In the previous article"Full Understanding Eureka - Exploration on the Source Code of Heartbeat Sending"We have studied the process of service sending, and we also know that heartbeat sendin...

Eureka Server source code analysis (4)-service renewal interface (accept client renewal)

Entrance: The client's heartbeat (renewal) processing is completed in the renewLease method of InstanceResource, the key code: registry.renew(app.getName(), id, isFromReplicaNode); com.netflix.eureka....

[Learn the source code together-microservices] Nexflix Eureka source code 9: service renewal source code analysis

Preface Review In the last lecture, we explained the related logic of service discovery. The so-called service discovery is actually registry crawling. By default, service instances go to the registry...

More Recommendation

Eureka service registration, service renewal source code analysis, why Eureka obtains service instances so slowly, self-protection mode

Some concepts of Eureka Register——Service Registration When Eureka Client registers with Eureka Server, Eureka Client provides its own metadata, such as IP address, port, Url of health ind...

Spring Cloud Eureka source code tracking (2. Service acquisition)

Description: Use the Scheduled Thread class (ScheduledExecutorService scheduler) to execute the DiscoveryClient.CacheRefreshThread method at intervals of renewalIntervalInSecs (default 30s). scheduler...

Spring Cloud: Eureka Source Code I see (6) Service Renewal (Heartbeat)

EUREKA Client will send heartbeat to Eureka Server every other time, keep your heartbeat, so that Eureka Server is perceived. EUREKA Client Service Renewal Process: (1) The service renewal process is ...

SpringCloud-Eureka-Service Registration & Service Renewal (2)

Series article catalog SpringCloud Practice Talking about the principle of SpringCloud Practice FEIGN SpringCloud-Eureka (1) SpringCloud-Eureka-Service Registration & Service Renewal (2) SpringClo...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top