From 841c183783f591d02fc30aed5e3cfc7cc3a82b77 Mon Sep 17 00:00:00 2001 From: Chris White Date: Tue, 30 Apr 2019 22:49:14 -0700 Subject: [PATCH] Detect dynamic mgmt port. Fixes gh-561 --- .../AbstractAutoServiceRegistration.java | 41 ++++++++++++------- ...oServiceRegistrationMgmtDisabledTests.java | 7 ++-- .../AbstractAutoServiceRegistrationTests.java | 3 +- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java index 1ca17d25a..28dbbff20 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java @@ -25,15 +25,17 @@ import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; -import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; +import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.cloud.client.discovery.ManagementServerPortUtils; import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.env.Environment; +import org.springframework.util.StringUtils; /** * Lifecycle methods that may be useful and common to {@link ServiceRegistry} @@ -43,10 +45,11 @@ * * @param Registration type passed to the {@link ServiceRegistry}. * @author Spencer Gibb + * @author Chris White */ public abstract class AbstractAutoServiceRegistration implements AutoServiceRegistration, ApplicationContextAware, - ApplicationListener { + ApplicationListener { private static final Log logger = LogFactory .getLog(AbstractAutoServiceRegistration.class); @@ -65,6 +68,8 @@ public abstract class AbstractAutoServiceRegistration private AtomicInteger port = new AtomicInteger(0); + private AtomicInteger mgmtPort = new AtomicInteger(0); + private AutoServiceRegistrationProperties properties; @Deprecated @@ -83,22 +88,25 @@ protected ApplicationContext getContext() { } @Override - @SuppressWarnings("deprecation") - public void onApplicationEvent(WebServerInitializedEvent event) { - bind(event); + public void onApplicationEvent(ApplicationEvent event) { + if (event instanceof ApplicationReadyEvent) { + this.start(); + } + else if (event instanceof WebServerInitializedEvent) { + this.bind((WebServerInitializedEvent) event); + } } @Deprecated public void bind(WebServerInitializedEvent event) { - ApplicationContext context = event.getApplicationContext(); - if (context instanceof ConfigurableWebServerApplicationContext) { - if ("management".equals(((ConfigurableWebServerApplicationContext) context) - .getServerNamespace())) { - return; - } + String serverNamespace = event.getApplicationContext().getServerNamespace(); + + if (StringUtils.isEmpty(serverNamespace)) { + this.port.compareAndSet(0, event.getWebServer().getPort()); + } + else if ("management".equals(serverNamespace)) { + this.mgmtPort.compareAndSet(0, event.getWebServer().getPort()); } - this.port.compareAndSet(0, event.getWebServer().getPort()); - this.start(); } @Override @@ -192,7 +200,12 @@ protected String getManagementServiceName() { */ @Deprecated protected Integer getManagementPort() { - return ManagementServerPortUtils.getPort(this.context); + if (this.mgmtPort.get() != 0) { + return this.mgmtPort.get(); + } + else { + return ManagementServerPortUtils.getPort(this.context); + } } /** diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java index bfaf66e39..248a10c6a 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java @@ -38,9 +38,10 @@ * @author Tim Ysewyn */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = AbstractAutoServiceRegistrationMgmtDisabledTests.Config.class, properties = { - "management.port=0", - "spring.cloud.service-registry.auto-registration.register-management=false" }, webEnvironment = RANDOM_PORT) +@SpringBootTest(classes = AbstractAutoServiceRegistrationMgmtDisabledTests.Config.class, + properties = { "management.server.port=0", + "spring.cloud.service-registry.auto-registration.register-management=false" }, + webEnvironment = RANDOM_PORT) public class AbstractAutoServiceRegistrationMgmtDisabledTests { @Autowired diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java index 1ad8816de..5ae8c7baf 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java @@ -44,7 +44,8 @@ */ @RunWith(SpringRunner.class) // @checkstyle:off -@SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, properties = "management.port=0", webEnvironment = RANDOM_PORT) +@SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, + properties = "management.server.port=0", webEnvironment = RANDOM_PORT) // @checkstyle:on public class AbstractAutoServiceRegistrationTests {