Simple carding under eureka Registry Code

Herein advantages: the step of pressing operation may string together

Registration center in two parts, eureka-server and eureka-client

I. Overview:

1.eureka-server: a major maintenance service address information eureka-client, the use of double map, then in fact, provides an interface additions and deletions to change search on this map, such as: registration (add), access to services (inquiry), renewal (updated expiration time), excluding (deletion), synchronous (full amount of the initial acquisition, new registrations will eliminate synchronization), and added two levels of cache, synchronization, and removed from the two tasks

2.eureka-client: The main service is to register and obtain the task (the full amount of the increment), renewed mission

Two carding codes .eureka-server

1. Locate the pom-dependent eureka-server

<dependencies>
    <!--Eureka 服务注册中心 start-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!--Eureka 服务注册中心 end-->
</dependencies>

2. Click on spring-cloud-starter-netflix-eureka-server, find the following dependence

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>

3. Search spring-cloud-netflix-eureka-server, and point into

4. Locate spring.factories under the package, find EurekaServerAutoConfiguration under EnableAutoConfiguration, and click on the go, this class is eureka-server automatic load a class below analyze how this class is loaded

PS: you need to understand the point of spring boot automatically loads knowledge

5. Locate the spring boot startup class, click @SpringBootApplication comment

6. Locate @EnableAutoConfiguration comment, and click

7. Locate follows @Import ({AutoConfigurationImportSelector.class}), click on the class

8.AutoConfigurationImportSelector ImportSelector class implements an interface, this interface is to manually inject bean interface

PS: It should be understood that spring pre-knowledge

FIG 1 is a lower class AutoConfigurationImportSelector FIG.

 

Click into the red box method, scanning the spring.factories here in META-INF, where they scanned the contents of step 4

selectImport method is called in the invokeBeanFactoryPostProcessors refresh, as follows:

9.进入EurekaServerAutoConfiguration类,@ConditonalOnBean就是后边的Marker类存在则加载本类,进入程序启动类,点击@EnableEurekaServer注解,这里加载了Marker类,所以这个注解是开启eureka-server.

进入程序启动类,点击@EnableEurekaServer

点击EurekaServerMarkerConfiguration

如下类注入了Marker,则激活了EurekaServerAutoConfiguration类(eureka-server自动装配类)

10.进入EurekaServerAutoConfiguration类

找到eurekaServerBootstrap方法,然后搜索PeerAwareInstanceRegistry注入的地方

11.找到PeerAwareInstanceRegistry,点击new InstanceRegistry方法,一直点super方法,最终定位到AbstractInstanceRegistry类,这个类中提供了注册,续约,服务获取,剔除等方法,如下图

12.服务同步和剔除任务则在如下Import的红框类中,这个类实现了SmartLifecycle接口,则在bean装载并初始化完后执行start方法

下图中sysup是服务同步方法,this.registry.openForTraffic方法则是启动剔除服务

三.eureka-client代码分析

发布了42 篇原创文章 · 获赞 25 · 访问量 7万+

Guess you like

Origin blog.csdn.net/qq812858143/article/details/104584377