Use WebServerInitializedEvent to get port for SimpleDiscoveryProperties (#598)
* Use WebServerInitializedEvent to get port for SimpleDiscoveryProperties. Fixes #255
This commit is contained in:
@@ -22,18 +22,17 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
// import org.springframework.boot.context.embedded.EmbeddedServletContainer;
|
||||
// import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
|
||||
|
||||
/**
|
||||
* Spring Boot auto-configuration for simple properties-based discovery client.
|
||||
*
|
||||
@@ -42,7 +41,8 @@ import org.springframework.core.annotation.Order;
|
||||
|
||||
@Configuration
|
||||
@AutoConfigureBefore(NoopDiscoveryClientAutoConfiguration.class)
|
||||
public class SimpleDiscoveryClientAutoConfiguration {
|
||||
public class SimpleDiscoveryClientAutoConfiguration
|
||||
implements ApplicationListener<WebServerInitializedEvent> {
|
||||
|
||||
@Autowired(required = false)
|
||||
private ServerProperties server;
|
||||
@@ -56,9 +56,12 @@ public class SimpleDiscoveryClientAutoConfiguration {
|
||||
@Autowired
|
||||
private InetUtils inet;
|
||||
|
||||
private int port = 0;
|
||||
|
||||
private SimpleDiscoveryProperties simple = new SimpleDiscoveryProperties();
|
||||
|
||||
@Bean
|
||||
public SimpleDiscoveryProperties simpleDiscoveryProperties() {
|
||||
SimpleDiscoveryProperties simple = new SimpleDiscoveryProperties();
|
||||
simple.getLocal().setServiceId(this.serviceId);
|
||||
simple.getLocal()
|
||||
.setUri(URI.create(
|
||||
@@ -74,15 +77,9 @@ public class SimpleDiscoveryClientAutoConfiguration {
|
||||
}
|
||||
|
||||
private int findPort() {
|
||||
// FIXME: is what is the boot 2.0 equiv?
|
||||
/*
|
||||
* if (ClassUtils.isPresent(
|
||||
* "org.springframework.boot.context.embedded.EmbeddedWebApplicationContext",
|
||||
* null)) { if (this.context instanceof EmbeddedWebApplicationContext) {
|
||||
* EmbeddedServletContainer container = ((EmbeddedWebApplicationContext)
|
||||
* this.context) .getEmbeddedServletContainer(); if (container != null) { return
|
||||
* container.getPort(); } } }
|
||||
*/
|
||||
if (port > 0) {
|
||||
return port;
|
||||
}
|
||||
if (this.server != null && this.server.getPort() != null
|
||||
&& this.server.getPort() > 0) {
|
||||
return this.server.getPort();
|
||||
@@ -90,4 +87,15 @@ public class SimpleDiscoveryClientAutoConfiguration {
|
||||
return 8080;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
|
||||
this.port = webServerInitializedEvent.getWebServer().getPort();
|
||||
if (this.port > 0) {
|
||||
simple.getLocal()
|
||||
.setUri(URI.create("http://"
|
||||
+ this.inet.findFirstNonLoopbackHostInfo().getHostname() + ":"
|
||||
+ this.port));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.client.discovery.simple;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ReactiveSimpleDiscoveryPropertiesAutoConfigurationTests.Config.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "spring.main.web-application-type=reactive")
|
||||
public class ReactiveSimpleDiscoveryPropertiesAutoConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private SimpleDiscoveryProperties discoveryProperties;
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void testPort() {
|
||||
then(discoveryProperties.getLocal().getPort()).isEqualTo(port);
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration
|
||||
public static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.client.discovery.simple;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ServletSimpleDiscoveryPropertiesAutoConfigurationTests.Config.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class ServletSimpleDiscoveryPropertiesAutoConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private SimpleDiscoveryProperties discoveryProperties;
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void testPort() {
|
||||
then(discoveryProperties.getLocal().getPort()).isEqualTo(port);
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration
|
||||
public static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user