Recently, it is the peak of job hopping. I worked overtime for many days, sorted out the interview dictionary containing more than 16000 interview questions, and pointed out that Beijun will continue to update the questions in this interview dictionary, hoping it can help you find your favorite job! [receiving method at the end of the document]
As mentioned in the previous article, Eureka server is essentially a web application project. Today, let's take a look at how Spring Boot starts Eureka.
Spring Boot starts Eureka process
@SpringBootApplication @EnableEurekaServer public class EurekaServer { public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); } }
The above code is the most common Spring Boot startup class. The key annotation for Spring Boot to start eureka is @ EnableEurekaServer.
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Import({EurekaServerMarkerConfiguration.class}) public @interface EnableEurekaServer { }
You can see that this annotation imports an EurekaServerMarkerConfiguration class.
@Configuration public class EurekaServerMarkerConfiguration { @Bean public Marker eurekaServerMarkerBean() { return new Marker(); } class Marker { } }
EurekaServerMarkerConfiguration injects an EurekaServerMarkerConfiguration.Marker object into the Spring container. Maker is an empty object and a class that marks switches. The class of the specific switch is in the comments above.
/** * Responsible for adding in a marker bean to activate * {@link EurekaServerAutoConfiguration} * * @author Biju Kunjummen */
The EurekaServerMarkerConfiguration.Marker object is used to activate the eurekaservenautoconfiguration class.
When is the eurekaservenautoconfiguration class loaded?
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration
As shown in the figure above, the call started by eurekaserver autoconfiguration is in spring.factories. During the startup of Spring Boot, all spring.factories will be loaded. At this time, the contents will be read and loaded into Spring.
-
@Import(EurekaServerInitializerConfiguration.class): EurekaServerInitializerConfiguration class will be imported when eurekaservenautoconfiguration is initialized.
-
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class): when an instance of EurekaServerMarkerConfiguration.Marker class exists in Spring, eurekaserveraoconfiguration is also imported into the Spring container.
public class EurekaServerInitializerConfiguration implements ServletContextAware, SmartLifecycle, Ordered
-
ServletContextAware: the implementation of this class can obtain the ServletContext container context.
-
SmartLifecycle: after the Spring container loads all bean s and completes initialization, it will then call back the corresponding start() method in the class that implements the interface
Take a look at what is called in the start() method?
@Override public void start() { new Thread(new Runnable() { @Override public void run() { try { eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext); log.info("Started Eureka Server"); publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig())); EurekaServerInitializerConfiguration.this.running = true; publish(new EurekaServerStartedEvent(getEurekaServerConfig())); } catch (Exception ex) { // Help! log.error("Could not initialize Eureka servlet context", ex); } } }).start(); }
start() starts a thread in which Eureka Server is started. Eurekaserverboot strap is an object that is automatically injected into eurekaserverboot strap. EurekaServerBootstrap has been mentioned in the previous article. It is the startup class of Eureka Server. Finally, look at its contextInitialized() method.
public void contextInitialized(ServletContext context) { try { initEurekaEnvironment(); 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); } }
The contextInitialized method calls initEurekaEnvironment() to initialize Eureka's running environment; initEurekaServerContext(), initializes the context of Eureka.
summary
Spring Boot starts Erueka Server through the following steps:
-
@EnableEurekaServer annotation
-
Injected EurekaServerMarkerConfiguration.Marker object
-
Judge whether EurekaServerMarkerConfiguration.Marker object is injected into the container EurekaServerAutoConfiguration
-
The EurekaServerInitializerConfiguration class that implements the SmartLifecycle interface is imported
-
The Spring container initializes the start() method of the EurekaServerInitializerConfiguration object.
-
start() starts a thread and calls the startup class of Erueka Server: EurekaServerBootstrap.
Interviews include: Java collection, JVM, multithreading, concurrent programming, design patterns, SpringBoot, SpringCloud, Java, MyBatis, ZooKeeper, Dubbo, Elasticsearch, Memcached, MongoDB, Redis, MySQL, RabbitMQ, Kafka, Linux, Netty, Tomcat, Python, HTML, CSS, Vue, React, JavaScript, Android big data Alibaba and other large factory interview questions, technology stack!
It's not easy to share. If you think sharing is helpful to you, follow the old rules, point praise, pay attention and leave messages for support!