AbstractDiscoveryLifecycle now function if actuator is not on the classpath
This commit is contained in:
@@ -19,8 +19,6 @@ package org.springframework.cloud.client.discovery;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
|
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -110,9 +108,7 @@ public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle,
|
||||
* @return if the management service should be registered with the DiscoveryService
|
||||
*/
|
||||
protected boolean shouldRegisterManagement() {
|
||||
return getManagementServerProperties() != null
|
||||
&& getManagementPort() != null
|
||||
&& ManagementServerPortUtils.isDifferent(this.context);
|
||||
return getManagementPort() != null && ManagementServerPortUtils.isDifferent(this.context);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,15 +163,7 @@ public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle,
|
||||
* @return the management server port
|
||||
*/
|
||||
protected Integer getManagementPort() {
|
||||
return getManagementServerProperties().getPort();
|
||||
}
|
||||
|
||||
private ManagementServerProperties getManagementServerProperties() {
|
||||
try {
|
||||
return this.context.getBean(ManagementServerProperties.class);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
return null;
|
||||
}
|
||||
return ManagementServerPortUtils.getPort(this.context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,17 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class ManagementServerPortUtils {
|
||||
private static final boolean hasActuator;
|
||||
static {
|
||||
boolean hasClass;
|
||||
try {
|
||||
Class.forName("org.springframework.boot.actuate.autoconfigure.ManagementServerProperties");
|
||||
hasClass = true;
|
||||
} catch (ClassNotFoundException e) {
|
||||
hasClass = false;
|
||||
}
|
||||
hasActuator = hasClass;
|
||||
}
|
||||
|
||||
public static ManagementServerPort get(BeanFactory beanFactory) {
|
||||
return ManagementServerPort.get(beanFactory);
|
||||
@@ -44,12 +55,30 @@ public class ManagementServerPortUtils {
|
||||
return get(beanFactory) == ManagementServerPort.SAME;
|
||||
}
|
||||
|
||||
public static Integer getPort(BeanFactory beanFactory) {
|
||||
if (!hasActuator) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
ManagementServerProperties properties = beanFactory
|
||||
.getBean(ManagementServerProperties.class);
|
||||
return properties.getPort();
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: copied from EndpointWebMvcAutoConfiguration.ManagementServerPort
|
||||
public static enum ManagementServerPort {
|
||||
|
||||
DISABLE, SAME, DIFFERENT;
|
||||
|
||||
public static ManagementServerPort get(BeanFactory beanFactory) {
|
||||
if (!hasActuator) {
|
||||
return SAME;
|
||||
}
|
||||
|
||||
ServerProperties serverProperties;
|
||||
try {
|
||||
serverProperties = beanFactory.getBean(ServerProperties.class);
|
||||
|
||||
Reference in New Issue
Block a user