The server is initialized

Find the Spring-cloud-Netflix-eureka-server file in the project dependencies

   org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration
Copy the code

EurekaServerAutoConfiguration initialization

@Configuration(proxyBeanMethods = false)
//EurekaServer is initialized
@Import(EurekaServerInitializerConfiguration.class)
/ / this is a logo for EurekaServerMarkerConfiguration Also for the import in @ EurekaServer registration
/ / EurekaServerMarkerConfiguration at start-up time. The Marker will exist
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class, InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerAutoConfiguration implements WebMvcConfigurer {...Copy the code
@Configuration(proxyBeanMethods = false)
public class EurekaServerInitializerConfiguration
    implements ServletContextAware.SmartLifecycle.Ordered {
  @Override
  public void start(a) {
    new Thread(() -> {
      try {
        / / eurekaServerBootstrap EurekaServerAutoConfiguration has been initialized
        eurekaServerBootstrap.contextInitialized(
            EurekaServerInitializerConfiguration.this.servletContext);
        log.info("Started Eureka Server");
      }catch(Exception ex) { } }).start(); }}Copy the code

The contextInitialized method in the EurekaServerBootStrap class

// Set some eureka configuration information
initEurekaEnvironment();
// Initializing the Eureka Server context will also copy the corresponding registration information from the adjacent Eureka Server node
// int registryCount = this.registry.syncUp();
initEurekaServerContext();
Copy the code

EurekaServerContextInitialize the

@Bean
@ConditionalOnMissingBean
public EurekaServerContext eurekaServerContext(ServerCodecs serverCodecs, PeerAwareInstanceRegistry registry, PeerEurekaNodes peerEurekaNodes) {
		return new DefaultEurekaServerContext(this.eurekaServerConfig, serverCodecs,
				registry, peerEurekaNodes, this.applicationInfoManager);
}
Copy the code

In the DefaultEurekaServerContext core code is as follows:

/** * represents the local server context and provides the component's get method */
@Singleton
public class DefaultEurekaServerContext implements EurekaServerContext {
    // The constructor and getters are omitted
    @PostConstruct
    @Override
    public void initialize(a) {
        // The Eureka server is started
        peerEurekaNodes.start();
        / / registry initialization, try PeerAwareInstanceRegistryImpl# implementation the init methodregistry.init(peerEurekaNodes); }}Copy the code

PeerEurekaNodes# start,

By default, PeerEurekaNode is updated every 10 minutes

public void start(a) {
  // Initialize the task executortaskExecutor = Executors.newSingleThreadScheduledExecutor( ... ) ;try {
    // Update node information
    updatePeerEurekaNodes(resolvePeerUrls());
    Runnable peersUpdateTask = new Runnable() {
      @Override
      public void run(a) { updatePeerEurekaNodes(resolvePeerUrls()); }};// Update node information periodically
    taskExecutor.scheduleWithFixedDelay(
      peersUpdateTask,
      // Update information between Eureka nodes takes 10 minutes by defaultserverConfig.getPeerEurekaNodesUpdateIntervalMs(), serverConfig.getPeerEurekaNodesUpdateIntervalMs(), TimeUnit.MILLISECONDS ); }}Copy the code

PeerAwareInstanceRegistryImpl initialization

@Override
public void init(PeerEurekaNodes peerEurekaNodes) throws Exception {
  this.numberOfReplicationsLastMin.start();
  this.peerEurekaNodes = peerEurekaNodes;
  initializedResponseCache();
  // Update the lease threshold periodically
  scheduleRenewalThresholdUpdateTask();
  / / if the configured for remote region, it is set in the remote region to regionNameVSRemoteRegistry
  initRemoteRegionRegistry();
}
Copy the code

conclusion

  • Joined the @ EnableEurekaServer EurekaServer end, will create a bean EurekaServerMarkerConfiguration. The Marker
  • Have Markerbean, activate the EurekaServerAutoConfiguration, In this class to create EurekaServerBootstrap, PeerAwareInstanceRegistry, PeerEurekaNodes, EurekaServerContext core classes, etc
  • EurekaServerBootstrap performs some initialization actions, such as setting the eurekaServer context
  • EurekaServerContext to create initialization time: start peerEurekaNodes, initialize PeerAwareInstanceRegistry