Detects dynamic mgmt port in auto service registration.

Fixes gh-561
Fixes gh-562
This commit is contained in:
Chris White
2019-04-30 22:49:14 -07:00
committed by spencergibb
parent 483f4a69fe
commit c92a941489
3 changed files with 29 additions and 15 deletions

View File

@@ -25,15 +25,17 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException; 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.boot.web.context.WebServerInitializedEvent;
import org.springframework.cloud.client.discovery.ManagementServerPortUtils; import org.springframework.cloud.client.discovery.ManagementServerPortUtils;
import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent; import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
/** /**
* Lifecycle methods that may be useful and common to {@link ServiceRegistry} * Lifecycle methods that may be useful and common to {@link ServiceRegistry}
@@ -43,9 +45,10 @@ import org.springframework.core.env.Environment;
* *
* @param <R> Registration type passed to the {@link ServiceRegistry}. * @param <R> Registration type passed to the {@link ServiceRegistry}.
* @author Spencer Gibb * @author Spencer Gibb
* @author Chris White
*/ */
public abstract class AbstractAutoServiceRegistration<R extends Registration> public abstract class AbstractAutoServiceRegistration<R extends Registration>
implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener<WebServerInitializedEvent> { implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener<ApplicationEvent> {
private static final Log logger = LogFactory.getLog(AbstractAutoServiceRegistration.class); private static final Log logger = LogFactory.getLog(AbstractAutoServiceRegistration.class);
@@ -63,6 +66,8 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
private AtomicInteger port = new AtomicInteger(0); private AtomicInteger port = new AtomicInteger(0);
private AtomicInteger mgmtPort = new AtomicInteger(0);
private AutoServiceRegistrationProperties properties; private AutoServiceRegistrationProperties properties;
@Deprecated @Deprecated
@@ -81,21 +86,25 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
} }
@Override @Override
@SuppressWarnings("deprecation") public void onApplicationEvent(ApplicationEvent event) {
public void onApplicationEvent(WebServerInitializedEvent event) { if (event instanceof ApplicationReadyEvent) {
bind(event); this.start();
}
else if (event instanceof WebServerInitializedEvent) {
this.bind((WebServerInitializedEvent) event);
}
} }
@Deprecated @Deprecated
public void bind(WebServerInitializedEvent event) { public void bind(WebServerInitializedEvent event) {
ApplicationContext context = event.getApplicationContext(); String serverNamespace = event.getApplicationContext().getServerNamespace();
if (context instanceof ConfigurableWebServerApplicationContext) {
if ("management".equals(((ConfigurableWebServerApplicationContext) context).getServerNamespace())) { if (StringUtils.isEmpty(serverNamespace)) {
return; 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 @Override
@@ -185,7 +194,12 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
*/ */
@Deprecated @Deprecated
protected Integer getManagementPort() { protected Integer getManagementPort() {
return ManagementServerPortUtils.getPort(this.context); if (this.mgmtPort.get() != 0) {
return this.mgmtPort.get();
}
else {
return ManagementServerPortUtils.getPort(this.context);
}
} }
/** /**

View File

@@ -39,7 +39,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = AbstractAutoServiceRegistrationMgmtDisabledTests.Config.class, @SpringBootTest(classes = AbstractAutoServiceRegistrationMgmtDisabledTests.Config.class,
properties = { "management.port=0", properties = { "management.server.port=0",
"spring.cloud.service-registry.auto-registration.register-management=false" }, "spring.cloud.service-registry.auto-registration.register-management=false" },
webEnvironment = RANDOM_PORT) webEnvironment = RANDOM_PORT)
public class AbstractAutoServiceRegistrationMgmtDisabledTests { public class AbstractAutoServiceRegistrationMgmtDisabledTests {

View File

@@ -44,7 +44,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
// @checkstyle:off // @checkstyle:off
@SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, properties = "management.port=0", @SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, properties = "management.server.port=0",
webEnvironment = RANDOM_PORT) webEnvironment = RANDOM_PORT)
// @checkstyle:on // @checkstyle:on
public class AbstractAutoServiceRegistrationTests { public class AbstractAutoServiceRegistrationTests {