From 4995ffc88adb2e867289c6694113c72d7dd7fe30 Mon Sep 17 00:00:00 2001 From: spencergibb Date: Mon, 15 Mar 2021 21:00:34 -0400 Subject: [PATCH] Revert "Detects dynamic mgmt port in auto service registration." This reverts commit c92a9414 --- .../AbstractAutoServiceRegistration.java | 40 ++++++------------- ...oServiceRegistrationMgmtDisabledTests.java | 2 +- .../AbstractAutoServiceRegistrationTests.java | 2 +- 3 files changed, 15 insertions(+), 29 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 58cd8bda..48f9c0aa 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,17 +25,15 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; -import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; 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} @@ -45,10 +43,9 @@ import org.springframework.util.StringUtils; * * @param Registration type passed to the {@link ServiceRegistry}. * @author Spencer Gibb - * @author Chris White */ public abstract class AbstractAutoServiceRegistration - implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener { + implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener { private static final Log logger = LogFactory.getLog(AbstractAutoServiceRegistration.class); @@ -66,8 +63,6 @@ public abstract class AbstractAutoServiceRegistration private AtomicInteger port = new AtomicInteger(0); - private AtomicInteger mgmtPort = new AtomicInteger(0); - private AutoServiceRegistrationProperties properties; @Deprecated @@ -86,25 +81,21 @@ public abstract class AbstractAutoServiceRegistration } @Override - public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof ApplicationReadyEvent) { - this.start(); - } - else if (event instanceof WebServerInitializedEvent) { - this.bind((WebServerInitializedEvent) event); - } + @SuppressWarnings("deprecation") + public void onApplicationEvent(WebServerInitializedEvent event) { + bind(event); } @Deprecated public void bind(WebServerInitializedEvent event) { - 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()); + ApplicationContext context = event.getApplicationContext(); + if (context instanceof ConfigurableWebServerApplicationContext) { + if ("management".equals(((ConfigurableWebServerApplicationContext) context).getServerNamespace())) { + return; + } } + this.port.compareAndSet(0, event.getWebServer().getPort()); + this.start(); } @Override @@ -194,12 +185,7 @@ public abstract class AbstractAutoServiceRegistration */ @Deprecated protected Integer getManagementPort() { - if (this.mgmtPort.get() != 0) { - return this.mgmtPort.get(); - } - else { - return ManagementServerPortUtils.getPort(this.context); - } + 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 6bc78601..830372a2 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 @@ -39,7 +39,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen */ @RunWith(SpringRunner.class) @SpringBootTest(classes = AbstractAutoServiceRegistrationMgmtDisabledTests.Config.class, - properties = { "management.server.port=0", + properties = { "management.port=0", "spring.cloud.service-registry.auto-registration.register-management=false" }, webEnvironment = RANDOM_PORT) public class AbstractAutoServiceRegistrationMgmtDisabledTests { 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 7e28872b..f5f820f4 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,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen */ @RunWith(SpringRunner.class) // @checkstyle:off -@SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, properties = "management.server.port=0", +@SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, properties = "management.port=0", webEnvironment = RANDOM_PORT) // @checkstyle:on public class AbstractAutoServiceRegistrationTests {