SpringCloud05-Eureka服务注册与发现

1、Eureka基础知识

1.1 什么是服务治理

Spring Cloud封装了Netflix公司开发的 Eureka 模块来实现服务治理;

在传统的RPC远程过程调用框架中,管理每个服务与服务之间的依赖关系比较复杂,所以需要使用服务治理。管理服务之间的依赖关系,实现服务的调用,负载均衡和容错等,实现服务发现与注册。

1.2 什么是服务注册与发现

Eureka 采用了 C/S 的设计架构, Eureka Server 作为服务注册功能的服务器,它是服务注册中心,而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server 并维持心跳连接。这样系统的维护人员就可以通过Eureka Server 来监控系统中各个微服务是否正常运行。

在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息 比如:服务地址、通讯地址等以别名方式注册到中心上。另一方(消费者|服务提供者)就以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用。RPC远程调用框架的核心设计思想:在于注册中心,因为注册中心管理每个服务之间的一个依赖关系(服务治理概念)。再任何RPC远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))

在这里插入图片描述

1.3 Eureka的两组件

Eureka包含两组件:Eureka Server 和 Eureka Client

1.3.1 EurekaServer

Eureka Server 提供服务注册服务。各个微服务节点通过配置启动后,会在 Eureka Server中进行注册;这样Eureka Server中的注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。

1.3.2 Eureka Client

Eureka Client 通过注册中心进行访问其他服务,是一个Java客户端;用于简化 Eureka Server 的交互。客户端同时也具备一个内置的,使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server 发送心跳(默认周期30秒)。如果 Eureka Server 在多个心跳周期内,没有接收到某个节点的心跳, Eureka Server 将会从服务注册表中把这个服务节点移除(默认90秒)。

2、单机Eureka构建

2.1 创建服务注册中心模块:cloud-eureka-server7001

2.1.1 创建Module

跟之前的创建订单和支付模块一样的方式创建一个Module,名称为 cloud-eureka-server7001 。

在这里插入图片描述

2.1.2 改pom

<?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>springcloud2020</artifactId>
        <groupId>com.zdw.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7001</artifactId>
    <description>eureka服务注册中心的服务端</description>

    <dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zdw.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般为通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

在以前,Eureka的依赖是没有区分客户端和服务端的。

 <!--eureka-server以前的坐标-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

2.1.3 写yml

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

2.1.4 主启动

package com.zdw.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer  //表示这是一个Eureka Server 端
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

注意:@EnableEurekaServer : 表示这是一个Eureka Server 端,这个不能忘记

2.1.5 测试

启动eureka7001,浏览器访问:http://localhost:7001/

在这里插入图片描述

No application available没有服务被发现 因为没有注册服务进来当前不可能有服务被发现。

2.2 把payment8001注册到Eureka7001

2.2.1 改pom

修改cloud-provider-payment8001的pom文件,引入Eureka Client的依赖:

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

2.2.2 改yml

修改 cloud-provider-payment8001的application.yml文件,添加Eureka的配置:

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # 单击版
      defaultZone: http://localhost:7001/eureka

eureka 是顶格写的。

2.2.3 修改主启动类

给主启动类加上注解:@EnableEurekaClient 这是一个Eureka的client端

package com.zdw.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient  //这是一个Eureka的client端
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

2.2.4 测试

启动payment8001,浏览器访问 : http://localhost:7001/ ,查看cloud-provider-payment8001是否注册到了Eureka注册中心:

在这里插入图片描述

可以看到界面多了一个 CLOUD-PAYMENT-SERVICE(对应是application.yml中的spring.application.name=cloud-eureka-service) 应用,并且是UP状态,同时我们访问之前的:http://localhost:8001/payment/get/1 也可以访问到数据。

2.3 把order80注册到Eureka7001

2.3.1 改pom

修改cloud-consumer-order80服务的pom文件,添加eureka-client 依赖:

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

2.3.2 改yml

修改cloud-consumer-order80服务的application.yml文件,添加Eureka的配置:

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

2.3.3 修改主启动类

主启动类上添加注解:@EnableEurekaClient

