This is the 8th day of my participation in Gwen Challenge

One, foreword

Corresponding eureka-server detailed source code analysis: link


The Spring-Cloud-starter-Eureka-server project is a Spring Boot project that encapsulates eureka-Server. Corresponding official website: github.com/spring-clou…


Now that you’ve learnedeureka-serverWhy study at allspring-cloud-starter-eureka-server?

  1. learningspring-cloudHow to encapsulateeureka-server
  2. learningspring-cloudHow to starteureka-server


spring bootintegrationeureka-serverJust add a note@EnableEurekaServer, the code is as follows:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Copy the code


Summary in advance

Question:

  1. spring-cloudHow to encapsulateeureka-server
  2. spring-cloudHow to starteureka-server

1. spring-cloudHow to starteureka-server?

There are two major steps to start:

  1. When Spring Boot is started, it will start with an embedded Tomcat container, starting itself as a Web application

  2. Follow the @enableeurekaserver annotation to complete the initialization to implement the original Bootstrap:

    1. EurekaServerAutoConfiguration initialization

    2. EurekaServerInitializerConfiguration initialization

    3. EurekaServerBootstrap: corresponds to the eureka-server initialization

The second step above completes the initialization of all components of eureka Server and starts eukrea Server.


Study 2.spring-cloudHow to encapsulateeureka-server?

The easiest thing to think of is to just copy the code.

However, to copy the code and reduce the coupling and ensure the independence of the module, spring Boot feature automatic assembly is required.

The steps can be divided into:

  1. willeureka-serverInitialize code, encapsulated:
  2. usingspring bootThe features of the rewrite code:beanBuild and load the configuration
  3. willeureka-serverConfiguration file packaging: integrate configuration intoapplication.ymlAnd assign the corresponding default value





Second, direct hate source code

The starting point is @enableeurekaserver.

EnableEurekaServer note: Eureka-Server is started after Spring Boot starts its own Web container.

Starting with @enableeurekaserver, we mainly look at three classes, which can also be called three steps:

  1. EurekaServerAutoConfigurationInitialize the
  2. EurekaServerInitializerConfigurationInitialize the
  3. Important:EurekaServerBootstrapThe person that


In IDEA, press CTRL + click to enter @enableeurekaserver. The source code is as follows:

/**
 * Annotation to activate Eureka Server related configuration {@link EurekaServerAutoConfiguration}
 *
 * @author Dave Syer
 * @author Biju Kunjummen
 *
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {
}
Copy the code


  1. EurekaServerAutoConfigurationInitialize the

@ EnableEurekaServer triggers EurekaServerAutoConfiguration initialization, the code is as follows:

@Configuration // Identifies the configuration class
// Execute first and generate the corresponding bean
@Import(EurekaServerInitializerConfiguration.class)
This configuration can only be loaded when the Marker object is initialized and loaded into the Spring container
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class) 
// Load the configuration information from the configuration file into the beans
@EnableConfigurationProperties({ EurekaDashboardProperties.class, InstanceRegistryProperties.class })
// Load the default configuration file
@PropertySource("classpath:/eureka/server.properties")
// Inherits to register eureka-Server's Jersey interceptor information with SpringMVC
public class EurekaServerAutoConfiguration extends WebMvcConfigurerAdapter {

    // Initialize a bunch of Eureka Server components
    // Generate corresponding beans from these components and load them into the Spring container. . }Copy the code


  1. EurekaServerInitializerConfigurationInitialize the

@ Import (EurekaServerInitializerConfiguration. Class), see how the name of the class is to initialize configuration, its source code is as follows:

@Configuration
public class EurekaServerInitializerConfiguration
		implements ServletContextAware.SmartLifecycle.Ordered {
    
    // One point worth noting: SmartLifecycle, whose start() method is called after the bean has been initialized for loading.
    
    // Inject a bunch of beans. .// By implementing SmartLifecycle, called in the Spring lifecycle
	@Override
	public void start(a) {
        // Start another thread
        new Thread(new Runnable() {
            @Override
            public void run(a) {
                try {
                    // Major: Initializes eureka-server
                    eurekaServerBootstrap.contextInitialized(
                        EurekaServerInitializerConfiguration.this.servletContext); . . }catch(Exception ex) { ... . } } }).start(); }}Copy the code


  1. EurekaServerBootstrapInitialize the

This can be compared to eureka-server source code analysis to see!!

public class EurekaServerBootstrap {
    public void contextInitialized(ServletContext context) {
        try {
            // 1. Initialize environment variables
            initEurekaEnvironment();
            // 2. Initialize context
            initEurekaServerContext();

            context.setAttribute(EurekaServerContext.class.getName(), this.serverContext);
        }
        catch (Throwable e) {
            log.error("Cannot bootstrap eureka server :", e);
            throw new RuntimeException("Cannot bootstrap eureka server :", e); }}}Copy the code