Eureka of Spring Cloud source code analysis (1)

Eureka of Spring Cloud source code analysis (1)

Opening remarks

When the project is split into multiple microservices, multiple services need to be managed at this time. The service provider registers the service and other consumers can call it. When a new service is registered, the client will regularly Pull down the service list to compare with the locally existing service list, and update the service list. When a service goes offline, the list will also be updated. The service provider and consumer are the main initiators of the action, and the eureka registry handles the reception of requests. For users, you can start with the eureka client as an entry point to study and see how the active initiator communicates with the registry.

Source code analysis

1. EnableDiscoveryClientImportSelector loads all metadata information of the startup class.
Eureka of Spring Cloud source code analysis (1)
Eureka of Spring Cloud source code analysis (1)
2. Loads all jar auto-configuration classes and removes duplicate classes, and then auto-assembly of the class.
Eureka of Spring Cloud source code analysis (1)
Eureka of Spring Cloud source code analysis (1)
3. DiscoveryClient is the default implementation class of EurekaClien. This class is a singleton and is implemented EurekaClient and LookupService
Eureka of Spring Cloud source code analysis (1)
4, DiscoveryClient class has a method for initializing scheduled tasks. The logic in this method is still more complicated. The first one is to get the latest service list every 30s; the second is to renew the contract every 30s. Service; The third is to register the service periodically, and then refresh the anti-kick and register the service regularly every 30s.
Eureka of Spring Cloud source code analysis (1)
5. In DiscoveryClient, the remote service is called through the client to renew the service, and the registration is the same principle.
Eureka of Spring Cloud source code analysis (1)

to sum up

1. The eureka client does the following things:
1. Get the latest list of services regularly . 2. Regularly
renew the registered service.
3. Register the service to the registration.
4. Check the heartbeat regularly. If it exceeds a certain time (90s), there is no heartbeat. Exclude services.
5. The default is to open the self-protection mode.
6. Invalid nodes will be cleared at a certain time interval.
2. The service list
can be obtained through the address configured by the project, the method service list, eureka.client.serviceUrl.defaultZone: url
3. Service registration
1. In order not to register the service to the registry, you can set: eureka.client.enabled: false, but this will not refresh the local configuration.
2. In order not to register the service to the registry, you can set: eureka.client.registerWithEureka: false to refresh the local configuration.
3. In order not to register the service to the registry, you can change the service name to another one, which is equivalent to starting another application.

Guess you like

Origin blog.51cto.com/xxdeelon/2556257