In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "EurekaServer automatic assembly method and startup process". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "EurekaServer automatic assembly method and startup process"!
@ EnableEurekaServer comment
We know that when using Eureka as the registry, we will add a @ EnableEurekaServer annotation to the startup class, which we are a custom EnableXXX series annotation, which we have mentioned many times before, just to introduce the configuration class. Take a look at the source code
@ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Import ({EurekaServerMarkerConfiguration.class}) public @ interface EnableEurekaServer {}
A configuration class EurekaServerMarkerConfiguration has been introduced. Take a look at the details of this class.
@ Configurationpublic class EurekaServerMarkerConfiguration {@ Bean public Marker eurekaServerMarkerBean () {return new Marker ();} class Marker {}}
Now it seems difficult to understand here, what does this mean? what do you want to do with an empty class? don't worry, move on.
Automatic assembly
Since we can't find what we want on the annotation, let's take a look at the spring.factories file, where the autoconfigured implementation class is EurekaServerAutoConfiguration
Since there is so much code involved in this class, I won't post it here, let's parse the class directly:
1. Introduce the EurekaServerInitializerConfiguration class
From the name, you can see that this class is responsible for initializing Eureka. This class implements the SmartLifecycle interface, so when spring is initialized and destroyed, its start and stop methods are called respectively.
First take a look at the start method
Public void start () {new Thread (new Runnable () {@ Override public void run () {try {/ / launch EurekaServereurekaServerBootstrap.contextInitialized (EurekaServerInitializerConfiguration.this.servletContext)) Log.info ("Started EurekaServer"); 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 ();}
This code seems quite straightforward, ah, just start a thread to start EurekaServer, and then release some startup events. Let's take a look at the startup process.
Public void contextInitialized (ServletContext context) {try {/ / initialize execution environment initEurekaEnvironment (); / / 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);}}
There are two branches: initialization environment and initialization context.
Initialize the execution environment
This is not very important. It can be filtered out.
Protected void initEurekaEnvironment () throws Exception {log.info ("Setting the eureka configuration.."); / / things related to AWS, you can ignore String dataCenter = ConfigurationManager.getConfigInstance () .getString (EUREKA_DATACENTER) If (dataCenter = = null) {log.info ("Eureka datacenter value eureka.datacenter is not set, defaulting to default"); ConfigurationManager.getConfigInstance () .setProperty (ARCHAIUS_DEPLOYMENT_DATACENTER, DEFAULT) } else {ConfigurationManager.getConfigInstance () .setProperty (ARCHAIUS_DEPLOYMENT_DATACENTER, dataCenter);} / / sets the Eureka environment. Default is test String environment = ConfigurationManager.getConfigInstance () .getString (EUREKA_ENVIRONMENT) If (environment = = null) {ConfigurationManager.getConfigInstance () .setProperty (ARCHAIUS_DEPLOYMENT_ENVIRONMENT, TEST); log.info ("Eureka environment value eureka.environment is not set, defaulting to test") } else {ConfigurationManager.getConfigInstance () .setProperty (ARCHAIUS_DEPLOYMENT_ENVIRONMENT, environment);}}
Initialization context
Protected void initEurekaServerContext () throws Exception {/ / sets json and xml serialization tools 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"); / / synchronize Eureka cluster data int registryCount = this.registry.syncUp (); this.registry.openForTraffic (this.applicationInfoManager, registryCount); / / register monitoring statistics EurekaMonitors.registerAllStats ();}
There are a lot of contents involved in synchronizing cluster data and registration monitoring information in this method, so this article will no longer be carried out, please pay attention to my follow-up articles.
@ ConditionalOnBean ({Marker.class})
This reveals the meaning of the bean injected into the @ EnableEurekaServer annotation at the beginning. That is to say, if our startup class does not use the @ EnableEurekaServer annotation, the automatic configuration class will not be executed, and there will be no Eureka.
@ EnableConfigurationProperties ({EurekaDashboardProperties.class, InstanceRegistryProperties.class})
Going deep into this annotation, it is found that the @ Import annotation mechanism introduces two classes. This annotation has been mentioned many times in previous source code parsing articles, so I won't expand it here.
EurekaDashboardProperties is a relatively simple class, which is mainly related to the configuration of the Eureka console.
/ / default path to the console private String path = "/"; / / whether to enable the console private boolean enabled = true
InstanceRegistryProperties, this class is the configuration information that controls the registration of the Eureka
/ / the number of renewals per minute @ Value ("${eureka.server.expectedNumberOfRenewsPerMin:1}") private int expectedNumberOfRenewsPerMin = 1; / / the default number of open communications @ Value ("${eureka.server.defaultOpenForTrafficCount:1}") private int defaultOpenForTrafficCount = 1 property Source ("classpath:/eureka/server.properties")
I believe you are familiar with this note, just loading the configuration file of Eureka.
Only this information is contained in the configuration file.
Bean automatically injected by spring.http.encoding.force=false
After parsing a few annotations on the EurekaServerAutoConfiguration class, let's take a look at some of the more important classes injected into this class
Configuration class EurekaServerConfigBeanConfiguration
EurekaServerConfig if the current application allows registration with other Eureka services, that is, when the property eureka.client.fetch-registry is true. Set the value of the property registrySyncRetries to 5, which means the number of attempts to get registration information on other servers in the cluster when the Eureka server starts
EurekaController
This is Eureka's own controller, where the relevant information of the console is obtained.
ServerCodecs
Set the serialization tool for Eureka
PeerAwareInstanceRegistry
For classes related to the synchronization of cluster registration information, please look forward to the following in-depth analysis articles.
FilterRegistrationBean so far, I believe you have a deeper understanding of "EurekaServer automatic assembly method and startup process", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.