Open chriswhite199 opened 5 years ago
A simple config based patch:
@EnableDiscoveryClient
@Configuration
public class DynamicMgmtPortConsulConfig implements ApplicationListener<WebServerInitializedEvent> {
private ConsulDiscoveryProperties discoveryProperties;
public DynamicMgmtPortConsulConfig(ConsulDiscoveryProperties discoveryProperties) {
this.discoveryProperties = discoveryProperties;
}
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
if (StringUtils.equals(event.getApplicationContext().getServerNamespace(), "management")) {
int mgmtPort = event.getWebServer().getPort();
this.discoveryProperties.setManagementPort(mgmtPort);
}
}
}
Would you be interested in submitting a PR based on that patch?
I meant that patch to useful to developers while an official version is patched. As i mentioned, i think the ManagementServerPortUtils
needs to be fixed to address this issue, rather than an AOP based hack.
That being said, i'll raise another ticket for ManagementServerPortUtils
and reference it here. Hopefully when that is closed out - this can be closed too
An update - it appears that the order of web server startup (web vs mgmt) is not well defined. I'm still seeing random failures on linux nodes where web comes up before mgmt, so my AOP patch above isn't always going to work. I have other patches that work when you disable lifecycle.enabled and manually start the config after the ApplicationStartEvent is fired, but it all seem hacky. Will investigate further.
Reclassifying as an enhancement as support for dynamic management port is not trivial.
Using both port 0 for management.server and server causes the following stack trace in 2.1.1.RELEASE:
Stacktrace
Issue tracing
Tracing through, the issue lies in
org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration#getManagementPort
which defers toorg.springframework.cloud.client.discovery.ManagementServerPortUtils#getPort
, which in turn returns 0 asManagementServerProperties.port
is 0.Appreciate this is more likely an issue with spring-cloud-commons (where ManagementServerPortUtils is defined), but raising here for visibility for others who may stumble down this path
application.yml
Application