Springclub02 Eureka registry


2、 Registry

1. Introduction to Eureka

Eureka is the core service of microservice system
Role: registration and discovery
To start all services, you need to register your address in the registry; to call other services, you need to get the registry from the registry to find other services.
Service ID
Host address

2. The working mechanism of Eureka:

  • register

    • When the service provider starts, it will repeatedly try to register with Eureka until the registration is successful
  • Pull

    • Consumers pull the registry every 30 seconds to refresh it
  • heartbeat

    • The service provider sends heartbeat data to Eureka every 30 seconds
    • If the Eureka server fails to receive the heartbeat of a service three times in a row, it will delete the registration information of the service
  • Self protection model

    • If the network is unstable, 85% of servers have abnormal heartbeat within 15 minutes, they will automatically enter the self-protection mode
    • All registration information will not be deleted
    • After the network is restored, it can automatically exit the protection mode and return to normal
    • During development and debugging, protection mode can be disabled to avoid affecting the test

3. Build Eureka server

3.1 create eurekaserver project

Springclub02 Eureka registry

3.2 editing pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>






3.3 modification application.yml

First of all application.propertie Change to application.yml

    name: eureka-server
  port: 2001
    enable-self-preservation: false
    hostname: eureka1
    register-with-eureka: false
    fetch-registry: false
  • Between Eureka cluster servers, throughhostnameTo distinguish
  • eureka.server.enable-self-preservation
    Eureka’s self-protection status: whether the rate of heartbeat failure exceeds 85% within 15 minutes. If it exceeds 85%, Eureka server will protect the current instance registration information and prompt a warning. Once it enters the protection mode, Eureka server will try to protect the information in its service registry and will not delete the data in the service registry. That is, no micro services will be logged off
  • eureka.client.register-with-eureka=false
    Do not register with yourself
  • eureka.client.fetch-registry=false
    Do not pull registration information from itself
  • eureka.instance.lease-expiration-duration-in-seconds
    After the last heartbeat, how long does it take to determine that the microservice is not available? The default value is 90

3.4 modify main program

  • add to@EnableEurekaServer
package cn.tedu.sp05;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
public class Sp05EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(Sp05EurekaApplication.class, args);

3.4 modify hosts file and add Eureka domain name mapping

Add content:       eureka1       eureka2` 

3.5 start and access the test

Springclub02 Eureka registry

Springclub02 Eureka registry

4. Service provider

  • Modify the item service, user service and order service to register the microservice with Eureka server

four point one pom.xml Add Eureka dependency


four point two application.yml Add Eureka registration configuration

      defaultZone: http://eureka1:2001/eureka
  • eureka.instance.lease-renewal-interval-in-seconds
    Heartbeat interval, default 30 seconds
  • The default zone, the default location, can be modified to a specific geographic location, such as Beijing, Shanghai, Shenzhen, etc., which indicates the deployment location of Eureka server and needs to be provided by the cloud server
  • eureka.client.registry-fetch-interval-seconds
    The time interval for pulling registration information is 30 seconds by default

4.3 start the service and view the registration information in Eureka

Springclub02 Eureka registry

5. High availability of Eureka and “service provider”

5.1 high availability of item service

startup parameter--server.portYou can override the port configuration in YML

5.1.2 configure startup parameters
  • item-service-8001

Springclub02 Eureka registry

  • item-service-8002

Springclub02 Eureka registry

5.1.3 start up test

Springclub02 Eureka registry

  • Visit Eureka to view item service registration information

Springclub02 Eureka registry

5.2 Eureka high availability

5.2.1. Add profile configuration files of two servers


    hostname: eureka1
    Register with Eureka: the configuration of true # profile will override the public configuration
    Fetch registry: the configuration of true # profile will override the public configuration
      defaultZone:  http://eureka2 : 2002 / Eureka # register with eureka2 when eureka1 starts`


    hostname: eureka2
    Register with Eureka: the configuration of true # profile will override the public configuration
    Fetch registry: the configuration of true # profile will override the public configuration
      defaultZone:  http://eureka1 : 2001 / Eureka # register with eureka1 when eureka2 starts
5.2.2 configure startup parameters


  • Eureka1 startup parameters:
--spring.profiles.active=eureka1 --server.port=2001

Springclub02 Eureka registry

  • Eureka2 startup parameters:
--spring.profiles.active=eureka2 --server.port=2002

Springclub02 Eureka registry

5.2.3 visit Eureka server to view the registration information

Springclub02 Eureka registry

Springclub02 Eureka registry

Springclub02 Eureka registry

5.2.4 when registering Eureka client, register with two servers

Modify the following micro services

  • sp02-itemservice
  • sp03-userservice
  • sp04-orderservice
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka 

When one Eureka service goes down, it can still connect to another Eureka service