Spring cloud core component registry

1. What is a microservice registry?


1.1 basic introduction

  1. Understand registry: service management; The core is to have a service registry; Dynamic maintenance of heartbeat mechanism.
  2. Service provider: report your network information to the registry when starting
  3. Service consumer: when starting, it also reports its own network information to the registration center and pulls the relevant network information of the provider
  4. Each micro service communicates with the registry using a certain mechanism (such as heartbeat). If the registry cannot communicate with a micro service for a long time, the instance will be unregistered.

1.2 why use a microservice registry?

  1. There are more and more microservice applications and machines. The caller needs to know the network address of the interface. If the network address is maintained by means of configuration file, it will be troublesome and difficult to dynamically add machines.
  2. When using the registry: when each micro service is started, it registers its own network address and other information to the registry, which stores these data. The service consumer queries the address of the service provider from the registry and calls the interface of the service provider through the address.

1.3 mainstream registry

zookeeper, eureka, consumer, etcd, etc.

2. Distributed application knowledge CAP theory knowledge

2.1 CAP theorem

It means that in a distributed system, Consistency, Availability and PartitionTolerance cannot be obtained at the same time.

2.2 C,A,P

  1. Consistency (C): whether the values of all data backups in the distributed system are the same at the same time (the data of all nodes at the same time are completely consistent. The more nodes, the more time-consuming data synchronization is)
  2. Availability (A): when the load suddenly increases at A certain time, whether the whole cluster can respond to the client's request (the service is always available and in normal time)
  3. Partition fault tolerance (P): partition tolerance, that is, high availability, that is, if one node collapses, it will not affect other nodes (if 1000 nodes are hung, it will not affect the service. The more nodes, the better)

2.3 summary

CAP theory is that: in distributed storage system, we can only realize the above two points at most. Because the current network hardware will certainly have problems such as delay and packet loss, we must realize partition tolerance. So we have to make a trade-off between consistency and availability.

3. Principle of distributed system CAP common interview questions and selection of Registration Center

  1. C. If A is satisfied, P cannot be satisfied
    Data synchronization (C) takes time, and the service is always available (A) needs to meet the normal response time. If both meet the requirements at the same time, there cannot be too many machines, that is, partition fault tolerance (P) cannot be met
  2. C. If P is satisfied, A cannot be satisfied
    Data synchronization (C) takes time. Partition fault tolerance (P) makes the number of machines large. If both are met at the same time, it cannot respond in normal time, and availability (A) cannot be met
  3. A. If P is satisfied, C cannot be satisfied
    The service availability (A) meets the normal response time, and the partition fault tolerance (P) makes the number of machines large, so the data cannot be synchronized to all nodes in time, and the consistency (C) is not satisfied

3.1 selection of Registration Center

  1. Zoomeeper: the CP design ensures consistency and partition fault tolerance. When a cluster is built, if a node fails, an election will be conducted to select a new leader, and no external services will be provided during the election process; If more than half of the nodes in the cluster are unavailable, they cannot provide external services.
  2. eureka: in AP design, there are no master-slave nodes. When one node hangs up, it will automatically switch to other nodes and decentralize.

3.2 conclusion

In the distributed system, P must be satisfied. You can only choose one of C and A:
The best choice is to select the architecture according to the business scenario:
If consistency is required, select zookeeper, such as bank
If availability is required, select eureka, such as e-commerce

4. Introduction to Eureka, the core component of spring cloud microservice, and the impact after source closure

reference resources: https://blog.csdn.net/zjcjava/article/details/78608892

5. Eureka Server setup for service registration and discovery (Eureka Server setup)

Use IDEA to build the Server side of Eureka service center and start it
Official documents: https://cloud.spring.io/spring-cloud-netflix/reference/html/

  1. Create project eureka_server:

  2. Add annotation on startup class: @ EnableEurekaServer

  3. Add configuration in application.yml (also known as application.properties):

  port: 8761

    hostname: localhost
    #Declare yourself a server
    registerWithEureka: false
    fetchRegistry: false
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  1. start-up

  2. Address bar entry http://localhost:8761/ To access the registry page:

6. Eureka Client build commodity service for service registration and discovery (Eureka Client build)

  1. New project: product_service

  2. Write a simple commodity module

    Product class:

package net.xdclass.product_service.domain;

import java.io.Serializable;

 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 19:36 2021/10/21
public class Product implements Serializable {

    private int id;
    private String name;
    private int price;
    private int store;

    public Product() {

    public Product(int id, String name, int price, int store) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.store = store;

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public int getPrice() {
        return price;

    public void setPrice(int price) {
        this.price = price;

    public int getStore() {
        return store;

    public void setStore(int store) {
        this.store = store;

ProductService interface:

package net.xdclass.product_service.service;

import net.xdclass.product_service.domain.Product;

import java.util.List;

 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 19:40 2021/10/21
public interface ProductService {

    List<Product> listProduct();

    Product findById(int id);


ProductServiceImpl class:

package net.xdclass.product_service.service.impl;

import net.xdclass.product_service.domain.Product;
import net.xdclass.product_service.service.ProductService;
import org.springframework.stereotype.Service;

import java.util.*;

 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 19:45 2021/10/21
public class ProductServiceImpl implements ProductService {

    private static final Map<Integer, Product> daoMap = new HashMap<>();
    static {
        Product p1 = new Product(1, "Gin", 5613, 512);
        Product p2 = new Product(2, "Vodka", 4851, 453);
        Product p3 = new Product(3, "Sherry", 3244, 657);
        Product p4 = new Product(4, "Rum", 4156, 234);
        Product p5 = new Product(5, "Bourbon", 8934, 435);
        Product p6 = new Product(6, "Vermouth", 453, 6523);
        Product p7 = new Product(7, "Rye", 5343, 56);
        Product p8 = new Product(8, "tequila", 2353, 3245);

        daoMap.put(p1.getId(), p1);
        daoMap.put(p2.getId(), p2);
        daoMap.put(p3.getId(), p3);
        daoMap.put(p4.getId(), p4);
        daoMap.put(p5.getId(), p5);
        daoMap.put(p6.getId(), p6);
        daoMap.put(p7.getId(), p7);
        daoMap.put(p8.getId(), p8);

    public List<Product> listProduct() {
        Collection<Product> products = daoMap.values();
        List<Product> list = new ArrayList<>(products);
        return list;

    public Product findById(int id) {
        return daoMap.get(id);

ProductController class:

package net.xdclass.product_service.controller;

import net.xdclass.product_service.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 19:34 2021/10/21
public class ProductController {

    private ProductService productService;

     * Query all product information
     * @return Product list
    public Object list(){
        return productService.listProduct();

     * Query commodity information according to id
     * @param id Commodity id
     * @return Commodity information
    public Object findById(@RequestParam("id") int id){
        return productService.findById(id);


  1. Start product_service to test whether the above two functions are executable:

4. Add configuration in application.yml (also known as application.properties):

  port: 8771

#Specify registry address
      defaultZone: http://localhost:8761/eureka/

#Specifies the name of the service
    name: product_service

  1. Start eureka_server, start product_service, access http://localhost:8761/ :

  2. Start another product_service:

    Start product again_ Service, access http://localhost:8761/

7. Eureka service registry configuration console problem handling

Question 1: a string of scarlet letters

The self-protection mode cannot be turned off. It is turned on by default.

Question 2: why can you register by adding only one registration center address

Tags: Microservices

Posted on Thu, 21 Oct 2021 22:19:25 -0400 by jemgames