Search in sources :

Example 1 with EurekaInstanceRenewedEvent

use of org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent 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 EurekaInstanceRenewedEvent

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

the class InstanceRegistryTests method testRenew.

@Test
public void testRenew() throws Exception {
    // Creating two instances of the app
    final InstanceInfo instanceInfo1 = getInstanceInfo(APP_NAME, HOST_NAME, INSTANCE_ID, PORT, null);
    final InstanceInfo instanceInfo2 = getInstanceInfo(APP_NAME, HOST_NAME, "my-host-name:8009", 8009, null);
    // creating application list with an app having two instances
    final Application application = new Application(APP_NAME, Arrays.asList(instanceInfo1, instanceInfo2));
    final List<Application> applications = new ArrayList<>();
    applications.add(application);
    // stubbing applications list
    doReturn(applications).when(instanceRegistry).getSortedApplications();
    // calling tested method
    instanceRegistry.renew(APP_NAME, INSTANCE_ID, false);
    instanceRegistry.renew(APP_NAME, "my-host-name:8009", false);
    // event of proper type is registered
    assertEquals(2, this.testEvents.applicationEvents.size());
    assertTrue(this.testEvents.applicationEvents.get(0) instanceof EurekaInstanceRenewedEvent);
    assertTrue(this.testEvents.applicationEvents.get(1) instanceof EurekaInstanceRenewedEvent);
    // event details are correct
    final EurekaInstanceRenewedEvent event1 = (EurekaInstanceRenewedEvent) (this.testEvents.applicationEvents.get(0));
    assertEquals(APP_NAME, event1.getAppName());
    assertEquals(INSTANCE_ID, event1.getServerId());
    assertEquals(instanceRegistry, event1.getSource());
    assertEquals(instanceInfo1, event1.getInstanceInfo());
    assertFalse(event1.isReplication());
    final EurekaInstanceRenewedEvent event2 = (EurekaInstanceRenewedEvent) (this.testEvents.applicationEvents.get(1));
    assertEquals(instanceInfo2, event2.getInstanceInfo());
}
Also used : EurekaInstanceRenewedEvent(org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent) ArrayList(java.util.ArrayList) InstanceInfo(com.netflix.appinfo.InstanceInfo) TestApplication(org.springframework.cloud.netflix.eureka.server.InstanceRegistryTests.TestApplication) Application(com.netflix.discovery.shared.Application) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with EurekaInstanceRenewedEvent

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

the class InstanceRegistry method renew.

@Override
public boolean renew(final String appName, final String serverId, boolean isReplication) {
    log("renew " + appName + " serverId " + serverId + ", isReplication {}" + isReplication);
    List<Application> applications = getSortedApplications();
    for (Application input : applications) {
        if (input.getName().equals(appName)) {
            InstanceInfo instance = null;
            for (InstanceInfo info : input.getInstances()) {
                if (info.getId().equals(serverId)) {
                    instance = info;
                    break;
                }
            }
            publishEvent(new EurekaInstanceRenewedEvent(this, appName, serverId, instance, isReplication));
            break;
        }
    }
    return super.renew(appName, serverId, isReplication);
}
Also used : EurekaInstanceRenewedEvent(org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Aggregations

EurekaInstanceRenewedEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent)3 InstanceInfo (com.netflix.appinfo.InstanceInfo)2 Application (com.netflix.discovery.shared.Application)2 Applications (com.netflix.discovery.shared.Applications)1 PeerAwareInstanceRegistry (com.netflix.eureka.registry.PeerAwareInstanceRegistry)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 TestApplication (org.springframework.cloud.netflix.eureka.server.InstanceRegistryTests.TestApplication)1 EurekaInstanceCanceledEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent)1 EurekaInstanceRegisteredEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent)1 EurekaRegistryAvailableEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent)1