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。。。");
}
}
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());
}
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);
}
Aggregations