Eureka从不注销服务



我目前面临一个问题,Eureka不注销已注册的服务。我直接从git hub中提取了Eureka服务器示例,只做了一个更改,eureka.enableSelfPreservation = false。我的应用程序。Yml看起来像这样:

server:
  port: 8761
eureka:
  enableSelfPreservation: false
  client:
    registerWithEureka: false
fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0

我读到,如果85%的已注册服务在15分钟内停止传递心跳,Eureka认为问题与网络有关,并且不会注销没有响应的服务。在我的例子中,我只有一个服务在运行,所以我禁用了自我保护模式。我突然终止了这个进程,Eureka让服务注册了一段似乎不确定的时间。

我的客户的申请。Yml看起来像这样:

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 3
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  appInfo:
    replicate:
      interval: 3
    initial:
      replicate:
        time: 3
spring:
  rabbitmq:
    addresses: ${vcap.services.${PREFIX:}rabbitmq.credentials.uri:amqp://${RABBITMQ_HOST:localhost}:${RABBITMQ_PORT:5672}}

我的目标是创建一个演示,其中Eureka快速检测到服务不再运行,并且另一个启动的服务可以快速注册自己。

到目前为止,一旦eureka客户端启动,它将在3秒内注册。当服务突然终止时,它永远不会注销。在我关闭服务后,尤里卡仪表板显示:

紧急!Eureka可能错误地声称实例已经启动,但实际上并没有。续费低于阈值,因此实例不会过期,只是为了安全。

我怎样才能防止这种行为?

我意识到自我保护模式实际上从来没有被禁用过。实际上属性是

eureka.server.enableSelfPreservation=false

(见DefaultEurekaServerConfig Code),我没有在任何地方找到文档。这解决了我的问题。

通过设置以下值

使服务取消注册工作

Eureka server application.yml

eureka:
  server:
    enableSelfPreservation: false
服务application.yml

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
完整的示例在这里https://github.com/ExampleDriven/spring-cloud-eureka-example

挣扎了很多,最后我得到了解决方案,如果任何服务未注册从尤里卡服务器由于一些问题。它将通过扩展Eureka-Server api的HealthCallback来通知管理员。假设Service-A注册到Eureka。因此,Eureka Client与Service- a集成,并在Service a中实现以下回调。

服务[Eureka-Client]在属性文件中添加以下属性:

#Eureka Configuration
eureka.client.eureka-server-port=8761
eureka.client.register-with-eureka=true
eureka.client.healthcheck.enabled=false
eureka.client.prefer-same-zone-eureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone=${eurekaServerURL1}, ${eurekaServerURL2}
eureka.client.eureka.service-url.defaultZone=${eurekaServerURL1}, ${eurekaServerURL2}
eureka.instance.hostname=${hostname}
eureka.client.lease.duration=30
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=30

添加以下java文件

@Component
public class EurekaHealthCheckHandler implements HealthCheckHandler, ApplicationContextAware, InitializingBean {

    static Logger logger = LoggerFactory.getLogger(EurekaHealthCheckHandler.class);
    private static final Map<Status, InstanceInfo.InstanceStatus> healthStatuses = new HashMap<Status, InstanceInfo.InstanceStatus>() {{
        put(Status.UNKNOWN, InstanceInfo.InstanceStatus.UNKNOWN);
        put(Status.OUT_OF_SERVICE, InstanceInfo.InstanceStatus.OUT_OF_SERVICE);
        put(Status.DOWN, InstanceInfo.InstanceStatus.DOWN);
        put(Status.UP, InstanceInfo.InstanceStatus.UP);
    }};
@Autowired
ComunocationService comunocationService ;
    private final CompositeHealthIndicator healthIndicator;
    private ApplicationContext applicationContext;
    public EurekaHealthCheckHandler(HealthAggregator healthAggregator) {
        Assert.notNull(healthAggregator, "HealthAggregator must not be null");
        this.healthIndicator = new CompositeHealthIndicator(healthAggregator);
        Health health = healthIndicator.health();
        logger.info(" =========== Testing =========== {}", health.toString() );
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        final Map<String, HealthIndicator> healthIndicators = applicationContext.getBeansOfType(HealthIndicator.class);
        for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
            logger.info("======"+ entry.getKey() +"============= "+entry.getValue());
            healthIndicator.addHealthIndicator(entry.getKey(), entry.getValue());
        }
    }
    @Override
    public InstanceInfo.InstanceStatus getStatus(InstanceInfo.InstanceStatus instanceStatus) {
        logger.info("============== Custome Eureka Implementation ==================="+ getHealthStatus());
        return getHealthStatus();
    }
    protected InstanceInfo.InstanceStatus getHealthStatus() {
        final Status status = healthIndicator.health().getStatus();
        return mapToInstanceStatus(status);
    }
    protected InstanceInfo.InstanceStatus mapToInstanceStatus(Status status) {
        logger.info("============== Test Custome Eureka Implementation ==================={}", status);
        if(status.equals(InstanceInfo.InstanceStatus.UP)) {
            // Send mail after configured times
              comunocationService.sendEmail("ServiceName");
        }
        if(!healthStatuses.containsKey(status)) {
            return InstanceInfo.InstanceStatus.UNKNOWN;
        }
        return healthStatuses.get(status);
    }
    public void getstatusChangeListner() {
        ApplicationInfoManager.StatusChangeListener statusChangeListener = new ApplicationInfoManager.StatusChangeListener() {
            @Override
            public String getId() {
                return "statusChangeListener";
            }
            @Override
            public void notify(StatusChangeEvent statusChangeEvent) {
                if (InstanceStatus.DOWN == statusChangeEvent.getStatus() ||
                        InstanceStatus.DOWN == statusChangeEvent.getPreviousStatus()) {
                    // log at warn level if DOWN was involved
                    logger.warn("Saw local status change event {}", statusChangeEvent);
                } else {
                    logger.info("Saw local status change event {}", statusChangeEvent);
                }
            }
        };
    }

}

@Configuration
public class EurekaHealthCheckHandlerConfiguration {
    @Autowired(required = false)
    private HealthAggregator healthAggregator = new OrderedHealthAggregator();
    @Bean
    @ConditionalOnMissingBean
    public EurekaHealthCheckHandler eurekaHealthCheckHandler() {
        return new EurekaHealthCheckHandler(healthAggregator);
    }
}

这绝对是工作和良好测试的代码

最新更新