Eureka服务发现和自我保护机制

配置服务信息完善

首先保证有依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在配置中指定eureka.instance.instance-id显示服务提供方的主机名

配置eureka.instance.prefer-ip-address 可显示服务提供方的ip地址

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
    # 将自己注册进eureka server
    register-with-eureka: true
    # 是否从eureka server抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
  instance:
    instance-id: payment8002 # 显示主机名
    prefer-ip-address: true # 访问路径可以显示ip地址

服务发现

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

在启动类上配置@EnableDiscoveryClient注解以启用DiscoveryClient

注入DiscoveryClient:
(org.springframework.cloud.client.discovery.DiscoveryClient)

@Resource
private DiscoveryClient discoveryClient;

使用:

List<String> services = discoveryClient.getServices();
for (String service : services) {
    log.info("****element: "+service);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
    log.info(instance.getInstanceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}

自我保护

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

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

在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。

提示:

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.

这个机制是CAP中AP的体现。

禁止自我保护

在eureka server配置中配置eureka.server.enable-self-perservation = false

客户端心跳配置

  • lease-renewal-interval-in-seconds:10 客户端向服务端发送心跳的时间间隔,单位为秒(默认为30秒)
  • lease-expiration-duration-in-seconds:20 服务端在收到最后一次心跳后等待时间上限,单位为秒(默认为90秒),超时将移除服务

Eureka停更说明

Eureka 2.0 (Discontinued)

The existing open source work on eureka 2.0 is discontinued. The code base and artifacts that were released as part of the existing repository of work on the 2.x branch is considered use at your own risk.

Eureka 1.x is a core part of Netflix's service discovery system and is still an active project.

目前已经不再推荐使用Eureka,可以使用Zookeeper/consul/nacos来代替它

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/eureka%e6%9c%8d%e5%8a%a1%e5%8f%91%e7%8e%b0%e5%92%8c%e8%87%aa%e6%88%91%e4%bf%9d%e6%8a%a4%e6%9c%ba%e5%88%b6/

(0)
彭晨涛彭晨涛管理者
上一篇 2020年3月20日 19:28
下一篇 2020年3月20日

相关推荐

发表回复

登录后才能评论