接着上次讲的springcloud服务提供者的实现

下面我们看一下eureka的自我保护功能

首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他节点的正常运行。

默认情况下,如果Eureka Server在一定时间内(默认90秒)没有接收到某个微服务实例的心跳,Eureka Server将会移除该实例。但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,而微服务本身是正常运行的,此时不应该移除这个微服务,所以引入了自我保护机制

注册中心的pom.xml

<?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>microservicecloud</artifactId><groupId>com.atguigu.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>microservicecloud-eureka-7001</artifactId><dependencies><!--eureka-server服务端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><!-- 修改后立即生效,热部署 --><!--    <dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId><optional>true</optional><scope>true</scope></dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>true</scope></dependency></dependencies><build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin><!-- 在这里添加热部署springloader plugin--><!-- <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><dependencies><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency></dependencies><executions><execution><goals><goal>repackage</goal></goals><configuration><classifier>exec</classifier></configuration></execution></executions></plugin>--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><!--fork :  如果没有该项配置,可能这个devtools不会起作用,即应用不会restart--><fork>true</fork></configuration></plugin></plugins></build>
</project>

注册中心的启动类代码:

package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001_App
{public static void main(String[] args){SpringApplication.run(EurekaServer7001_App.class, args);}
}

注册中心的application.yml配置文件:

server:port: 7001spring:application:name: microservicecloud-eureka#热部署devtools:restart:enabled: truetrigger-file: devtools.tgeureka:instance:hostname: localhost            #eureka服务端的实例名称client:register-with-eureka: false    #false表示不向注册中心注册自己fetch-registry: false          #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务service-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)

服务提供者的pom.xml

<?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>microservicecloud</artifactId><groupId>com.atguigu.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>microservicecloud-provider-dept-8001</artifactId><dependencies><!-- 引入自己定义的api通用包,可以使用Dept部门Entity --><dependency><groupId>com.atguigu.springcloud</groupId><artifactId>microservicecloud-api</artifactId><version>${project.version}</version></dependency><!-- actuator监控信息完善 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 将微服务provider侧注册进eureka --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- 修改后立即生效,热部署 --><!--<dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId><optional>true</optional><scope>true</scope></dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>true</scope></dependency></dependencies><build><plugins><!--<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><dependencies><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency></dependencies><executions><execution><goals><goal>repackage</goal></goals><configuration><classifier>exec</classifier></configuration></execution></executions></plugin>
--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><!--fork :  如果没有该项配置,可能这个devtools不会起作用,即应用不会restart--><fork>true</fork></configuration></plugin></plugins></build></project>

服务提供者的启动类代码:

package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
//@EnableDiscoveryClient //服务发现
public class DeptProvider8001_App {public static void main(String[] args) {SpringApplication.run(DeptProvider8001_App.class, args);}
}

服务提供者的application.yml配置文件

server:port: 8001mybatis:config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包mapper-locations:- classpath:mybatis/mapper/**/*.xml                       # mapper映射文件spring:application:name: microservicecloud-dept#热部署devtools:restart:enabled: truetrigger-file: devtools.tgdatasource:type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包url: jdbc:mysql://localhost:3306/cloudDB01              # 数据库名称username: rootpassword: rootdbcp2:min-idle: 5                                           # 数据库连接池的最小维持连接数initial-size: 5                                       # 初始化连接数max-total: 5                                          # 最大连接数max-wait-millis: 200                                  # 等待连接获取的最大超时时间#以下为新增druid:# 指明是否在从池中取出连接前进行检验,如果检验失败, 则从池中去除连接并尝试取出另一个,#注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串test-on-borrow: false# 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.#注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串test-while-idle: true# 指明是否在归还到池中前进行检验,注意: 设置为true后如果要生效,#validationQuery参数必须设置为非空字符串test-on-return: false# SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.#如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录validation-query: select 1eureka:client: #客户端注册进eureka服务列表内service-url: defaultZone: http://localhost:7001/eureka#defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/instance:instance-id: microservicecloud-dept8001prefer-ip-address: true     #访问路径可以显示IP地址     info: app.name: atguigu-microservicecloudcompany.name: www.atguigu.combuild.artifactId: $project.artifactId$build.version: $project.version$

两个工程的测试代码已经完成下面我们来测试下eureka的自我保护功能

分别启动注册中心和服务提供者两个服务如下图所示:

注册的服务名称如下图yml配置:

eureka客户端也就是服务的提供者yml配置文件配置服务以及服务状态配置:

服务已经启动我们访问注册中心:http://localhost:7001/

接下来我们下个下服务的实例名称

再次访问:http://localhost:7001/

如下图可知微服务立马改变

再次改变yml配置微服务名称

再次访问:http://localhost:7001/

总结一句话:某时候某一个微服务不可用了,eureka不会立刻清理,依旧会对该微服务的信息进行保存

自我保护模式正是一种针对网络异常波动的安全保护措施,使用自我保护模式能使Eureka集群更加的健壮、稳定的运行。

