Eureka service registration and discovery I

Eureke service registration and discovery

What is Eureke?

  • Eureka: English / ju ˈ ri ː k ə/ Beauty / ju ˈ ri ː k ə/ Eureka Eureka
  • NetFilx followed the AP principle when designing Eureka
  • Eureka is a sub module and one of the core modules of NetFilx. Eureka is a REST based service used to locate services to realize cloud middle tier service discovery and failover. Service registration and discovery are very important for microservices. With service discovery and registration, you can access services only by using the service identifier without modifying the configuration file of service invocation, The function is similar to Dubbo's registry, such as Zookeeper

Principle explanation

  • Eureka's basic architecture

    • Spring cloud encapsulates the Eureka module developed by NetFIlx company to realize service registration and discovery
    • Eureka adopts the c-s architecture design. Eureka server is the server of service registration function. It is the service registration center
    • Other microservices in the system use Eureka's client to connect to Eureka server and maintain heartbeat connection, so that system maintenance personnel can monitor whether each microservice in the system operates normally through Eureka server, and some other modules of spring cloud (such as Zuul) can discover other microservices in the system through Eureka server and execute relevant logic
    • Eureka consists of two components: Eureka Server and Eureka Client
    • Eureka Server provides service registration service. After each node is started, it will be registered in Eureka Server. In this way, the service registry in Eureka Server will store the information of all available service nodes, and the information of service nodes can be seen intuitively in the interface.
    • Eureka Client is a java client, which is used to simplify the interaction of Eureka Server. The client also has a built-in load balancer using polling load algorithm. After the application starts, it will send heartbeat to Eurake Server (the default cycle is 30 seconds) If Eureka Server removes this service node from the server (the default period is 90 seconds)
  • Three roles

    • Eureka Server: provides service registration and discovery, similar to Zookeeper
    • Service Provider: register itself in Eureka so that consumers can find
    • Service Consumer: the Service Consumer obtains the service registration list from Eurake to find the service for consumption.

Write Eureka registry

spring cloud is like four steps

    1. Import dependency
    1. Write configuration
    1. Write configuration class
    1. Open annotation
  1. Import dependency
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""


  1. Write configuration information
  • application.yml
  port: 7001

#Eureka registration center is basically configured in this way
    hostname: localhost # Instance name of Eurake server
    register-with-eureka: false # Indicates whether to register yourself with the Eureka registry
    fetch-registry: false # If the catch registry is false, it means that it is the registry
    service-url: # Monitoring page
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  1. Write the startup class and open the annotation
package cn.lzm.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableEurekaServer //That's the comment
public class EurakeServer_7001 {

    public static void main(String[] args) {,args);
  1. Test, view the monitoring page (#. #)

Open the service registration with the previously written service provider code

  1. In POM Add Eureka client to XML
  <!--Eureka client-->
  1. Add the @ EnableEurekaClient annotation for the startup class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class PersonProviderApplication {
    public static void main(String[] args) {,args);
  1. Client configuration
# Eureka client configuration
    appname: app
      defaultZone: http://localhost:7001/eureka / # points to the registry
  1. After successful opening, you can see the registration information in Eureka registration center

Configure Eureka monitoring information

  • Eureka can click in to view the information. What we are doing now is to improve it
  1. pom.xml add
  1. to configure
#info configuration
info: laosi-springcloud # app name cn.lzm #corporate name
  1. View and click the page to see the json data, which is just configured

Self protection mechanism

One sentence summary: when a micro service is unavailable at a certain time, Eureka will not clean it up immediately, but will still save the information of the micro service!

  • By default, if EurekaServer does not receive the heartbeat of a micro service instance within a certain period of time, EurekaServer will log off the instance (the default time is 90s). However, when a network partition failure occurs, there is no normal traffic between the microservice and Eureka, and the above behavior may become very dangerous - because the microservice itself is actually healthy, the service should not be cancelled at this time. Eureka solves this problem through the self-protection mechanism. 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. Once it enters the mode, EurakeServer will protect the information in the service registry and will not delete the data in the service registry (that is, it will not log off any microservices) However, when the fault is recovered, the EurekaServer node will automatically exit the self-protection mode.
  • In the self-protection mode, EurekaServer will protect the information in the service registration and will not unregister any service instances. When the number of heartbeats it receives recovers above the threshold, the EurekaServer node will automatically exit the self-protection mode. His design philosophy is to retain the wrong service registration information rather than blindly cancel any possible healthy service instances. In a word, living is better than dying.
  • To sum up, self-protection mode is a security protection measure to deal with network abnormalities. Its architectural philosophy is to retain all micro services at the same time (both healthy and unhealthy micro services will be retained), rather than blindly cancel any healthy micro services. Using self-protection mode can make Eureka cluster more robust and stable
  • In spring cloud, you can use Eureka server. Enable self preservation = false disable self-protection mode [it is not recommended to turn off beat me protection mode]

Service discovery

  • When you let other services access it, you can get some service information
  1. Provide interface
import cn.lzm.springcloud.pojo.Person;
import cn.lzm.springcloud.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
 * Key, providing Restful services
public class PersonController {
    PersonService personService;
    //Get some information and get specific micro services
    DiscoveryClient discoveryClient;
    public Object discover(){
        //Get the list of micro services
        List<String> services = discoveryClient.getServices();
        System.out.println("discover=>services:  "+services);
        //Get a specific micro service information
        List<ServiceInstance> list = discoveryClient.getInstances("app");
        for (ServiceInstance serviceInstance : list) {
        return discoveryClient;

    public Person getPersonById(@PathVariable("id") int id) {
        return personService.getPersonById(id);

    public String addPerson(Person person) {
    public List<Person> getAllPerson() {
        return personService.getAllPerson();
  1. Turn on service discovery
@EnableDiscoveryClient //Allow discovery services
public class PersonProviderApplication {
    public static void main(String[] args) {,args);
  1. Test it

Tags: Spring Cloud

Posted by dysonline on Mon, 02 May 2022 14:41:06 +0300