SpringCloud framework construction (2) Eureka service governance system

Service governance is the most core and basic module in the microservice architecture. It is mainly used to realize the automatic registration and discovery of each microservice instance.

The system is as follows:

Spirng Cloud Eureka uses Netflix Eureka to implement service registration and discovery. It contains both server-side components and client-side components, and both the server and client are written in Java, so Eureka is mainly suitable for distributed systems implemented through Java, or systems built with JVM-compatible languages. Eureka's server provides a relatively complete REST API, so Eureka also supports the inclusion of services implemented in non-java language into the Eureka service governance system. Other language platforms only need to implement Eureka's client programs themselves.

Eureka contains two components: Eureka Server and Eureka Client:
1. Eureka Client is a Java client used to simplify interaction with Eureka Server;
2. Eureka Server provides service discovery capabilities. When each microservice starts, it will register its own information (such as network information) with Eureka Server through Eureka Client, and Eureka Server will store the service information; 3
. After the microservice is started, it will periodically send heartbeats to Eureka Server (default period is 30 seconds) to renew its own information. If Eureka Server does not receive the heartbeat of a microservice node within a certain period of time, Eureka Server will log out of the microservice node (90 seconds by default);
4. Each Eureka Server is also an Eureka Client, and the synchronization of the service registry is completed between multiple Eureka Servers through replication;
5. Eureka Client caches information in Eureka Server. Even if all Eureka Server nodes are down, service consumers can still use the information in the cache to find the service provider.

1: Service registration center

1.1 Create a maven project of eureka-server and introduce the dependency of spring-cloud-starter-netflix-eureka-server

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.cjc.spring.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cjc.spring.cloud</groupId>
    <artifactId>eureka-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

 

1.2 Add configuration to application.yml

server:
  port: 8081
spring:
  application:
    name: eureka-service
eureka:
  client:
    register-with-eureka: false #false means not to register yourself with the registration center.
    fetch-registry: false #false means that the own end is the registration center. My responsibility is to maintain the service instance and do not need to retrieve the service.
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
    healthcheck:
      enabled: true

 

register-with-eureka indicates whether to register this service with the registration center, and fetch-registry indicates whether to obtain the service list from the service registration center. defaultZone refers to the service registration center address. The microservice name is the value of spring.application.name.

1.3 Start a service registration center

 You only need to add an annotation @EnableEurekaServer to the startup application class of the springboot project, and access http://localhost:8081/ after starting the service .

In the future, this eureka service will be used for registration and discovery of microservices.

1.4 Eurake self-protection mechanism

If a microservice becomes unavailable at a certain moment, Eurake will not clean it up immediately, but will still save the information of the microservice. By default, if EurakeServer does not receive the heartbeat of a service instance within a certain period of time, EurakeServer will The instance will be logged out (90 seconds by default). However, when a network partition failure occurs, the microservice and EurakeServer cannot communicate normally, and the above behavior may become very dangerous, because the microservice itself is healthy and the service should not be logged off at this time. Eurake solves this problem through "self-protection". When the EurakeServer node loses too many customers in a short period of time (a network partition failure may occur), then this node will enter self-protection mode. Once entering this mode, EurakeServer will protect the information in the service registry and will no longer delete the data in the service registry (that is, it will not log out any microservices). When the fault is restored, the EurakeServer node will automatically exit the self-protection mode.

1.5   Eurake cluster configuration

The application.yml of the Eurake server changes the defaultZone to defaultZone: http://localhost:8081/eureka/, http://localhost:8001/eureka/ or defaultZone: http://localhost:8001/eureka/. That is, this registration center is registered with other registration centers for mutual registration and discovery of microservices.

2: Service provider

2.1 Create a user-action mave project, referencing spring-cloud-starter-netflix-eureka-client

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

1.2 Add configuration to application.yml

server:
  port: 8082
spring:
  application:
    name: eureka-user

eureka:
  client:
    register-with-eureka: true #false means not to register yourself with the registration center.
    fetch-registry: true #false means that the own end is the registration center. My responsibility is to maintain the service instance and do not need to retrieve the service
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/

1.3 Start a service provider

 You only need to add an annotation @EnableEurekaClient to the startup application class of the springboot project. After starting the service, visit http://localhost:8081/ and you will find that there is an additional service.

1.3 Service synchronization

Since multiple service registration centers register each other as services, when a service provider sends a registration request to a service registration center, it will forward the request to other registration centers connected in the cluster, thereby achieving service synchronization between service registration centers. . Through service synchronization, the provider's service information can be obtained through any service registration center in the cluster.

1.4 Service renewal

After registering the service, the service provider will maintain a heartbeat to continue high-speed Eureka Server, "I am still continuing to provide services", otherwise the elimination task of Eureka Server will exclude the service instance from the service list. We call this a service renewal.

The following are two important attributes for service renewal:

(1)eureka.instance.lease-expiration-duration-in-seconds

leaseExpirationDurationInSeconds, which indicates the timeout period for the eureka server to wait for the next heartbeat after receiving the last heartbeat from the client. If the next heartbeat is not received within this time, the instance will be removed.

The default is 90 seconds
. If this value is too large, the instance may no longer be alive when the traffic is forwarded.
If the value is set too small, the instance is likely to be removed due to temporary network jitter.
This value should be at least greater than leaseRenewalIntervalInSeconds
(2) eureka.instance.lease-renewal-interval-in-seconds

leaseRenewalIntervalInSeconds, indicates the frequency of eureka client sending heartbeats to the server. If the server does not receive the client's heartbeat after leaseExpirationDurationInSeconds, the instance will be removed. In addition, if the instance implements HealthCheckCallback and decides to make itself unavailable, the instance will not receive traffic. The default is 30 seconds.

 

Source code download: https://github.com/cxsummer/springcloud

Guess you like

Origin blog.csdn.net/cjc000/article/details/91348347