package com.zdw.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient  //这是一个Eureka的client端
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}

2.3.4 测试

启动order80,浏览器访问:http://localhost:7001/

在这里插入图片描述

2.4 让order80通过Eureka访问payment8001

2.4.1 改OrderController的访问地址

上面,我们已经把order80和payment8001注册到了Eureka注册中心,但是此时,我们访问:http://localhost/consumer/payment/get/1 的时候,并不是走的注册中心,因为我们之前的OrderController中是这样的:

package com.zdw.springcloud.controller;

@RestController
@Slf4j
public class OrderController {

    //支付服务的地址
    public static final String PAYMENT_URL = "http://localhost:8001";
}

我们是写死了8001的地址:public static final String PAYMENT_URL = “http://localhost:8001”; 现在我们改成 通过payment8001注册到Eureka的别名:CLOUD-PAYMENT-SERVICE 来访问 payment8001.

@RestController
@Slf4j
public class OrderController {
    //支付服务的地址
    //public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
}

2.4.2 修改OrderConfig配置类

在配置类OrderConfig中,当注册RestTemplate的时候,在方法上添加注解:@LoadBalanced

import org.springframework.cloud.client.loadbalancer.LoadBalanced;

@Configuration
public class OrderConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

2.4.3 测试

浏览器访问:http://localhost/consumer/payment/get/1 可以正常访问到数据,说明我们是成功的

3、集群Eureka构建

3.1 集群Ereuka的原理

在这里插入图片描述

3.2 Eureka集群环境搭建

3.2.1 搭建Eureka7002

参照cloud-eureka-server7001创建一个新的Module:cloud-eureka-server7002,它和cloud-eureka-server7001的不同体现端口和yml的配置;

每个Eureka之间的关系是 :互相注册,相互守望。也就是自己要关联集群中的其他Eureka。

1、建module

在这里插入图片描述

2、改pom
<?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>springcloud2020</artifactId>
        <groupId>com.zdw.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7002</artifactId>
    <description>eureka服务注册中心的服务端</description>
    <dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zdw.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般为通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
3、写yml
server:
  port: 7002
spring:
  application:
    name: cloud-eureka-service
eureka:
  instance:
    # eureka服务端的实例名称 # 单机
    # hostname: localhost
    hostname: eureka7002.com
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
    fetch-registry: false
    service-url:
      # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址  # 单机
      # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 相互注册
      defaultZone: http://eureka7001.com:7001/eureka/

注意:hostname: eureka7002.com 这里不再是localhost,因为我们是一台物理机,上面要运行两个Eureka,所以需要修改 C:\Windows\System32\drivers\etc 目录下面的hosts文件:添加下面两行配置,把127.0.0.1映射到eureka7001.com和eureka7002.com

# SpringCloud
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com

所以,cloud-eureka-server7002的yml文件也要修改:

server:
  port: 7001
spring:
  application:
    name: cloud-eureka-service
eureka:
  instance:
    # eureka服务端的实例名称 # 单机
    # hostname: localhost
    hostname: eureka7001.com
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
    fetch-registry: false
    service-url:
      # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址  # 单机
      # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 相互注册
      defaultZone: http://eureka7002.com:7002/eureka/
4、主启动
package com.zdw.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7002 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7002.class,args);
    }
}

@EnableEurekaServer 记得加上这个注解

5、测试

启动cloud-eureka-server7001和cloud-eureka-server7002,浏览器分别访问:http://localhost:7001/和http://localhost:7002/:

在这里插入图片描述

在这里插入图片描述

这就达到了互相注册, 相互守望的效果

4、把payment8001和order80注册到上面的eureka集群

4.1 把payment8001注册到上面的eureka集群

修改cloud-provider-payment8001的yml文件的defaultZone的值:

server:
  port: 8001
spring:
  application:
    name: cloud-payment-service
  datasource:
    # 当前数据源操作类型
    type: com.alibaba.druid.pool.DruidDataSource
    # mysql驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloud2020?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # 单击版
      # defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001
    # 访问路径可以显示ip地址
    prefer-ip-address: true

mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.zdw.springcloud.entities

4.2 把order80注册到上面的eureka集群

