From c8be52b025ade2f0dc5abebf86f515895b12a8bc Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 12 Aug 2014 00:56:27 -0600 Subject: [PATCH] publish eureka instance events --- .../eureka/EurekaServerAutoConfiguration.java | 52 ++++++++++++++++++- .../eureka/advice/LeaseManagerLite.java | 28 ---------- .../event/EurekaInstanceCanceledEvent.java | 21 ++++++++ .../event/EurekaInstanceRegisteredEvent.java | 23 ++++++++ .../event/EurekaInstanceRenewedEvent.java | 21 ++++++++ .../EurekaRegistryAvailableEvent.java | 2 +- .../{ => event}/EurekaServerStartedEvent.java | 2 +- .../event/LeaseManagerMessageBroker.java | 44 ++++++++++++++++ 8 files changed, 162 insertions(+), 31 deletions(-) delete mode 100644 spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/LeaseManagerLite.java create mode 100644 spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceCanceledEvent.java create mode 100644 spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceRegisteredEvent.java create mode 100644 spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceRenewedEvent.java rename spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/{ => event}/EurekaRegistryAvailableEvent.java (94%) rename spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/{ => event}/EurekaServerStartedEvent.java (94%) create mode 100644 spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/LeaseManagerMessageBroker.java diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaServerAutoConfiguration.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaServerAutoConfiguration.java index bddbd056..310ea422 100644 --- a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaServerAutoConfiguration.java +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaServerAutoConfiguration.java @@ -18,14 +18,24 @@ package org.springframework.platform.netflix.eureka; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; +import com.netflix.eureka.PeerAwareInstanceRegistry; +import com.netflix.eureka.lease.LeaseManager; +import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationListener; import org.springframework.context.SmartLifecycle; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; +import org.springframework.platform.netflix.eureka.advice.PiggybackMethodInterceptor; +import org.springframework.platform.netflix.eureka.event.EurekaRegistryAvailableEvent; +import org.springframework.platform.netflix.eureka.event.EurekaServerStartedEvent; +import org.springframework.platform.netflix.eureka.event.LeaseManagerMessageBroker; +import org.springframework.util.ReflectionUtils; import org.springframework.web.context.ServletContextAware; import com.netflix.blitz4j.LoggingConfiguration; @@ -33,6 +43,9 @@ import com.netflix.eureka.EurekaBootStrap; import com.netflix.eureka.EurekaServerConfig; import com.netflix.eureka.EurekaServerConfigurationManager; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + /** * @author Dave Syer * @@ -112,5 +125,42 @@ public class EurekaServerAutoConfiguration implements ServletContextAware, public int getOrder() { return order; } - + + @Configuration + @ConditionalOnClass(PeerAwareInstanceRegistry.class) + protected static class Initializer implements + ApplicationListener { + + @Autowired + private ApplicationContext applicationContext; + + @Bean + public LeaseManagerMessageBroker leaseManagerMessageBroker() { + return new LeaseManagerMessageBroker(); + } + + @Override + public void onApplicationEvent(EurekaRegistryAvailableEvent event) { + //wrap the instance registry... + ProxyFactory factory = new ProxyFactory(PeerAwareInstanceRegistry.getInstance()); + //...with the LeaseManagerMessageBroker + factory.addAdvice(new PiggybackMethodInterceptor(leaseManagerMessageBroker(), LeaseManager.class)); + factory.setProxyTargetClass(true); + + //Now replace the PeerAwareInstanceRegistry with our wrapped version + Field field = ReflectionUtils.findField(PeerAwareInstanceRegistry.class, "instance"); + try { + // Awful ugly hack to work around lack of DI in eureka + field.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + ReflectionUtils.setField(field, null, factory.getProxy()); + } + catch (Exception e) { + throw new IllegalStateException("Cannot modify instance registry", e); + } + } + + } } diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/LeaseManagerLite.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/LeaseManagerLite.java deleted file mode 100644 index 603319b3..00000000 --- a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/LeaseManagerLite.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2013-2014 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.platform.netflix.eureka.advice; - -import com.netflix.appinfo.InstanceInfo; - -/** - * @author Dave Syer - * - */ -public interface LeaseManagerLite { - - void register(final InstanceInfo info, final boolean isReplication); - -} diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceCanceledEvent.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceCanceledEvent.java new file mode 100644 index 00000000..f3721c8e --- /dev/null +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceCanceledEvent.java @@ -0,0 +1,21 @@ +package org.springframework.platform.netflix.eureka.event; + +import lombok.Data; +import org.springframework.context.ApplicationEvent; + +/** + * @author Spencer Gibb + */ +@Data +public class EurekaInstanceCanceledEvent extends ApplicationEvent { + private String appName; + private String serverId; + boolean replication; + + public EurekaInstanceCanceledEvent(Object source, String appName, String serverId, boolean replication) { + super(source); + this.appName = appName; + this.serverId = serverId; + this.replication = replication; + } +} diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceRegisteredEvent.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceRegisteredEvent.java new file mode 100644 index 00000000..7bfc5e22 --- /dev/null +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceRegisteredEvent.java @@ -0,0 +1,23 @@ +package org.springframework.platform.netflix.eureka.event; + +import lombok.Data; +import org.springframework.context.ApplicationEvent; + +/** + * @author Spencer Gibb + */ +@Data +public class EurekaInstanceRegisteredEvent extends ApplicationEvent { + private String appName; + private String vip; + private int leaseDuration; + boolean replication; + + public EurekaInstanceRegisteredEvent(Object source, String appName, String vip, int leaseDuration, boolean replication) { + super(source); + this.appName = appName; + this.vip = vip; + this.leaseDuration = leaseDuration; + this.replication = replication; + } +} diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceRenewedEvent.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceRenewedEvent.java new file mode 100644 index 00000000..a7d8a2c2 --- /dev/null +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaInstanceRenewedEvent.java @@ -0,0 +1,21 @@ +package org.springframework.platform.netflix.eureka.event; + +import lombok.Data; +import org.springframework.context.ApplicationEvent; + +/** + * @author Spencer Gibb + */ +@Data +public class EurekaInstanceRenewedEvent extends ApplicationEvent { + private String appName; + private String serverId; + boolean replication; + + public EurekaInstanceRenewedEvent(Object source, String appName, String serverId, boolean replication) { + super(source); + this.appName = appName; + this.serverId = serverId; + this.replication = replication; + } +} diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaRegistryAvailableEvent.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaRegistryAvailableEvent.java similarity index 94% rename from spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaRegistryAvailableEvent.java rename to spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaRegistryAvailableEvent.java index 58f86a1b..38d87243 100644 --- a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaRegistryAvailableEvent.java +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaRegistryAvailableEvent.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.platform.netflix.eureka; +package org.springframework.platform.netflix.eureka.event; import org.springframework.context.ApplicationEvent; diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaServerStartedEvent.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaServerStartedEvent.java similarity index 94% rename from spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaServerStartedEvent.java rename to spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaServerStartedEvent.java index 802d9d55..ddfa3f70 100644 --- a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/EurekaServerStartedEvent.java +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/EurekaServerStartedEvent.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.platform.netflix.eureka; +package org.springframework.platform.netflix.eureka.event; import org.springframework.context.ApplicationEvent; diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/LeaseManagerMessageBroker.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/LeaseManagerMessageBroker.java new file mode 100644 index 00000000..26c4afff --- /dev/null +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/event/LeaseManagerMessageBroker.java @@ -0,0 +1,44 @@ +package org.springframework.platform.netflix.eureka.event; + +import com.netflix.appinfo.InstanceInfo; +import com.netflix.eureka.lease.LeaseManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; + +/** + * @author Spencer Gibb + */ +public class LeaseManagerMessageBroker implements LeaseManager { + private static final Logger logger = LoggerFactory.getLogger(LeaseManagerMessageBroker.class); + + @Autowired + private ApplicationContext ctxt; + + @Override + public void register(InstanceInfo info, int leaseDuration, boolean isReplication) { + logger.debug("register {}, vip {}, leaseDuration {}, isReplication {}", + info.getAppName(), info.getVIPAddress(), leaseDuration, isReplication); + //TODO: what to publish from info (whole object?) + ctxt.publishEvent(new EurekaInstanceRegisteredEvent(this, info.getAppName(), + info.getVIPAddress(), leaseDuration, isReplication)); + } + + @Override + public boolean cancel(String appName, String serverId, boolean isReplication) { + logger.debug("cancel {}, serverId {}, isReplication {}", appName, serverId, isReplication); + ctxt.publishEvent(new EurekaInstanceCanceledEvent(this, appName, serverId, isReplication)); + return false; + } + + @Override + public boolean renew(String appName, String serverId, boolean isReplication) { + logger.debug("renew {}, serverId {}, isReplication {}", appName, serverId, isReplication); + ctxt.publishEvent(new EurekaInstanceRenewedEvent(this, appName, serverId, isReplication)); + return false; + } + + @Override + public void evict() {} +}