Search in sources :

Example 1 with EurekaInstanceCanceledEvent

use of org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent in project spring-boot by Linda-Tan.

the class EurekaInstanceCanceledListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
    // 服务挂掉自动通知
    if (applicationEvent instanceof EurekaInstanceCanceledEvent) {
        EurekaInstanceCanceledEvent event = (EurekaInstanceCanceledEvent) applicationEvent;
        // 获取当前Eureka示例中的节点信息
        PeerAwareInstanceRegistry registry = EurekaServerContextHolder.getInstance().getServerContext().getRegistry();
        Applications applications = registry.getApplications();
        // 便利获取已注册节点中与当前失效节点ID一致 的节点信息
        applications.getRegisteredApplications().forEach(registryApplication -> {
            registryApplication.getInstances().forEach(instance -> {
                if (Objects.equals(instance.getInstanceId(), event.getServerId())) {
                    log.info("服务:{}挂啦。。。", instance.getAppName());
                // emailService.send(new SimpleMailMessage());
                }
            });
        });
    }
    if (applicationEvent instanceof EurekaInstanceRegisteredEvent) {
        EurekaInstanceRegisteredEvent event = (EurekaInstanceRegisteredEvent) applicationEvent;
        log.info("服务:{}注册成功啦。。。", event.getInstanceInfo().getAppName());
    }
    if (applicationEvent instanceof EurekaInstanceRenewedEvent) {
        EurekaInstanceRenewedEvent event = (EurekaInstanceRenewedEvent) applicationEvent;
        log.info("心跳检测服务:{}。。。", event.getInstanceInfo().getAppName());
    }
    if (applicationEvent instanceof EurekaRegistryAvailableEvent) {
        log.info("服务 Available。。。");
    }
}
Also used : EurekaInstanceRenewedEvent(org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent) Applications(com.netflix.discovery.shared.Applications) EurekaInstanceCanceledEvent(org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent) EurekaRegistryAvailableEvent(org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent) PeerAwareInstanceRegistry(com.netflix.eureka.registry.PeerAwareInstanceRegistry) EurekaInstanceRegisteredEvent(org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent)

Example 2 with EurekaInstanceCanceledEvent

use of org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent in project spring-cloud-netflix by spring-cloud.

the class InstanceRegistryTests method testInternalCancel.

@Test
public void testInternalCancel() throws Exception {
    // calling tested method
    instanceRegistry.internalCancel(APP_NAME, HOST_NAME, false);
    // event of proper type is registered
    assertEquals(1, this.testEvents.applicationEvents.size());
    assertTrue(this.testEvents.applicationEvents.get(0) instanceof EurekaInstanceCanceledEvent);
    // event details are correct
    final EurekaInstanceCanceledEvent registeredEvent = (EurekaInstanceCanceledEvent) (this.testEvents.applicationEvents.get(0));
    assertEquals(APP_NAME, registeredEvent.getAppName());
    assertEquals(HOST_NAME, registeredEvent.getServerId());
    assertEquals(instanceRegistry, registeredEvent.getSource());
    assertFalse(registeredEvent.isReplication());
}
Also used : EurekaInstanceCanceledEvent(org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with EurekaInstanceCanceledEvent

use of org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent in project spring-cloud-netflix by spring-cloud.

the class InstanceRegistry method handleCancelation.

private void handleCancelation(String appName, String id, boolean isReplication) {
    log("cancel " + appName + ", serverId " + id + ", isReplication " + isReplication);
    publishEvent(new EurekaInstanceCanceledEvent(this, appName, id, isReplication));
}
Also used : EurekaInstanceCanceledEvent(org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent)

Aggregations

EurekaInstanceCanceledEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent)3 Applications (com.netflix.discovery.shared.Applications)1 PeerAwareInstanceRegistry (com.netflix.eureka.registry.PeerAwareInstanceRegistry)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 EurekaInstanceRegisteredEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent)1 EurekaInstanceRenewedEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent)1 EurekaRegistryAvailableEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent)1