order
This article mainly studies Eureka ServerInitializerConfiguration of spring cloud
EurekaServerInitializerConfiguration
spring-cloud-netflix-eureka-server-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/server/EurekaServerInitializerConfiguration.java
@Configuration public class EurekaServerInitializerConfiguration implements ServletContextAware, SmartLifecycle, Ordered { //...... @Override public void start() { new Thread(new Runnable() { @Override public void run() { try { //TODO: is this class even needed now? 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(); } @Override public void stop() { this.running = false; eurekaServerBootstrap.contextDestroyed(this.servletContext); } }
The contextInitialized method is called when start ing here, and then when stop, contextDestroyed is called
EurekaServerBootstrap.contextInitialized
spring-cloud-netflix-eureka-server-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/server/EurekaServerBootstrap.java
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); } }
Here initEurekaEnvironment initializes the environment configuration, with emphasis on initEurekaServerContext
EurekaServerBootstrap.initEurekaServerContext
protected void initEurekaServerContext() throws Exception { // For backward compatibility JsonXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(), XStream.PRIORITY_VERY_HIGH); XmlXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(), XStream.PRIORITY_VERY_HIGH); if (isAws(this.applicationInfoManager.getInfo())) { this.awsBinder = new AwsBinderDelegate(this.eurekaServerConfig, this.eurekaClientConfig, this.registry, this.applicationInfoManager); this.awsBinder.start(); } EurekaServerContextHolder.initialize(this.serverContext); log.info("Initialized server context"); // Copy registry from neighboring eureka node int registryCount = this.registry.syncUp(); this.registry.openForTraffic(this.applicationInfoManager, registryCount); // Register all monitoring statistics. EurekaMonitors.registerAllStats(); }
This calls EurekaServerContextHolder.initialize(this.serverContext), registry.syncUp(), then registry.openForTraffic
EurekaServerContextHolder.initialize(this.serverContext)
eureka-core-1.8.8-sources.jar!/com/netflix/eureka/EurekaServerContextHolder.java
/** * A static holder for the server context for use in non-DI cases. * * @author David Liu */ public class EurekaServerContextHolder { private final EurekaServerContext serverContext; private EurekaServerContextHolder(EurekaServerContext serverContext) { this.serverContext = serverContext; } public EurekaServerContext getServerContext() { return this.serverContext; } private static EurekaServerContextHolder holder; public static synchronized void initialize(EurekaServerContext serverContext) { holder = new EurekaServerContextHolder(serverContext); } public static EurekaServerContextHolder getInstance() { return holder; } }
Reference to Eureka ServerContext mainly for non-IOC containers
syncUp
eureka-core-1.8.8-sources.jar!/com/netflix/eureka/registry/PeerAwareInstanceRegistryImpl.java
/** * Populates the registry information from a peer eureka node. This * operation fails over to other nodes until the list is exhausted if the * communication fails. */ @Override public int syncUp() { // Copy entire entry from neighboring DS node int count = 0; for (int i = 0; ((i < serverConfig.getRegistrySyncRetries()) && (count == 0)); i++) { if (i > 0) { try { Thread.sleep(serverConfig.getRegistrySyncRetryWaitMs()); } catch (InterruptedException e) { logger.warn("Interrupted during registry transfer.."); break; } } Applications apps = eurekaClient.getApplications(); for (Application app : apps.getRegisteredApplications()) { for (InstanceInfo instance : app.getInstances()) { try { if (isRegisterable(instance)) { register(instance, instance.getLeaseInfo().getDurationInSecs(), true); count++; } } catch (Throwable t) { logger.error("During DS init copy", t); } } } } return count; }
Here, client is used to get the instance information of another eureka server, and isReplication=true is used to register instances
openForTraffic
Override public void openForTraffic(ApplicationInfoManager applicationInfoManager, int count) { // Renewals happen every 30 seconds and for a minute it should be a factor of 2. this.expectedNumberOfRenewsPerMin = count * 2; this.numberOfRenewsPerMinThreshold = (int) (this.expectedNumberOfRenewsPerMin * serverConfig.getRenewalPercentThreshold()); logger.info("Got {} instances from neighboring DS node", count); logger.info("Renew threshold is: {}", numberOfRenewsPerMinThreshold); this.startupTime = System.currentTimeMillis(); if (count > 0) { this.peerInstancesTransferEmptyOnStartup = false; } DataCenterInfo.Name selfName = applicationInfoManager.getInfo().getDataCenterInfo().getName(); boolean isAws = Name.Amazon == selfName; if (isAws && serverConfig.shouldPrimeAwsReplicaConnections()) { logger.info("Priming AWS connections for all replicas.."); primeAwsReplicas(applicationInfoManager); } logger.info("Changing status to UP"); applicationInfoManager.setInstanceStatus(InstanceStatus.UP); super.postInit(); }
openForTraffic primarily identifies itself (eureka server) as an UP state before it can begin receiving requests.
eurekaServerBootstrap.contextDestroyed
public void contextDestroyed(ServletContext context) { try { log.info("Shutting down Eureka Server.."); context.removeAttribute(EurekaServerContext.class.getName()); destroyEurekaServerContext(); destroyEurekaEnvironment(); } catch (Throwable e) { log.error("Error shutting down eureka", e); } log.info("Eureka Service is now shutdown..."); } /** * Server context shutdown hook. Override for custom logic */ protected void destroyEurekaServerContext() throws Exception { EurekaMonitors.shutdown(); if (this.awsBinder != null) { this.awsBinder.shutdown(); } if (this.serverContext != null) { this.serverContext.shutdown(); } }
Destroy context and environment variables here, note that there is a this.serverContext!= null judgment, that is, if the serverContext has been destroyed, it will not be called again.
Summary
EurekaServerInitializerConfiguration mainly implements the Lifecycle method, initializes the servlet-related context, calls EurekaServerBootstrap.contextInitialized at start time, and calls eurekaServerBootstrap.contextDestroyed at stop time, all with the help of the eurekaServerBootstrap class.The Eureka ServerContext is called in the first section of the Eureka ServerBootstrap.
Eureka ServerBootstrap.contextInitialized There are two important actions registry.syncUp() and registry.openForTraffic
- registry.syncUp() takes instance information from other Eureka servers, registers it with this server, and copies it to other servers
- registry.openForTraffic() identifies its server as UP, indicating that it can begin receiving requests