Source address: https://gitee.com/bingqilinpeishenme/Java-Tutorials

preface

In the last article, the Eureka registry and client were built by code, which is a simple application of Eureka. In this article, we will explain more about the application and principle of Eureka server.

Eureka self-protection mechanism

Signs of entering self-protection mechanism

When using Eureka, you will sometimes see the following content on the Eureka server page:


You’ll see a line of red letters on the surveillance page. What do those red letters mean? This represents that the Eureka registry has entered into a self-protection mechanism.

What is the self-protection mechanism?

By default, if Eureka Server does not receive a heartbeat from a service instance within a certain period of time, Eureka will unregister the instance (90 seconds by default). However, when the network partition fails, the microservice client cannot communicate with the Eureka Server. This behavior can become particularly dangerous because the microservice itself is healthy and the service instance cannot be logged out at this point.

Eureka solves this problem through self-protection mechanism. When The Eureka Server loses too many service instances in a short period of time (network partition failure may occur), the Eureka Server enters self-protection mode. Once it enters this mode, The Eureka Server protects the information in the service registry and does not delete the data in the service registry (that is, does not deregister any service instances). After the network fault is rectified, the Eureka Server automatically exits the self-protection mode.

To sum up, the self-protection mode is a security protection measure to deal with network failures. Its architectural philosophy is that it would rather keep all micro-services at the same time, rather than blindly cancel any healthy micro-services. Using the self-protection mode, Eureka can be more robust and stable.

Bottom line: When a large number of clients lose contact, the Eureka registry goes into self-protection mode and does not unregister any instances

Configure the self-protection mechanism

Disable the self-protection mechanism in Eureka Server

# Disable Self-protection is enabled by default

eureka.server.enable-self-preservation=false

Copy the code

If you want to eliminate the failed Eureka service in time, in addition to closing the self-protection mechanism, you can lower the heartbeat value of Eureka

Eureka - server server

Add the following configuration to the configuration file



Turn off the protection mechanism to ensure that the registry properly culls unusable instances

eureka.server.enable-self-preservation=false

# (5 seconds, in milliseconds, to clean up failed services)

eureka.server.eviction-interval-timer-in-ms=5000



Copy the code
The client

Add the following configuration to the configuration file



Heartbeat detection detection and renewal time

When testing, set the value to a smaller value to ensure that the registry can be kicked out of the service after it is shut down

# Configuration description

# lease-renewal-interval-in-seconds Send a heartbeat to the server every 10 seconds to prove that the server is still alive.

# lease- expiration-durations-in-seconds tell the server that if I don't send you a heartbeat in 20 seconds, I'm "dead" and kick me out.

eureka.instance.lease-renewal-interval-in-seconds=10

eureka.instance.lease-expiration-duration-in-seconds=20

Copy the code

Registry High availability Registry Cluster

Registry clustering prevents single points of failure

Eureka can be deployed in high availability by running multiple instances and registering with each other. Eureka Server instances synchronize information with each other.

Create and configure the Eureka cluster

Tips: the code in the last tutorial before development, on the basis of source address: https://gitee.com/bingqilinpeishenme/Java-Tutorials

1. Create the second Eureka server eureka-server-8800


2. Modify the POM file


3. Create and modify the startup class

package com.lby;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;



/ * *

* @author luxiaoyang

* @create 2020-03-30-20:36

* /

@EnableEurekaServer

@SpringBootApplication

public class EurekaServer8800 {

public static void main(String[] args) {

SpringApplication.run(EurekaServer8800.class,args);

}

}



Copy the code

4. Write configuration files


5. Modify the configuration file of registry Eureka-server-8801

The basic logic between registries is to register each other


At this point we have a cluster of Eureka registries

6. Modify the configurations of all clients. The clients need to register with the registry cluster, so you need to configure the addresses of all registries


Demonstrate the effects of a registry cluster

1. Start all registries and clients

2. View Eureka Registry management page

On the registry 8800 management page, you can view that the client can be registered


View the management page of registry 8801, you can see that clients can also be registered


3. Close a registry 8800. The registry 8801 is not affected and the entire microservice cluster is not affected

conclusion

That’s all about Eureka registry high availability and self-protection mechanisms.

Source address: https://gitee.com/bingqilinpeishenme/Java-Tutorials

Congratulations on completing this chapter. A round of applause! If this article is helpful to you, please help to like, comment, retweet, this is very important to the author, thank you.


To learn more about the use of SpringCloud, stay tuned for this series of tutorials.

Ask for attention, ask for likes, ask for retweets

Welcome to pay attention to my public account: Teacher Lu’s Java notes, will update Java technology graphic tutorials and video tutorials in the long term, Java learning experience, Java interview experience and Java actual combat development experience.