publish eureka instance events
This commit is contained in:
@@ -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<EurekaRegistryAvailableEvent> {
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<InstanceInfo> {
|
||||
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() {}
|
||||
}
|
||||
Reference in New Issue
Block a user