当前位置 博文首页 > 蜗牛为梦想而生H:03-注册中心之Eureka

    蜗牛为梦想而生H:03-注册中心之Eureka

    作者:[db:作者] 时间:2021-09-07 19:17

    1. 什么是eureka

    Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务, Eureka包含两个组件:Eureka Server和Eureka Client。 ??

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

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

    Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。
    ?

    ?

    2. eureka原理图

    3. Eureka的基本使用

    3.1. 搭建eurekaServer

    ?

    ?3.2. 添加依赖

     ? <dependencies>
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
     ? ? ? ? ? ?<artifactId>spring-boot-starter-web</artifactId>
     ? ? ? ?</dependency>
     ? ? ? ?<!--eureka注册中心服务-->
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>org.springframework.cloud</groupId>
     ? ? ? ? ? ?<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
     ? ? ? ?</dependency>
     ? ?</dependencies>

    3.3. 编写主配置类

    ?3.4. 修改端口

    server:
      port: 10000

    3.5. 启动报错

    ?

    3.6. 错误描述

    1:问题描述
    ??? ?由于eurekaServer的依赖 其中依赖了一个eurekaclient 所以eurekaServer是个服务的同时又是客户端 所以启?? ?动时会去注册中心注册,因为是同一个工程,此时项目还没有启动 没有注册中心所以报错
    ? ?
    ? ?问题解决
    ? ?? ? ? 注册中心的服务 不往注册中心注册?
    ? ?? ? ? ?? ?eureka:
    ? ??? ??? ??? ?client:
    ? ? ??? ??? ??? ?register-with-eureka:false
    ? ? ??? ??? ??? ??? ??
    ? ? ??? ??? ??? ??? ??
    ?----------------------------------------------------------------------------------------------- ? ?
    2: 问题描述
    ?? ?由于eurekaServer的依赖 其中依赖了一个eurekaclient 所以eurekaServer是个服务的同时又是客户端 所以启?? ?动时会去注册中心拉去列表,因为是同一个工程,此时项目还没有启动 没有注册中心所以报错
    ? ? ??
    ? ? 问题解决:
    ? ? ? 注册中心的服务,不去注册中心拉去列表
    ? ? ? ?? ?eureka:
    ? ?? ??? ??? ?client:
    ? ? ? ?? ??? ??? ?fetch-registry:false

    ?

    3.7. 访问测试

    ?3.8. 修改提供者

    • 添加依赖

       <dependency>
       ? ?<groupId>org.springframework.cloud</groupId>
       ? ?<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>
    • 添加注解

    • 添加配置

    server:
      port: 8080
    ## 方式一 (建议使用)
    spring:
      application:
     ?  name: cloud-provider
    ?
    ## 方式二
    #eureka:
    #  instance:
    # ?  appname: cloud-provider
    ?
    eureka:
      client:
     ?  service-url:
     ? ?  defaultZone: http://localhost:10000/eureka
      instance:
     ?  prefer-ip-address: true
     ?  instance-id: ${spring.application.name}:${server.port}
    

    3.9. 修改消费者

    • 添加依赖
         <dependency>
     ? ? ? ? ? ?<groupId>org.springframework.cloud</groupId>
     ? ? ? ? ? ?<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     ? ? ? ?</dependency>
    • 添加注解

    • 添加配置

    server:
      port: 8081
    spring:
      application:
     ?  name: cloud-consumer
    eureka:
      client:
     ?  service-url:
     ? ?  defaultZone: http://localhost:10000/eureka
      instance:
     ?  instance-id: ${spring.application.name}:${server.port}
     ?  prefer-ip-address: true
    
    • 启动

    3.10. 刷新注册中心

    ?

    3.11. 问题说明

    修改完工程之后,什么代码都不修改 直接运行项目 重新请求时 会发现返回的xml??

    问题解决

       		<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                <exclusions>
                    <exclusion>
                        <artifactId>jackson-dataformat-xml</artifactId>
                        <groupId>com.fasterxml.jackson.dataformat</groupId>
                    </exclusion>
                </exclusions>
            </dependency>

    3.12. 使用服务发现远程调用

    3.13. 测试

    4. eureka集群

    由于消费者是从注册中心拿提供者的信息,如果注册中心挂了则就拿不到信息,此时我们需要一个eureka集群

    4.1. 集群搭建

    4.1.1. 创建2个eurekaServer

    创建方式和第一个一样

    4.1.2. 修改server-10001

    • 添加依赖

       ? <dependencies>
       ? ? ? ?<dependency>
       ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
       ? ? ? ? ? ?<artifactId>spring-boot-starter-web</artifactId>
       ? ? ? ?</dependency>
       ? ? ? ?<!--eureka注册中心服务-->
       ? ? ? ?<dependency>
       ? ? ? ? ? ?<groupId>org.springframework.cloud</groupId>
       ? ? ? ? ? ?<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
       ? ? ? ?</dependency>
       ? ?</dependencies>
    • 编写主配置类

    • 编写配置文件

      server:
        port: 10001
      ?
      eureka:
        client:
       ?  register-with-eureka: false
       ?  fetch-registry: false
       ?  service-url:
       ? ?  defaultZone: http://localhost:10000/eureka/,http://localhost:10002/eureka/
        instance:
       ?  hostname: server-10001

    4.1.3. 修改server-10002

    • 添加依赖

       ? <dependencies>
       ? ? ? ?<dependency>
       ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
       ? ? ? ? ? ?<artifactId>spring-boot-starter-web</artifactId>
       ? ? ? ?</dependency>
       ? ? ? ?<!--eureka注册中心服务-->
       ? ? ? ?<dependency>
       ? ? ? ? ? ?<groupId>org.springframework.cloud</groupId>
       ? ? ? ? ? ?<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
       ? ? ? ?</dependency>
       ? ?</dependencies>
    • 编写主配置类

    ?

    ?

    • 编写配置文件

      server:
        port: 10002
      eureka:
        client:
       ?  fetch-registry: false
       ?  register-with-eureka: false
       ?  service-url:
       ? ?  defaultZone: http://localhost:10000/eureka/,http://localhost:10001/eureka/
        instance:
       ?  hostname: server-10002
      ?

    4.1.4. 修改server-10000

    server:
      port: 10000
    ?
    eureka:
      client:
     ?  register-with-eureka: false
     ?  fetch-registry: false
     ?  service-url:
     ? ?  defaultZone: http://localhost:10001/eureka/,http://localhost:10002/eureka/
      instance:
     ? ?  hostname: server-10000

    4.1.5. 启动测试

    4.2. 把提供者加入集群

    • 修改配置

    ?

    4.3. 把消费者加入集群

    • 修改配置

      ?

    4.4. 测试集群

    测试关掉一个或者2个eureka, 还能正常远程调用

    5. 了解的一些概念

    • 服务注册中心

    ?服务注册中心即EurekaServer提供服务注册和发现功能?

    • 服务提供者

    提供服务的应用 服务提供者要向EurekaServer注册服务;

    • 服务注册

    服务提供者在启动的时候 会检测配置属性中的eureka.client.register-eureka是否等于true 默认就是true ?如果等于true 会向EurekaServer发起一个请求 并且携带自己的数据 EurekaServer获得数据之后 存储到一个Map集合中

    • 服务续约

    注册成功之后 服务提供者会持续一个心跳(定时向注册中心发送请求) 证明我还活着 默认情况下是30秒 发送一次请求 ?维持一次心跳 ?如果90秒没有发送心跳 那么EurekaServer注册中心 将会从服务列表中剔除 ?
    ?
    相关属性:
    ? ? lease-renewal-interval-in-seconds: 30 ?//每隔30秒发送依次心跳
    ??? lease-expiration-duration-in-seconds: 90?

    • 失效剔除

    Eureka Server在启动完成后会创建一个定时器每隔60秒检查一次服务健康状况,如果其中一个服务节点超过90秒未检查到心跳,那么Eureka Server会自动从服务实例列表内将该服务剔除。(剔除不剔除还受自我保护的影响)
    ?
    由于非正常关闭不会执行主动下线动作,所以才会出现失效剔除机制,该机制主要是应对非正常关闭服务的情况,如:内存溢出、杀死进程、服务器宕机等非正常流程关闭服务节点时。
    ?
    可以通过eureka.server.eviction-interval-timer-in-ms参数对其进行修改,

    • 自我保护

    随便关闭一个微服务 就会出现自我保护?
    ?
    Eureka Server的自我保护机制会检查最近15分钟内所有Eureka Client正常心跳的占比,如果低于85%就会被触发。 我们如果在Eureka Server的管理界面发现如下的红色内容,就说明已经触发了自我保护机制。
    ? 当触发自我保护机制后Eureka Server就会锁定服务列表,不让服务列表内的服务过期,不过这样我们在访问服务时,得到的服务很有可能是已经失效的实例,如果是这样我们就会无法访问到期望的资源,会导致服务调用失败,所以这时我们就需要有对应的容错机制、熔断机制(后面的知识)。
    ??
    ???server:
    ??? ? ? enable-self-preservation:false 关闭自我保护

    • 消费者

    消费应用从注册中心获取服务列表 从而得知服务的信息 知道去哪里调用服务方?
    当服务消费者启动是,会检测eureka.client.fetch-registry参数的值,如果为true,则会从Eureka Server服务的列表只读备份,然后缓存在本地。并且每隔30秒会重新获取并更新数据。

    我们可以通过下面的参数来修改:
     ? registry-fetch-interval-seconds: 30
    
    

    6. 配置大比拼

    1:spring.application.name
    ? ? 配置服务名字?
    2:eureka.client.register-with-eureka?
    ? ? 启动时 是否像注册中心注册自己 默认为true所以一般在EurekaServer使用 生产者消费者不能使用 否则注册不 ???到注册中心
    ?
    3:Eureka.client.fetch-registry?
    ? ? 表示自己之充当注册中心一般搭配eureka.client.register-with-eureka在server端使用
    ?
    4:Eureka.client.service-url.defaultZone?
    ? ? 注册中心表示对外暴露的服务 ?生产者消费者表示 连接注册中心的服务链接?
    ?
    5:eureka.instance.instance_id?
    ? ? ?表示服务实例的id ?
    ?
    6:eureka.instance.prefer-ip-address?
    ??? ? 表示显示ip 不显示localhost
    ?
    7:eureka.instance.lease-expiration-duration-in-seconds?
    ? ? 表示超过多少秒没有发送心跳 将会从列表中移除 默认90?
    ?
    8:eureka.instance.lease-renew-internal-in-seconds?
    ? ? 表示提供者发送心跳间隔时间
    ?
    9:eureka.client.registry-fetch-interval-seconds?
    ? ? 表示消费者获取服务列表的时间间隔?
    ?
    10:eureka.server.enable-self-preservation?
    ? ? 表示是否关闭自我保护模式 默认true?

    ?

    ?

    ?

    ?

    ?

    cs