修改cloud-consumer-order80的yml文件的defaultZone的值:

server:
  port: 80
spring:
  application:
    name: cloud-consumer-order

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: order80
    # 访问路径可以显示ip地址
    prefer-ip-address: true

4.3 测试

首先启动先要启动EurekaServer,7001/7002服务,然后启动cloud-provider-payment8001,最后启动 cloud-consumer-order80

浏览器访问:http://localhost:7001/

浏览器访问:http://localhost:7002/

浏览器访问:http://localhost:8001/payment/get/1

浏览器访问:http://localhost/consumer/payment/get/1

不出意外的话,应该都是正常的。

5、payment支付模块集群搭建

服务提供方一般都是集群配置的,所以我们可以参照 cloud-provider-payment8001 创建一个新模块:cloud-provider-payment8002,然后也把它注册到Eureka 注册中心,配置好负载均衡之后,order80就可以按照默认的轮询机制,访问8001和8002两个服务了。

5.1 创建cloud-provider-payment8002

cloud-provider-payment8002 的创建方式和cloud-provider-payment8001完全是一样的(包括业务代码,MyBatis的Mapper文件等),可以照抄,所以相关的代码和pom配置可以去 cloud-provider-payment8001 拷贝;我们只要修改yml配置文件和主启动类就差不多了。

在这里插入图片描述

5.2 改yml

server:
  port: 8002
spring:
  application:
    name: cloud-payment-service
  datasource:
    # 当前数据源操作类型
    type: com.alibaba.druid.pool.DruidDataSource
    # mysql驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloud2020?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # 单击版
      # defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8002
    # 访问路径可以显示ip地址
    prefer-ip-address: true

mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.zdw.springcloud.entities

主要是port 和 instance-id 不同

5.3 主启动

package com.zdw.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient  //这是一个Eureka的client端
public class PaymentMain8002 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8002.class,args);
    }
}

@EnableEurekaClient 注解不能少

6、负载均衡测试

上面我们已经创建了两个Eureka,两个支付模块:8001和8002,还有一个客户端订单模块order80,现在我们要测试Eureka的负载均衡。首先要注意的是,在order80 的配置类OrderConfig中,注册RestTemplate到IOC容器的时候,一定要加上注解:@LoadBalanced(默认的负载均衡算法是轮询)。

6.1 RestTemplate上面@LoadBalanced

这步操作我们之前已经加过了,不然的话之前的访问就会报错,这里只是为了强调,现在再来仔细的查看一下order80模块的OrderConfig类:

