三、服务注册与发现(Eureka)

一、Eureka基础知识

1.1、什么是服务治理

  Spring Cloud 封装了Netflix公司开发的Eureka模块来实现服务治理,在传统的rpc远程调用框架中,管理每个服务于服务之间依赖关系比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用负载均衡容错等,实现服务发现个注册。

1.2、什么是服务注册

  

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

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

1.3、Eureka两组件

  Eureka包含连个组件:Eureka Server 和 Eureka Client

    • Eureka Server:提供服务注册服务
      各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用于服务节点的信息,服务节点的信息可以在界面中直观看到。
    • Eureka Client:通过注册中心进行访问
      是一个Java客户端,用于简化eureka Server的交互,客户端同时也具备一个内置、使用轮询负责算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期是30s)。若果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server 将会从服务注册表中把这个服务节点移除(默认是90s)

二、搭建注册中心

  新建Spring boot的Module名称:cloud-eureka-server7001

  pom.xml添加以下依赖

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</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-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>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
  </dependency>
</dependencies> 

  编写配置文件application.yml

server:
  port: 7001

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

  主启动类加@EnableEurekaServer注解

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

  测试:http://localhost:7001/

三、注册服务

  通过修改名称为cloud-provider-payment8001的module将其注册到Eureka Server 作为服务提供者给cloud-provider-payment8001的pom.xml添加下面的依赖

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

  在application.xml中添加关于Eureka的配置

 eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机版(入住地址)
      defaultZone: http://localhost:7001/eureka
    instance:
      instance-id: payment8001
      prefer-ip-address: true     #访问路径可以显示IP地址
      #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
      #lease-renewal-interval-in-seconds: 1
      #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
      #lease-expiration-duration-in-seconds: 2

  主启动类添加@EnableEurekaClient注解

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

  测试:先要启动EurekaServer在启动cloud-provider-payment8001访问http://localhost:7001/

  微服务注册名配置说明

  按照同样的方法将cloud-consumer-order80服务也注册到Eureka注册中心。

三、集群Eureka搭建

3.1、集群原理说明

3.2、EurekaServer集群环境构建步骤

  参考cloud-eureka-server7001 新建cloud-eureka-server7002

  修改映射配置。找到C:\Windows\System32\drivers\etc路径下的hosts文件,修改映射配置添加进hosts文件

 127.0.0.1  eureka7001.com
 127.0.0.1  eureka7002.com

  下面是Eureka Server的application.yml

  • 7001
server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com    #eureka服务端的实例名字
  client:
    register-with-eureka: false    #表识不向注册中心注册自己
    fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
    service-url:
        #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7002.com:7002/eureka/    
  • 7002
server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com #eureka服务端的实例名字
  client:
    register-with-eureka: false    #表识不向注册中心注册自己
    fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
    service-url:
    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
    defaultZone: http://eureka7001.com:7001/eureka/     

3.3、向集群中注册服务

  将支付服务8001微服务发布到上面2台Eureka集群配置中

service-url:
     #集群版
  defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka 

3.4、测试

  • 先要启动EurekaServer,7001/7002服务

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

  • 看7001和7002上有没有注册上服务

四、actuator微服务信息完善

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版
  instance:
      主机名称:服务名称修改
    instance-id: payment8001
    #访问信息有ip信息提示 
    prefer-ip-address: true

五、服务发现Discovery

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

  修改cloud-provider-payment8001的Controller

@Resource
private DiscoveryClient discoveryClient;
 
@GetMapping(value = "/payment/discovery")
public Object discovery(){
    List<String> services = discoveryClient.getServices();
    for (String element : services) {
        log.info("***** element:"+element);
    }
    List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    for (ServiceInstance instance : instances) {
        log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
    }
    return this.discoveryClient;
}

  主启动类加@EnableDiscoveryClient

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

  测试,先要启动EurekaServer,7001/7002服务。再启动8001主启动类,需要稍等一会,访问,http://localhost:8001/payment/discovery

六、Eureka自我保护

6.1、故障现象

  概述:保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何微服务信息。简单说就是,某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存。属于CAP里面的AP分支。

  默认情况下,如果EurekaServer在一定时间内没有接受但某个服务实例的心跳,EurekaServer将会注销该实例(默认是90秒),但是当网络分区故障发生(延迟、卡顿、拥挤)时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险,因为服务本身其实时健康的,此时本不应该注销这个微服务。通过“自我保护模式”来解决这个问题,当EurekaSever节点在短时间内丢失过多客户端时(此时发生了网络故障),那么这个节点就会进入自我保护模式

6.2、导致原因

  为了防止EurekaClient可以正常运行,但是EurekaServer网络不通情况下,EurekaServer不会立刻将EurekaClient服务剔除。

6.3、怎么禁止自我保护

  一般生产环境中不会禁止自我保护

1、注册中心eureakeServer端7001

  出厂默认,自我保护机制是开启的eureka.server.enable-self-preservation = true,使用eureka.server.enable-self-preservation = false可以禁用自我保护模式,在eurekaServer端7001处设置关闭自我保护机制

eureka:
  server:
    enable-self-preservation: false    #关闭Eureka的自我保护机制
    eviction-interval-timer-in-ms: 2000 #单位毫秒

2、生产者客户端eureakeClient端8001

  默认(单位为秒)

  • eureka.instance.lease-renewal-interval-in-seconds=30

  • eureka.instance.lease-expiration-duration-in-seconds=90

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db1022?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      #单机版(入住地址)
      defaultZone: http://localhost:7001/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

  测试,7001和8001都配置完成,先启动7001再启动8001,先关闭8001(马上被删除了)

posted @ 2021-02-23 22:04  jingdy  阅读(494)  评论(0编辑  收藏  举报