Dubbo implementation principle and source code analysis — Fine collection Netty implementation principle and source code analysis — boutique collection
“Spring Implementation principles and source code analysis — Boutique Collection” MyBatis implementation principle and source code analysis — boutique collection
Spring MVC Implementation Principle and source code Analysis — A collection of fine works Database Entity Design Collection

Abstract: the original source www.iocoder.cn/Eureka/eure… “Taro source” welcome to reprint, keep the summary, thank you!

This article is mainly based on Eureka version 1.8.X

  • 1. An overview of the
  • 2. EurekaClientConfig
    • 2.1 Class diagram
    • 2.2 Configuring Attributes
    • 2.3 DefaultEurekaClientConfig
    • 2.4 DefaultEurekaClientConfigProvider
    • 2.5 summary
  • 3. EurekaTransportConfig
    • 3.1 Class diagram
    • 3.2 Configuring Attributes
    • 3.3 DefaultEurekaTransportConfig
  • 666. The eggs

🙂🙂🙂 follow wechat public number:

  1. RocketMQ/MyCAT/Sharding-JDBC all source code analysis article list
  2. RocketMQ/MyCAT/Sharding-JDBC 中文 解 决 source GitHub address
  3. Any questions you may have about the source code will be answered carefully. Even do not know how to read the source can also ask oh.
  4. New source code parsing articles are notified in real time. It’s updated about once a week.
  5. Serious source communication wechat group.

1. An overview of the

This article is based on “Eureka source code parsing — EurekaClient initialization (1) EurekaInstanceConfig”, mainly share the second part of EurekaClient initialization process — EurekaClientConfig, The registration process of Eureka-client to Eureka-server is not included (a separate article will be shared at 🙂).

The main objects involved in eureka-client initialization are as follows:

  1. Create the EurekaInstanceConfig object
  2. Create the InstanceInfo object using the EurekaInstanceConfig object
  3. Create the ApplicationInfoManager object using the EurekaInstanceConfig object + InstanceInfo object
  4. Create the EurekaClientConfig object
  5. Use the ApplicationInfoManager object + EurekaClientConfig object to create the EurekaClient object

Considering that the whole process of initialization involves a lot of configuration, it is divided into three articles:

  1. (a) EurekaInstanceConfig)
  2. (2) EurekaClientConfig
  3. (3) EurekaClient

Let’s look at the implementation of each class.

Recommended Spring Cloud books:

  • Please support the legal version. Download piracy, is equal to the initiative to write low-level bugs.
  • DD — Spring Cloud Micro Services
  • Zhou Li — “Spring Cloud and Docker Micro-service Architecture Combat”
  • Buy two books together, jingdong free delivery.

Spring Cloud

2. EurekaClientConfig

Com.net flix. Discovery. EurekaClientConfig, Eureka – Client configuration interface.

2.1 Class diagram

The overall class relationship of EurekaClientConfig is shown as follows:

  • This article only parses the red-circled classes.
  • EurekaArchaius2ClientConfig based on Netflix Archaius 2 x, at present is still in development, so no parsing.

2.2 Configuring Attributes

Click EurekaClientConfig to view the introduction of configuration properties. Chinese annotations have been added, which can be understood together with English annotations. Here, the author has selected some important attributes:

  • Region and Zone are related

    • #getRegion(): Area where Eureka-client is located (region).
    • #getAvailabilityZones() : A collection of available zones in eureka-client regions. Although this parameter is an array, the first element represents the available area in which it resides. The implementation code is as follows:

      // InstanceInfo.java public static String getZone(String[] availZones, InstanceInfo myInfo) { String instanceZone = ((availZones == null || availZones.length == 0) ? "default" : availZones[0]); if (myInfo ! = null && myInfo.getDataCenterInfo().getName() == DataCenterInfo.Name.Amazon) { String awsInstanceZone = ((AmazonInfo) myInfo.getDataCenterInfo()) .get(AmazonInfo.MetaDataKey.availabilityZone); if (awsInstanceZone ! = null) { instanceZone = awsInstanceZone; } } return instanceZone; }Copy the code
      • x
      • Understanding Region and Zone See Zhouli Region and Zone Analysis.
  • Obtain the eureka-server URL using DNS
    • #shouldUseDnsForFetchingServiceUrls(): Specifies whether to obtain the EUreka-server URL in DNS mode.
    • #getEurekaServerDNSName(): indicates the DNS name of eureka-server.
    • #getEurekaServerPort(): Indicates the port of eureka-server.
    • #getEurekaServerURLContext(): URL Context of eureka-server.
    • #getEurekaServiceUrlPollIntervalSeconds(): Obtains the change frequency of the EUreka-server address in round robin, in seconds. Default: 300 seconds.
    • #shouldPreferSameZoneEureka(): Preferentially use the same area (zoneEureka – Server).
  • Directly associated with eureka-server URL
    • #getEurekaServerServiceUrls(): URL set of eureka-server.
  • Found: Related to obtaining registration information from Eureka-server
    • #shouldFetchRegistry(): Indicates whether to pull registration information from eureka-server.
    • #getRegistryFetchIntervalSeconds(): Frequency of pulling registration information from eureka-server, unit: second. Default: 30 seconds.
    • #shouldFilterOnlyUpInstances(): Indicates whether to filter. Only the set of application instances in the Up state is obtained.
    • #fetchRegistryForRemoteRegions(): TODO [0009] : RemoteRegionRegistry
    • #getCacheRefreshExecutorThreadPoolSize(): Register information cache refresh thread pool size.
    • #getCacheRefreshExecutorExponentialBackOffBound(): Indicates the delayed retry time after the registration information cache refreshes.
    • #getRegistryRefreshSingleVipAddress(): Get only onevipAddressRegistration information of the corresponding application instances.
      • Implementation logic and “Eureka Source Code Parsing — Application Instance Registration Discovery (6) : Full Acquisition”
      • This series of temporary write on its source code analysis, interested students can seecom.netflix.discovery.shared.transport.EurekaHttpClient#getVip(String, String...)com.netflix.eureka.resources.AbstractVIPResource
  • Register: Register your service with eureka-Server
    • #shouldRegisterWithEureka(): Indicates whether to register the service with eureka-server.
    • #shouldUnregisterOnShutdown(): Indicates whether to cancel registration with eureka-server when the process is shut down.
    • #getInstanceInfoReplicationIntervalSeconds(): Indicates the change frequency of application instance information to be synchronized to eureka-server (unit: second).
    • #getInitialInstanceInfoReplicationIntervalSeconds(): Synchronization of application information to eureka-server Initialization delay (unit: second).
    • #getBackupRegistryImpl(): Gets the backup registry implementation class. When eureka-Client started, registration information could not be read from Eureka-Server (possibly hung) and was read from the backup registry. Currently eureka-Client does not provide a suitable implementation.
    • #getHeartbeatExecutorThreadPoolSize(): Size of the heartbeat execution thread pool.
    • #getHeartbeatExecutorExponentialBackOffBound(): Delay retry time after the heartbeat execution times out.

