Make writing a habit together! This is the fifth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details

Eureka Client service registration process

Determination is the beginning of success

Eureka Server startup process analysis diagram + source code explain Eureka Client startup process analysis diagram + source code explain Eureka Server registry cache logic diagram + source code explain Eureka Client Pull registry process

Registration Core Flow chart

Where to start

At the time of the client to initialize clientConfig. ShouldEnforceRegistrationAtInit () registration whether open mandatory initialization, this value is the default is false so will not be registered, It is registered in the renew() method in the 30s send heartbeat mechanism task

if (clientConfig.shouldRegisterWithEureka() && 
    / / clientConfig shouldEnforceRegistrationAtInit () default is false
    clientConfig.shouldEnforceRegistrationAtInit()) {
    if(! register()) {throw new IllegalStateException("Registration error at startup. Invalid server response."); }}Copy the code

The core logic

Through renew () sends a heartbeat when returned to find instance information, so again to register operation, through eurekaTransport. RegistrationClient client requests for send the heart operation

1. Initiate registration by performing a scheduled heartbeat task

boolean renew(a) {
EurekaHttpResponse<InstanceInfo> httpResponse;
    httpResponse = eurekaTransport.registrationClient.
        sendHeartBeat(instanceInfo.getAppName(), 
                      instanceInfo.getId(), instanceInfo, null);
    if (httpResponse.getStatusCode() == Status.NOT_FOUND.getStatusCode()) {
        REREGISTER_COUNTER.increment();
        long timestamp = instanceInfo.setIsDirtyWithTime();
        boolean success = register(); // Register an instance
        if (success) {
            instanceInfo.unsetIsDirty(timestamp);
        }
        return success;
    }
    return httpResponse.getStatusCode() == Status.OK.getStatusCode();
}
Copy the code

Registered by the register () method for instance, by eurekaTransport. RegistrationClient register method access, access is AbstractJersey2EurekaHttpClient register method

boolean register(a) throws Throwable { EurekaHttpResponse<Void> httpResponse; httpResponse = eurekaTransport.registrationClient.register(instanceInfo); . }Copy the code

The AbstractInstanceRegistry register method under the Eureka-Core project is accessed through the Jersey2 framework

public void register(InstanceInfo registrant, int leaseDuration,
                     boolean isReplication) {
    /** * Get a service instance from the local gMap by the service name */
    Map<String, Lease<InstanceInfo>> gMap = registry.get(registrant.getAppName());
    if (gMap == null) {
        // Create a gNewMap if there is no instance currently registered in the local gMap
        // a service instance in Registry
        final ConcurrentHashMap<String, Lease<InstanceInfo>> gNewMap = 
            new ConcurrentHashMap<String, Lease<InstanceInfo>>();
        // Set the current service name as key and the service name, value, is the current gNewMap to be registered
        gMap = registry.putIfAbsent(registrant.getAppName(), gNewMap);
        if (gMap == null) { gMap = gNewMap; }}// Create an instance of gMap
    gMap.put(registrant.getId(), lease);
}
Copy the code

2. Put the current instance into the queue

The registration queue and change the queue is created when the service is initialized, through the recent change of queue can be monitored the recent change, new registered queue and referrals queue is mainly to display in the page, in recent change of queue in order to obtain incremental registered instance information when used

/** * Puts the current instance into the recently registered queue */
recentRegisteredQueue.add(new Pair<Long, String>(System.currentTimeMillis(),
             registrant.getAppName() + "(" + registrant.getId() + ")"));
// Set the type to Add type
registrant.setActionType(ActionType.ADDED);
/** * Add is also a change, so create a RecentlyChangedItem and place it in the latest change queue. * The timestamp is the current system time */
recentlyChangedQueue.add(new RecentlyChangedItem(lease));
Copy the code

3. Invalid local cache

/** * The current read/write cache is invalid when a new instance comes */
invalidateCache(registrant.getAppName(), registrant.getVIPAddress(),
                registrant.getSecureVipAddress());
Copy the code

The invalidate method of ResponseCacheImpl removes some of the keys from the cache and removes them from the read/write cache

public void invalidate(Key... keys) {
    for (Key key : keys) {
        // Remove the key from the read/write cache
        readWriteCacheMap.invalidate(key);
        Collection<Key> keysWithRegions = regionSpecificKeys.get(key);
        if (null! = keysWithRegions && ! keysWithRegions.isEmpty()) {for(Key keysWithRegion : keysWithRegions) { readWriteCacheMap.invalidate(keysWithRegion); }}}}Copy the code

summary

  1. Initiate registration through heartbeat
  2. Puts the current instance into the registration queue and the recently changed queue
  3. Invalid server local cache