F version SpringCloud 5-Eureka clusters and self-protection mechanisms

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

Foreword

The last article, the code built by Eureka registry and the client is a simple application of Eureka, in this article will explain more about Eureka server-side applications of the principles.

Eureka self-protection mechanism

Sign into the self-protection mechanism [focus]

In the process of using Eureka, sometimes you see the following content on the service side of the page Eureka:

On the monitoring page will see a line of red children, these children red What does it mean? Eureka registered on behalf of the center into the self-protection mechanism.

What is a self-protection mechanism [focus]

By default, if Eureka Server is not received within a certain time heartbeat service instance, Eureka will cancel the instance (default 90 seconds). But when the network partition fails, the micro clients and Eureka Server service can not communicate properly. Above behavior may become particularly dangerous, because the micro service itself is healthy, this time can not cancel the service instance.

Eureka to solve this problem through self-protection mechanism, when Eureka Server excessive loss of service instances in a short time (network partition failure may have occurred), then the Eureka Server into the self-protection mode, Once in this mode, Eureka Server will protect service registry information, not remove the service registry data (that is no longer written off any service instance), when the network fault recovery, Eureka Server will automatically exit the self-protection mode.

In summary, the self-protection mode is a safety measure to deal with network failure , its architecture is rather philosophy while retaining all of the micro-services, nor blindly write off any micro health services, the use of self-protection mode lets Eureka, more robust ,stable.

Bottom line: a large area when a client of the missing, Eureka registry into the self-protection mode, do not write off any instance

Configure self-protection mechanisms

In Eureka Server self-protection mechanisms in the closed configuration

#关闭自我保护机制  默认开启
eureka.server.enable-self-preservation=false
复制代码

如果想及时剔除失效的eureka服务除了关闭自我保护机制外,可以调低eureka的心跳值

eureka-server服务端
配置文件中我们添加如下配置

#关闭保护机制,以确保注册中心将不可用的实例正确剔除
eureka.server.enable-self-preservation=false
#(代表是5秒,单位是毫秒,清理失效服务的间隔 )
eureka.server.eviction-interval-timer-in-ms=5000

复制代码
客户端
配置文件中我们添加如下配置

# 心跳检测检测与续约时间
# 测试时将值设置设置小些,保证服务关闭后注册中心能及时踢出服务
# 配置说明
#  lease-renewal-interval-in-seconds 每间隔10s,向服务端发送一次心跳,证明自己依然”存活“
#  lease-expiration-duration-in-seconds 告诉服务端,如果我20s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=20
复制代码

注册中心高可用「注册中心集群」

注册中心集群 防止单点故障

Eureka可以通过运行多个实例并互相注册实现高可用部署,Eureka Server实例会彼此同步信息。

创建并配置Eureka集群

Tips:代码在前上篇教程的基础上开发,源码地址:https://gitee.com/bingqilinpeishenme/Java-Tutorials

1.创建第二个Eureka服务端 eureka-server-8800

2.修改pom文件

3.创建并修改启动类

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);
}
}

复制代码

4.写配置文件

5.修改注册中心eureka-server-8801的配置文件

注册中心之间的基本逻辑是:互相注册

此时我们就拥有了一个Eureka注册中心的集群

6.修改所有客户端的配置,客户端需要向 注册中心集群注册 所以需要配置所有注册中心的地址

演示注册中心集群的效果

1.启动所有的注册中心和客户端

2.查看Eureka注册中心管理页面

查看注册中心 8800 的管理页面,可以看到客户端可以正常注册

查看注册中心 8801 的管理页面,可以看到客户端也可以正常注册

3.关闭一个注册中心8800,注册中心8801不受任何影响,整个微服务的集群也不受任何影响,客户端可以正常注册

总结

以上就是Eureka注册中心高可用和自我保护机制的相关内容。

源码地址:https://gitee.com/bingqilinpeishenme/Java-Tutorials

恭喜你完成了本章的学习,为你鼓掌!如果本文对你有帮助,请帮忙点赞,评论,转发,这对作者很重要,谢谢。

要掌握SpringCloud更多的用法,请持续关注本系列教程。

求关注,求点赞,求转发

欢迎关注本人公众号:鹿老师的Java笔记,将在长期更新Java技术图文教程和视频教程,Java学习经验,Java面试经验以及Java实战开发经验。

Guess you like

Origin juejin.im/post/5e81eafc6fb9a03c4857630d