Eureka

理论知识

1.基本概念

  • 原理图在这里插入图片描述
  • Eureka Server:注册中心服务端
对外提供三个功能:
1.服务注册:服务提供者启动时,向Eureka Server注册信息,Eureka Server存储该信息
	Eureka Server内部有两层缓存机制来维护整个注册表
2.提供注册表:服务消费者调用服务时,Eureka Client未缓存注册表,则会从Eureka Server获取最新的注册表
3.同步状态:Eureka Client通过注册、心跳机制和Eureka Server同步当前客户端的状态
	心跳机制:Eureka Client每隔30秒发送一次心跳来续约(服务续约),通过续约告知注册中心服务端该服务提供者是否正常运行,默认90秒未收到续约信号则会将该服务提供者从注册列表中删除(服务剔除)
		续约间隔配置:eureka.instance.lease-renewal-interval-in-seconds=30
		服务失效时间:eureka.instance.lease-expiration-duration-in-seconds=90
  • Eureka Client:注册中心客户端
Eureka Client会拉取、更新和缓存Eureka Server中的信息,当所有的Eureka Server节点都掉线,服务消费者依然可以通过缓存中信息找到服务提供者信息,但服务有变更时会出现信息不一致
  • Cancel: 服务下线
服务下线不会自动完成,手动调用DiscoveryManager.getInstance().shutdownComponent()
  • GetRegisty: 获取注册列表信息
默认情况下Eureka Client使用JSON格式获取注册列表的信息
	启用服务消费者从注册中心获取注册列表配置:eureka.client.fetch-registry=true
	设置服务消费者从注册中心拉取服务列表的间隔:eureka.client.registry-fetch-interval-seconds=30
  • Remote Call: 远程调用
服务消费者从注册中心获取到服务提供者信息后,可通过Http请求调用对应的服务
当服务提供者有多个时,服务消费者通过Ribbon自动进行负载均衡

2.自我保护机制

  • 出现
微服务架构内服务之间通常是跨进程调用,网络通信面临各种问题
Eureka Server90秒之内未收到续约则会注销该实例
而大量的实例注销严重威胁整个微服务架构的可用性,此时推出自我保护机制
  • 含义
Eureka Server在运行期间统计心跳失败比例在15分钟内是否低于85%,如果低于85%Eureka Server即会进入自我保护机制

当个别客户端出现心跳失联时,则认为是客户端的问题,剔除掉客户端
当Eureka捕获到大量的心跳失败时,则认为可能是网络问题,进入自我保护机制
当客户端心跳恢复时,Eureka会自动退出自我保护机制
  • Eureka Serve进入自我保护机制出现以下情况
1.Eureka不再从注册列表中移除因为长时间没收到心跳而应该过期的服务
2.Eureka仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上(即保证当前节点依然可用)
3.当网络稳定时,当前实例新的注册信息会被同步到其它节点中
  • 防止误杀服务
  • 配置参数
eureka.server.enable-self-preservation=true

HelloEureka

1.HelloSpringCloud基础之上进行配置,以下只出示改动部分

2.Eureka基础配置

  • 目录结构
    在这里插入图片描述
  • pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<!--热部署:写完代码不用重启,刷新即可-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>
  • application.yml
server:
  port: 7001

#eureka配置
eureka:
  instance:
    hostname: localhost #服务端实例名称,可通过c:\windows\system32\drivers\etc修改域名映射
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #为false表示自己为服务注册中心
    serviceUrl: #eureka源码看到defaultZone是其默认地址,此时修改这个默认地址 http://服务端实例名称:端口/eureka/
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eureka-server
  • 启动类
@SpringBootApplication
@EnableEurekaServer//启动Eureka服务器
public class EurekaServer {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer.class, args);
    }
}

3.服务提供方注册到Eureka

  • 改变microcloud-provider-dept-8001
  • pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • application.yml
#eureka配置,注册服务到eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: microcloud-provider-dept-8001 #实例名,应用下的多个微服务的名字
  • 启动类
@SpringBootApplication
@MapperScan("com.yc.mapper")
@EnableEurekaClient //服务启动后自动注册到eureka
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

4.服务提供方启动监控器Acurator

  • 改变microcloud-provider-dept-8001,提供监控信息
  • pom.xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • application.yml
#actuator配置,打开actuator中的端口,默认情况springboot2后这些端点都没有开放
management:
  endpoints:
    web:
      exposure:
        include: "*" #yml中*有关键字,所以加要加""

5.关闭自我保护机制+配置清理服务注册列表间隔

  • 改变eureka-7001,自我保护机制开启时Eureka中有红字
    在这里插入图片描述
  • application.yml
#eureka配置
eureka:
  instance:
    hostname: localhost #服务端实例名称,可通过c:\windows\system32\drivers\etc修改域名映射
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #为false表示自己为服务注册中心
    serviceUrl: #eureka源码看到defaultZone是其默认地址,此时修改这个默认地址 http://服务端实例名称:端口/eureka/
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    eviction-interval-timer-in-ms: 1000 #清理服务注册列表的间隔时间(清除掉线的,默认情况60s)
    enable-self-preservation: false #是否启用自我保护模式(默认关闭)

6.配置提供方发送心跳时间间隔+心跳失败时间

  • 改变microcloud-provider-dept-8001,提供监控信息
  • pom.xml
