Commit 11298720 by Dave Syer

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
parent f2fee748
......@@ -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;
}
......
......@@ -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;
/**
......@@ -69,6 +71,28 @@ public class EurekaClientAutoConfigurationTests {
}
@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);
assertEquals(ScopedProxyFactoryBean.class.getName(),
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment