Default status page and health URLs if possible and where needed

If the management.port is explicitly set to something other than the
server.port, we can use it to set the status page and health URL
default values. (They can still be overridden by config properties.)

If management.port=0 there's not much we can do because we don't know
the port number yet when the instance config is created.

Fixes gh-425
This commit is contained in:
Dave Syer
2015-11-30 09:29:09 +00:00
parent f2fee74880
commit 1129872061
3 changed files with 47 additions and 3 deletions

View File

@@ -46,6 +46,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig;
@@ -73,6 +74,12 @@ public class EurekaClientAutoConfiguration {
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
int nonSecurePort;
@Value("${management.port:${MANAGEMENT_PORT:${PORT:8080}}}")
int managementPort;
@Value("${eureka.instance.hostname:${EUREKA_INSTANCE_HOSTNAME:}}")
String hostname;
@Autowired
ConfigurableEnvironment env;
@@ -93,6 +100,16 @@ public class EurekaClientAutoConfiguration {
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean();
instance.setNonSecurePort(this.nonSecurePort);
instance.setInstanceId(getDefaultInstanceId(this.env));
if (this.managementPort != this.nonSecurePort && this.managementPort != 0) {
if (StringUtils.hasText(this.hostname)) {
instance.setHostname(this.hostname);
}
String scheme = instance.getSecurePortEnabled() ? "https" : "http";
instance.setStatusPageUrl(scheme + "://" + instance.getHostname() + ":"
+ this.managementPort + instance.getStatusPageUrlPath());
instance.setHealthCheckUrl(scheme + "://" + instance.getHostname() + ":"
+ this.managementPort + instance.getHealthCheckUrlPath());
}
return instance;
}

View File

@@ -21,12 +21,14 @@ import org.junit.Test;
import org.springframework.aop.scope.ScopedProxyFactoryBean;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
/**
@@ -68,6 +70,28 @@ public class EurekaClientAutoConfigurationTests {
this.context.getBeanDefinition("eurekaClient").getFactoryMethodName());
}
@Test
public void managementPort() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",
"management.port=9999");
setupContext(RefreshAutoConfiguration.class);
EurekaInstanceConfigBean instance = this.context
.getBean(EurekaInstanceConfigBean.class);
assertTrue("Wrong status page: " + instance.getStatusPageUrl(),
instance.getStatusPageUrl().contains("9999"));
}
@Test
public void hostname() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",
"management.port=9999", "eureka.instance.hostname=foo");
setupContext(RefreshAutoConfiguration.class);
EurekaInstanceConfigBean instance = this.context
.getBean(EurekaInstanceConfigBean.class);
assertTrue("Wrong status page: " + instance.getStatusPageUrl(),
instance.getStatusPageUrl().contains("foo"));
}
@Test
public void refreshScopedBeans() {
setupContext(RefreshAutoConfiguration.class);

View File

@@ -56,16 +56,19 @@ public class SidecarConfiguration {
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
private int serverPort = 8080;
@Value("${management.port:${MANAGEMENT_PORT:${PORT:8080}}}")
private int managementPort = 8080;
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
EurekaInstanceConfigBean config = new EurekaInstanceConfigBean();
int port = sidecarProperties.getPort();
int port = this.sidecarProperties.getPort();
config.setNonSecurePort(port);
String scheme = config.getSecurePortEnabled() ? "https" : "http";
config.setStatusPageUrl(scheme + "://" + config.getHostname() + ":"
+ this.serverPort + config.getStatusPageUrlPath());
+ this.managementPort + config.getStatusPageUrlPath());
config.setHealthCheckUrl(scheme + "://" + config.getHostname() + ":"
+ this.serverPort + config.getHealthCheckUrlPath());
+ this.managementPort + config.getHealthCheckUrlPath());
config.setHomePageUrl(scheme + "://" + config.getHostname() + ":" + port
+ config.getHomePageUrlPath());
return config;