#eureka配置,注册服务到eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: microcloud-provider-dept-8001 #实例名,应用下的多个微服务的名字
    lease-renewal-interval-in-seconds: 2 #设置客户端向服务端发送心跳信息的时间间隔(默认:30s)
    lease-expiration-duration-in-seconds: 6 #设置服务器如果6秒都没有接到心跳,则将此客户端的租约设为超期

7.消费方获取eureka配置信息

  • 改变microcloud-provider-dept-8001
  • pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 方法1:原生EurekaClient(netflix)对象
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaClient
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}
-------------------------------------------
@Autowired
private EurekaClient eurekaClient;

@RequestMapping("/eurekaClient")
public Object discovery(){
    System.out.println("客户端配置信息:" + this.eurekaClient.getEurekaClientConfig());
    System.out.println("服务端应用列表:"+   this.eurekaClient.getApplications());
    return this.eurekaClient.getAllKnownRegions();//["us-east-1"]
}
  • 方法2:DiscoveryClient(springcloud)对象
@SpringBootApplication
@EnableDiscoveryClient//启用springcloud的discoveryClient
public class ProviderProductApp {
    public static void main(String[] args) {
        SpringApplication.run(  ProviderProductApp.class, args);
    }
}
-------------------------------------------
@Autowired
private DiscoveryClient discoveryClient;

@RequestMapping("/discoveryClient")
public Object springCloudDiscoveryClient(){
    System.out.println(this.discoveryClient.description());
    System.out.println("注册的服务名:"+this.discoveryClient.getServices());
    return this.discoveryClient;
}

Eureka集群理论

1.分布式系统不可能同时满足以下三种

  • 一致性(C:Consistency)
在分布式环境中,一致性指数据在多个副本之间保持数据一致
在一致性的需求下,当一个系统在数据一致的状态下执行更新操作后,应保证系统的数据仍然处于一致状态
例如一个将数据副本分布在不同分布式节点上的系统来说,如果对第一个节点的数据进行了更新操作并且更新成功后,其他节点上的数据也应该得到更新,并且所有用户都可以读取到其最新的值,那么这样的系统就被认为具有强一致性(或严格的一致性,最终一致性)
一致性要求节点少(节点越少,数据同步的消耗就小),但带来的节点少却又造成服务的可用性差
  • 可用性(A:Available)
在分布式环境中,可用性指系统提供的服务必须一直处于可用的状态,对于用户的每一个操作请求总是能够在有限的时间内返回结果
高可用无非就是增加节点,但很多节点之间的数据同步是一种消耗,极其容易造成数据不一致
  • 分区容错性(P:Partition Tolerance)
分布式系统在遇到任何网络分区故障的时候,仍然需要能够保证对外提供满足一致性和可用性的服务,除非是整个网络环境都发生了故障
必须保证

P必须要保证,CA任意保证一个

2.ZooKeeper保证的是CP

  • 当master节点因网络故障而数去联系时,需重新选举leader,但选举leader时间太长,且选举期间整个eureka集群不可用,导致不保证可用性
  • 为保证一致性,ZooKeeper做了两类同步:初始化同步+更新同步
  • 当ZooKeeper发现超过半数的Follower同步超时,则其会再次进行同步,且该期间整个eureka集群不可用

3.Eureka采用的是AP原则(牺牲一致性,保证可用性)

  • 一个节点挂掉剩下正常工作的节点任然可以提供注册和查询服务
  • 一般配置的节点是奇数个

4.不是分布式架构可满足CA

  • 关系数据库按照CA进行设计,放弃分区容错性

HelloEureka集群

1.添加域名映射

  • 位置
    在这里插入图片描述
  • 修改
    在这里插入图片描述

2.eureka-7001中添加配置文件

  • 目录结构
    在这里插入图片描述
  • application.yml
#eureka配置 --> 添加映射地址后修改hostname,defaultZone修改集群地址,name修改
eureka:
  instance:
    hostname: eureka1 #服务端实例名称,已经通过c:\windows\system32\drivers\etc修改域名映射
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #为false表示自己为服务注册中心
    serviceUrl: #eureka源码看到defaultZone是其默认地址,此时修改这个默认地址 http://服务端实例名称:端口/eureka/  -->配置集群地址
      defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/
  server:
    eviction-interval-timer-in-ms: 1000 #清理服务注册列表的间隔时间(清除掉线的,默认情况60s)
    enable-self-preservation: false #是否启用自我保护模式(默认关闭)

spring:
  application:
    name: eureka-server1
  • application-second.yml
server:
  port: 7002

eureka:
  instance:
    hostname: eureka2

spring:
  application:
    name: eureka-server2
  • application-third.yml
server:
  port: 7003

eureka:
  instance:
    hostname: eureka3

spring:
  application:
    name: eureka-server3

3.为每个配置文件分配一台服务器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.microcloud-provider-dept-8001中修改单个eureka地址为集群

  • application.yml
#eureka配置,注册服务到eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/
  instance:
    instance-id: microcloud-provider-dept-8001 #实例名,应用下的多个微服务的名字
    lease-renewal-interval-in-seconds: 2 #设置客户端向服务端发送心跳信息的时间间隔(默认:30s)
    lease-expiration-duration-in-seconds: 6 #设置服务器如果6秒都没有接到心跳,则将此客户端的租约设为超期
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值