2.3 DefaultEurekaClientConfig

Com.net flix. Discovery. DefaultEurekaClientConfig, based on the configuration file of Eureka – Client configuration implementation class, the implementation code is as follows:

public class DefaultEurekaClientConfig implements EurekaClientConfig { public static final String DEFAULT_ZONE = "defaultZone"; private final String namespace; private final DynamicPropertyFactory configInstance; private final EurekaTransportConfig transportConfig; Public DefaultEurekaClientConfig (String namespace) {/ / set the namespace, This. namespace = namespace.endsWith(".")? Namespace: namespace + "."; / / initialization configuration file object enclosing configInstance = Archaius1Utils. InitConfig (CommonConstants. CONFIG_FILE_NAME); / / create the HTTP transport configuration enclosing transportConfig = new DefaultEurekaTransportConfig (namespace, configInstance); }}Copy the code
  • A similar PropertiesInstanceConfig, Click “Eureka source code analysis — Eureka Client initialization (1) EurekaInstanceConfig” “2.4 PropertiesInstanceConfig” for detailed analysis.
  • incom.netflix.discovery.PropertyBasedClientConfigConstantsYou can see each property KEY of the configuration file.
  • transportConfigProperties,”3. EurekaTransportConfig”Detailed analysis.

2.4 DefaultEurekaClientConfigProvider

Com.net flix. Discovery. Will DefaultEurekaClientConfigProvider, create DefaultEurekaClientConfig factory, the implementation code is as follows:

public class DefaultEurekaClientConfigProvider implements Provider<EurekaClientConfig> { @Inject(optional = true) @EurekaNamespace private String namespace; private DefaultEurekaClientConfig config; @Override public synchronized EurekaClientConfig get() { if (config == null) { config = (namespace == null) ? new DefaultEurekaClientConfig() : new DefaultEurekaClientConfig(namespace); // TODO: Remove this when DiscoveryManager is finally no longer used DiscoveryManager.getInstance().setEurekaClientConfig(config); } return config; }}Copy the code

2.5 summary

Recommended reading:

  • Program ape DD – “Spring Cloud Micro service Combat” Spring Cloud Eureka – configuration details
  • “Microservice Architecture: Details of Eureka Parameter Configuration”

3. EurekaTransportConfig

3.1 Class diagram

The overall class relationship of EurekaTransportConfig is shown as follows:

  • This article only parses the red-circled classes.
  • EurekaArchaius2TransportConfig based on Netflix Archaius 2 x, at present is still in development, so no parsing.

3.2 Configuring Attributes

Click EurekaTransportConfig to view the brief introduction of configuration properties. Chinese annotations have been added, which can be understood together with English annotations. Here, the author has selected some important attributes:

3.3 DefaultEurekaTransportConfig

Com.net flix. Discovery. Shared. Transport. DefaultEurekaTransportConfig, based on the network configuration implementation class configuration file, the implementation code is as follows:

public class DefaultEurekaTransportConfig implements EurekaTransportConfig { private static final String SUB_NAMESPACE =  TRANSPORT_CONFIG_SUB_NAMESPACE + "."; private final String namespace; private final DynamicPropertyFactory configInstance; public DefaultEurekaTransportConfig(String parentNamespace, DynamicPropertyFactory configInstance) {this.namespace = parentNamespace == null? (parentNamespace.endsWith(".") ? parentNamespace + SUB_NAMESPACE : parentNamespace + "." + SUB_NAMESPACE); // Config file object this.configInstance = configInstance; }}Copy the code
  • A similar PropertiesInstanceConfig, Click “Eureka source code analysis — Eureka Client initialization (1) EurekaInstanceConfig” “2.4 PropertiesInstanceConfig” for detailed analysis.
  • incom.netflix.discovery.shared.transport.PropertyBasedTransportConfigConstantsYou can see each property KEY of the configuration file.

666. The eggs

When it comes to configuration, the content will seem a lot at first, but it will become very “wordy” after you understand it. Please be patient.

Fat friend, share a circle of friends.