自我保护机制的工作机制是如果在15分钟内超过85%的客户端节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,Eureka Server自动进入自我保护机制,此时会出现以下几种情况:

1、Eureka Server不再从注册列表中移除因为长时间没收到心跳而应该过期的服务。
2、Eureka Server仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上,保证当前节点依然可用。
3、当网络稳定时,当前Eureka Server新的注册信息会被同步到其它节点中。

因此Eureka Server可以很好的应对因网络故障导致部分节点失联的情况,而不会像ZK那样如果有一半不可用的情况会导致整个集群不可用而变成瘫痪。

自我保护开关

Eureka自我保护机制,通过配置eureka.server.enable-self-preservation来true打开/false禁用自我保护机制,默认打开状态,建议生产环境打开此配置。

开发环境配置

开发环境中如果要实现服务失效能自动移除,只需要修改注册中心application.yml文件以下配置。

1、 注册中心关闭自我保护机制,修改检查失效服务的时间。

eureka:server: enable-self-preservation: falseeviction-interval-timer-in-ms: 3000

2、 微服务修改减短服务心跳的时间。

# 默认90秒
lease-expiration-duration-in-seconds: 10
# 默认30秒
lease-renewal-interval-in-seconds: 3

eureka自我保护功能相关推荐

  1. eureka自我保护时间_Spring Cloud Eureka 自我保护机制

    自我保护出现 首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他节点的正常运行. 默认情况下,如果Eureka Serve ...

  2. Spring Cloud Eureka 自我保护机制

    Spring Cloud Eureka 自我保护机制 Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这 ...

  3. eureka自我保护时间_SpringCloud Eureka自我保护机制

    自我保护背景 首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他节点的正常运行. 默认情况下,如果Eureka Serve ...

  4. SpringCloud Eureka自我保护机制

    转载自 SpringCloud Eureka自我保护机制 自我保护背景 首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他 ...

  5. 《SpringCloud超级入门》Eureka自我保护模式和InstanceID的配置《十四》

    关闭自我保护 保护模式主要在一组客户端和 Eureka Server 之间存在网络分区场景时使用.一旦进入保护模式,Eureka Server 将会尝试保护其服务的注册表中的信息,不再删除服务注册表中 ...

  6. SpringCloud Eureka自我保护机制介绍及配置

    概述:谈到Eureka的自我保护机制时,我们需要知道其中一些客户端和服务端的概念.比如客户端的心跳发送时间间隔.服务续约时间:服务端的服务剔除时间间隔.阈值更新时间间隔. 客户端心跳发送时间间隔(eu ...

  7. Eureka自我保护机制

    本文来说下Eureka自我保护机制 文章目录 为什么要有自我保护机制 重要变量 变量更新 Eureka-Server初始化 cancle主动下线 客户端注册 定时器 自我保护机制 开启 解除 本文小结 ...

  8. eureka自我保护时间_Eureka自我保护机制

    自我保护背景 首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他节点的正常运行. 默认情况下,如果Eureka Serve ...

  9. eureka自我保护时间_Eureka的自我保护机制

    前言 首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他节点的正常运行. 默认情况下,如果Eureka Server在一定 ...

最新文章

  1. 【Netty】从 BIO、NIO 聊到 Netty
  2. mysql数据集_Mysql 数据库-我的测试环境
  3. centos7 更新firefox版本
  4. 数据库数据满足树结构时,求一个结点的子结点有哪些
  5. 什么情况下应不建或少建索引
  6. MyBatis动态SQL小结
  7. 【C/C++】C++重复率最高、最经典面试题/笔试题(程序题篇)【持续更新】
  8. 漫画:如何实现大整数相乘?
  9. 单片机sprintf函数的用法_C++小知识之sprintf用法
  10. 拓扑排序算法(1.0版)
  11. 如何裁剪动图的边框?教你一键在线裁剪动图
  12. ESP8266的FATAL EXCEPTION 28 29问题原因
  13. 泰国服务器怎么挑选?
  14. [NCTF2019]SQLi 1regexp注入
  15. 【Ryo】不定期更新的藏宝阁——发现GitHub上的宝贝
  16. 一些风力发电机组工作参数的安全运行范围
  17. SDUT 操作系统课程 CATS考试工具部分专题代码实现
  18. IEC61499标准背后的逻辑
  19. 【Scratch案例实操】scratch星际迷航 scratch编程案例教学 少儿编程教案
  20. docker搭建tensorflow环境

热门文章

  1. windows rt c语言,有arm架构的windows平板吗??与x86的win有什么不同?arm写c语言怎样?
  2. java struts1_struts1.x
  3. c++ string 字符_C/C++知识分享:C++标准库之 string 类型,各种运算全部掌握
  4. 测试管理 | 基于风险的测试
  5. 【Java】使用前准备工作配置环境变量
  6. 你值得掌握的 Git分支等 常用命令 (持续更新中)
  7. VMware workstation 15.5.2及镜像文件下载
  8. 【js】数组置空的其他方式及使用场景
  9. 谈谈写程序与学英语(转载)
  10. 机器学习笔记——深度学习入门篇