package com.zdw.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class OrderConfig {

    /**
     * 注册RestTemplate
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

6.2 启动测试

先要启动EurekaServer,7001/7002服务

再要启动服务提供者provider,8001/8002服务

最后启动order80 服务:

浏览器访问:http://eureka7001.com:7001/ http://eureka7002.com:7002/ 可以看到他们两已经互相注册了,同时其他的三个微服务8001,8002,80都注册了

浏览器访问:http://localhost:8001/payment/get/1 http://localhost:8002/payment/get/1 都能正常访问到数据,说明8001和8002的服务是正常的;

浏览器多次访问:http://localhost/consumer/payment/get/1 可以看到:

在这里插入图片描述

在这里插入图片描述

这两个是交替出现的,这也正是我们想要的结果,由8001和8002的集群环境一起为order80提供服务,默认的负载均衡机制就是轮询。

7、服务注册发现@EnableDiscoveryClient

对于注册到eureka里面的微服务,可以通过服务发现来获得该服务的信息;

要想暴露微服务的信息,主要有两步:

1、主启动类加上注解:@EnableDiscoveryClient

2、在Controller中,通过 DiscoveryClient 获取微服务信息,并返回给浏览器;

7.1 payment8001配置服务发现

7.1.1 PaymentMain8001主启动类添加注解@EnableDiscoveryClient

package com.zdw.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的client端
@EnableDiscoveryClient  //服务发现开启
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

7.1.2 添加一个Controller

package com.zdw.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class DiscoveryClientController {

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/discovery/info")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for(String element : services){
            System.out.println("******   "+element);
        }
        //通过服务别名获取服务的实例
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for(ServiceInstance instance : instances){
            System.out.println("服务主机:"+instance.getHost()+",服务端口:"+instance.getPort()+",服务ID"+instance.getServiceId()+",服务uri:"+instance.getUri());
        }
        return discoveryClient;
    }
}

7.2 payment8002和order80配置服务发现

参照payment8001的方式进行配置即可。

然后启动工程,浏览器查看效果:

http://localhost:8001/discovery/info

http://localhost:8002/discovery/info

http://localhost/discovery/info

在这里插入图片描述

8、Eureka的自我保护

8.1 什么是Eureka保护机制

首先我们看看Eureka的保护机制是什么,就可以访问 eureka7001.com:7001:

在这里插入图片描述

这一行红色的字(EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.)就是Eureka进入了保护模式。

保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server 将会尝试保护其服务注册表的信息,不会删除服务注册表中的数据,也就是不会注销任何微服务。

8.2 为什么出现Eureka的自我保护机制

为了防止Eureka Client可以正常运行,但是与Eureka Server网络不通的情况下,Eureka Server会立刻删除Eureka Client。

默认情况下,如果Eureka Server在一定时间内没有收到某个微服务实例的心跳,Eureka Server 就会将这个实例注销(默认是90秒)。但是当网络发生故障(延时,卡顿,拥挤)时,微服务和Eureka Server 无法正常通信,但是微服务本身是健康的,此时不应该注销该微服务。所以为了解决这个问题,Eureka就有一种自我保护机制,当Eureka Server在短时间内丢失过多客户端时,那么就会进入自我保护模式:

在这里插入图片描述

它的设计哲学是:宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务注册信息。

总之,保护模式是一种应对网络异常的安全保护措施。使用自我保护模式,可以让Eureka更加健壮、稳定。一句话总结就是:某时刻 一个微服务不可用了,Eureka不会立刻清理,依旧会对该服务的信息进行保存。属于CAP里面的AP分支

8.3 关闭Eureka的自我保护

出产默认,自我保护机制是开启的,eureka.server.enable-self-preservation=true,使用eureka.server.enable-self-preservation=false 可以禁用自我保护模式;Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒),Eureka服务端在收到最后一次心跳后等待时间上限 ,单位为秒(默认是90秒),超时剔除服务。

8.3.1 Eureka Server 7001/7002的修改

要关闭Eureka的自我保护,就要在7001和7002的yml配置中添加下面的配置:

server:
  port: 7001
spring:
  application:
    name: cloud-eureka-service
eureka:
  instance:
    # eureka服务端的实例名称 # 单机
    # hostname: localhost
    hostname: eureka7001.com
  server:
    # 禁用自我保护,保证不可用服务被及时删除
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
    fetch-registry: false
    service-url:
      # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址  # 单机
      # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 相互注册
      defaultZone: http://eureka7002.com:7002/eureka/

  server:
    # 禁用自我保护,保证不可用服务被及时删除
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

这些是新增的配置,7001和7002都加上这些配置

8.3.2 payment8001和8002,order80修改

在 payment8001和8002,order80的yml配置中添加下面的配置:

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # 单击版
      # defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001
    # 访问路径可以显示ip地址
    prefer-ip-address: true
    #Eureka客户端向服务端发送心跳的实际间隔,单位为秒(默认为30秒)
    lease-renewal-interval-in-seconds: 1
    #Eureka服务端收到最后一次心跳后等待时间上线,单位为秒(默认为90秒) 超时将剔除服务
    lease-expiration-duration-in-seconds: 2
    #Eureka客户端向服务端发送心跳的实际间隔,单位为秒(默认为30秒)
    lease-renewal-interval-in-seconds: 1
    #Eureka服务端收到最后一次心跳后等待时间上线,单位为秒(默认为90秒) 超时将剔除服务
    lease-expiration-duration-in-seconds: 2

这两个是新增的配置

分别启动上面的五个微服务,查看效果:

在这里插入图片描述

如果关闭8001和80,再次查看效果:发现关闭的服务立马就被Eureka Server给剔除了。

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值