Microservice architecture | 3.1 Netflix Eureka registry



reference material
《Spring Microservices in Action》
Principle and practice of spring cloud Alibaba microservice
“Spring cloud framework development tutorial in Silicon Valley of station B” Zhou Yang
Spring cloud Netflix official information

Eureka is a service discovery framework developed by Netflix. It is a rest based service. It is mainly used to locate the middle tier services running in the AWS domain to achieve the purpose of load balancing and middle tier service failover;

Spring cloud integrates it into its sub project spring cloud Netflix to realize the service discovery function of spring cloud;

1. Eureka Basics

1.1 server and client in Eureka model

  • The server: service registration center, which is responsible for maintaining and managing the list of registered services;
  • client: all services to be registered into Eureka are called Eureka clients, which can be spring cloud config of the service configuration center; It can also be a gateway zuul; It can also be actual business services, etc;

1.2 Eureka’s 30s start-up mechanism

  • When the client service is registered through Eureka, Eureka will wait for three consecutive health checks within 30s before obtaining the service through Eureka;
  • It takes 30 s for each service registration to be displayed in Eureka service, because Eureka needs to receive three times from the service. Continuous heartbeat packet Ping, with an interval of 10 s for each heartbeat packet Ping, before using this service;

1.3 why does Eureka register the IP of the service instead of the host name

  • For details of specific examples, please refer to “3.2 modifying bootstrap.yml configuration file”;
  • When in a server based environment: when Eureka attempts to register the client service, the client service will be assigned a DNS supported host name, and the client service will contact the outside world through this DNS host name;
  • In container based deployment (such as docker): the container starts with a randomly generated host name, and there is no DNS record; The host name cannot be resolved;

1.4 high availability of Eureka server (cluster)

  • In actual production, in order to realize the high availability of Eureka, multiple Eureka servers need to be established to copy the contents of the registry to each other;
  • A set of Eureka registries communicate with each other using a point-to-point communication model;
  • Need to be on the clienteureka.serviceUrl.defaultZoneSeparate the positions of multiple Eureka servers with commas in the attribute;
  • At the same time, the servereureka.serviceUrl.defaultZoneSeparate multiple locations of Eureka server except itself with commas in the attribute;

2. Build Eureka server

2.1 introduction of POM XML dependency


  • Eureka’s Maven dependency file contains ribbon components by default;
  • Choose one of the old and new versions;

2.2 modify application Yaml profile

  Port: 8761 #eureka listening port
    Name: Eureka server #eureka server name
    Fetch registry: false # do not cache registry information locally
    Register with Eureka: false # do not register with Eureka service
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    hostname: localhost
  Server: # configuration attribute. However, due to Eureka self-protection mode and long heartbeat cycle, Eureka server often fails to eliminate the closed nodes
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 5000
    Waittimelnmswhensyncempty: 5 # the initial time to wait before the server receives the request. This attribute should be commented out in actual production
  • If you need to register components in Eureka, you need toeureka.client.register-with-eurekaSet to true, or ignore it, and use the default value of true;
  • Only Eureka’s clients need toeureka.client.register-with-eurekaSet to true;

2.3 mark notes on the main program class

@EnableEurekaServer: indicates that the service is Eureka server;

3. Register the client into Eureka server

3.1 introduction of POM XML dependency

  • Because each service is basically registered in Eureka server, the dependency can be added to the parent project to represent the public dependency;

3.2 modify bootstrap YML profile

    Name: XXX client # application ID, often written in bootstrap YML lower
    active: dev
      enabled: true
    Preferipaddress: true # registers the IP of the service instead of the service name
    Registerwitheureka: true # registers the service with Eureka
    Fetchregistry: true # pull the local copy of the registry
      defaultZone:  http://localhost:8761/eureka/ #Eureka service module
  • Each service registered through Eureka will have two associated IDS:
    • Application ID: indicates a group of service entities, D is alwaysspring.application.nameProperty (written in the bootstrap.yml configuration file instead of application.xml);
    • Instance ID: a random number used to represent a single service instance;
  • Why register the IP of the service instead of the host name:
    • In the server based environment: when Eureka attempts to register the client service, the client service will be assigned a DNS supported host name, and the client service will contact the outside world through this DNS host name;
    • In container based deployment (such as docker): the container starts with a randomly generated host name without DNS records; The host name cannot be resolved;

3.3 mark the annotation of service discovery on the main program class

  • @EnableEurekaClient: Eureka is recommended as the registration center;
  • @EnableDiscoveryClient: it is recommended to use other components as the registration center;

4. Self protection of Eureka server

4.1 fault phenomenon

  • If you see the following prompt on the homepage of Eureka server, Eureka has entered the protection mode:

Eureka 的自我保护

  • Once Eureka server enters self-protection mode, it will try to protect the information in its service registry,The data in the service registry will not be deleted, nor will any microservices be unregistered

4.2 overview of self-protection

  • The protection mode is mainly used for the connection between a group of clients and Eureka serverNetwork partition scenarioProtection under;
  • In order to prevent the Eureka client from running normally, but when the network with Eureka server is disconnected, Eureka server will not immediately eliminate the Eureka client service;

4.3 self protection principle

  • By default, if Eureka server does not receive the heartbeat of a micro service instance within a certain period of time, Eureka server will log off the instance (90 seconds by default);
  • But whenNetwork partition failureIn case of (delay, jamming, congestion), the normal communication between the micro service and Eureka server cannot be performed, and the above behaviors may become very dangerous;
  • Because the micro service itself is actually healthy, it should not be cancelled at this time;
  • Eureka solves this problem through “self-protection mode”: when Eureka server node loses too many clients in a short time (network partition failure may occur), the node will enter the self-protection mode;
  • In self-protection mode, Eureka server willProtect the information in the service registry and do not unregister any service instances


4.4 close the self-protection mechanism

  • The following configuration can be used to turn off the self-protection mechanism to ensure that unavailable services are kicked out in time:
  • eureka.server.enable-self-preservation = false


Newcomer production, if there are mistakes, welcome to point out, thank you very much!Welcome to follow official account and share some more daily things!If you need to reprint, please mark the source!
Microservice architecture | 3.1 Netflix Eureka registry

Recommended Today

A front-end developer's Vim is the same as an IDE

Here is my new configurationjaywcjlove/vim-webI've been grinding it, and it's basically ready to use. Take it out and cheat the star Install The latest version of Vim 7.4+ uses (brew install macvim) installation, vim version updatebrew install macvim –override-system-vim View configuration locations # Enter vim and enter the following characters :echo $MYVIMRC download vim-web Download […]