Browse Source

Add management.context-path to status & health url

fixes gh-1795
pull/6/head
Roman Rodov 8 years ago committed by Spencer Gibb
parent
commit
e130ec03ad
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 66
      spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java
  2. 48
      spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfigurationTests.java

66
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java

@ -22,9 +22,10 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.net.MalformedURLException;
import java.net.URL;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
@ -55,6 +56,7 @@ import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.ApplicationInfoManager;
@ -64,7 +66,6 @@ import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient.DiscoveryClientOptionalArgs; import com.netflix.discovery.DiscoveryClient.DiscoveryClientOptionalArgs;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig; import com.netflix.discovery.EurekaClientConfig;
import static org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceId; import static org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceId;
/** /**
@ -84,17 +85,15 @@ import static org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceI
@AutoConfigureAfter(name = "org.springframework.cloud.autoconfigure.RefreshAutoConfiguration") @AutoConfigureAfter(name = "org.springframework.cloud.autoconfigure.RefreshAutoConfiguration")
public class EurekaClientAutoConfiguration { public class EurekaClientAutoConfiguration {
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
private int nonSecurePort;
@Value("${management.port:${MANAGEMENT_PORT:${server.port:${SERVER_PORT:${PORT:8080}}}}}")
private int managementPort;
@Autowired
private ConfigurableEnvironment env; private ConfigurableEnvironment env;
@Autowired(required = false) @Autowired(required = false)
private HealthCheckHandler healthCheckHandler; private HealthCheckHandler healthCheckHandler;
private RelaxedPropertyResolver propertyResolver;
public EurekaClientAutoConfiguration(ConfigurableEnvironment env) {
this.env = env;
this.propertyResolver = new RelaxedPropertyResolver(env);
}
@Bean @Bean
public HasFeatures eurekaFeature() { public HasFeatures eurekaFeature() {
@ -105,7 +104,7 @@ public class EurekaClientAutoConfiguration {
@ConditionalOnMissingBean(value = EurekaClientConfig.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = EurekaClientConfig.class, search = SearchStrategy.CURRENT)
public EurekaClientConfigBean eurekaClientConfigBean() { public EurekaClientConfigBean eurekaClientConfigBean() {
EurekaClientConfigBean client = new EurekaClientConfigBean(); EurekaClientConfigBean client = new EurekaClientConfigBean();
if ("bootstrap".equals(this.env.getProperty("spring.config.name"))) { if ("bootstrap".equals(propertyResolver.getProperty("spring.config.name"))) {
// We don't register during bootstrap by default, but there will be another // We don't register during bootstrap by default, but there will be another
// chance later. // chance later.
client.setRegisterWithEureka(false); client.setRegisterWithEureka(false);
@ -115,21 +114,27 @@ public class EurekaClientAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils) { public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils) throws MalformedURLException {
RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(env, "eureka.instance."); PropertyResolver eurekaPropertyResolver = new RelaxedPropertyResolver(this.env, "eureka.instance.");
String hostname = relaxedPropertyResolver.getProperty("hostname"); String hostname = eurekaPropertyResolver.getProperty("hostname");
boolean preferIpAddress = Boolean.parseBoolean(relaxedPropertyResolver.getProperty("preferIpAddress"));
boolean preferIpAddress = Boolean.parseBoolean(eurekaPropertyResolver.getProperty("preferIpAddress"));
int nonSecurePort = Integer.valueOf(propertyResolver.getProperty("server.port", propertyResolver.getProperty("port", "8080")));
int managementPort = Integer.valueOf(propertyResolver.getProperty("management.port", String.valueOf(nonSecurePort)));
String managementContextPath = propertyResolver.getProperty("management.contextPath", propertyResolver.getProperty("server.contextPath", "/"));
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils); EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);
instance.setNonSecurePort(this.nonSecurePort); instance.setNonSecurePort(nonSecurePort);
instance.setInstanceId(getDefaultInstanceId(this.env)); instance.setInstanceId(getDefaultInstanceId(propertyResolver));
instance.setPreferIpAddress(preferIpAddress); instance.setPreferIpAddress(preferIpAddress);
if (managementPort != nonSecurePort && managementPort != 0) {
if (this.managementPort != this.nonSecurePort && this.managementPort != 0) {
if (StringUtils.hasText(hostname)) { if (StringUtils.hasText(hostname)) {
instance.setHostname(hostname); instance.setHostname(hostname);
} }
String statusPageUrlPath = relaxedPropertyResolver.getProperty("statusPageUrlPath"); String statusPageUrlPath = eurekaPropertyResolver.getProperty("statusPageUrlPath");
String healthCheckUrlPath = relaxedPropertyResolver.getProperty("healthCheckUrlPath"); String healthCheckUrlPath = eurekaPropertyResolver.getProperty("healthCheckUrlPath");
if (!managementContextPath.endsWith("/")) {
managementContextPath = managementContextPath + "/";
}
if (StringUtils.hasText(statusPageUrlPath)) { if (StringUtils.hasText(statusPageUrlPath)) {
instance.setStatusPageUrlPath(statusPageUrlPath); instance.setStatusPageUrlPath(statusPageUrlPath);
} }
@ -137,17 +142,15 @@ public class EurekaClientAutoConfiguration {
instance.setHealthCheckUrlPath(healthCheckUrlPath); instance.setHealthCheckUrlPath(healthCheckUrlPath);
} }
String scheme = instance.getSecurePortEnabled() ? "https" : "http"; String scheme = instance.getSecurePortEnabled() ? "https" : "http";
instance.setStatusPageUrl(scheme + "://" + instance.getHostname() + ":" URL base = new URL(scheme, instance.getHostname(), managementPort, managementContextPath);
+ this.managementPort + instance.getStatusPageUrlPath()); instance.setStatusPageUrl(new URL(base, StringUtils.trimLeadingCharacter(instance.getStatusPageUrlPath(), '/')).toString());
instance.setHealthCheckUrl(scheme + "://" + instance.getHostname() + ":" instance.setHealthCheckUrl(new URL(base, StringUtils.trimLeadingCharacter(instance.getHealthCheckUrlPath(), '/')).toString());
+ this.managementPort + instance.getHealthCheckUrlPath());
} }
return instance; return instance;
} }
@Bean @Bean
public DiscoveryClient discoveryClient(EurekaInstanceConfig config, public DiscoveryClient discoveryClient(EurekaInstanceConfig config, EurekaClient client) {
EurekaClient client) {
return new EurekaDiscoveryClient(config, client); return new EurekaDiscoveryClient(config, client);
} }
@ -192,8 +195,7 @@ public class EurekaClientAutoConfiguration {
@Bean(destroyMethod = "shutdown") @Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
public EurekaClient eurekaClient(ApplicationInfoManager manager, public EurekaClient eurekaClient(ApplicationInfoManager manager, EurekaClientConfig config) {
EurekaClientConfig config) {
return new CloudEurekaClient(manager, config, this.optionalArgs, return new CloudEurekaClient(manager, config, this.optionalArgs,
this.context); this.context);
} }
@ -221,8 +223,7 @@ public class EurekaClientAutoConfiguration {
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
@org.springframework.cloud.context.config.annotation.RefreshScope @org.springframework.cloud.context.config.annotation.RefreshScope
@Lazy @Lazy
public EurekaClient eurekaClient(ApplicationInfoManager manager, public EurekaClient eurekaClient(ApplicationInfoManager manager, EurekaClientConfig config, EurekaInstanceConfig instance) {
EurekaClientConfig config, EurekaInstanceConfig instance) {
manager.getInfo(); // force initialization manager.getInfo(); // force initialization
return new CloudEurekaClient(manager, config, this.optionalArgs, return new CloudEurekaClient(manager, config, this.optionalArgs,
this.context); this.context);
@ -232,8 +233,7 @@ public class EurekaClientAutoConfiguration {
@ConditionalOnMissingBean(value = ApplicationInfoManager.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = ApplicationInfoManager.class, search = SearchStrategy.CURRENT)
@org.springframework.cloud.context.config.annotation.RefreshScope @org.springframework.cloud.context.config.annotation.RefreshScope
@Lazy @Lazy
public ApplicationInfoManager eurekaApplicationInfoManager( public ApplicationInfoManager eurekaApplicationInfoManager(EurekaInstanceConfig config) {
EurekaInstanceConfig config) {
InstanceInfo instanceInfo = new InstanceInfoFactory().create(config); InstanceInfo instanceInfo = new InstanceInfoFactory().create(config);
return new ApplicationInfoManager(config, instanceInfo); return new ApplicationInfoManager(config, instanceInfo);
} }

48
spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfigurationTests.java

@ -113,6 +113,54 @@ public class EurekaClientAutoConfigurationTests {
instance.getHealthCheckUrl().contains("/myHealthCheck")); instance.getHealthCheckUrl().contains("/myHealthCheck"));
} }
@Test
public void statusPageUrlPathAndManagementPortAndContextPath() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",
"management.port=9999", "management.contextPath=/manage",
"eureka.instance.statusPageUrlPath=/myStatusPage");
setupContext(RefreshAutoConfiguration.class);
EurekaInstanceConfigBean instance = this.context
.getBean(EurekaInstanceConfigBean.class);
assertTrue("Wrong status page: " + instance.getStatusPageUrl(),
instance.getStatusPageUrl().endsWith(":9999/manage/myStatusPage"));
}
@Test
public void healthCheckUrlPathAndManagementPortAndContextPath() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",
"management.port=9999", "management.contextPath=/manage",
"eureka.instance.healthCheckUrlPath=/myHealthCheck");
setupContext(RefreshAutoConfiguration.class);
EurekaInstanceConfigBean instance = this.context
.getBean(EurekaInstanceConfigBean.class);
assertTrue("Wrong health check: " + instance.getHealthCheckUrl(),
instance.getHealthCheckUrl().endsWith(":9999/manage/myHealthCheck"));
}
@Test
public void statusPageUrlPathAndManagementPortAndContextPathKebobCase() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",
"management.port=9999", "management.context-path=/manage",
"eureka.instance.statusPageUrlPath=/myStatusPage");
setupContext(RefreshAutoConfiguration.class);
EurekaInstanceConfigBean instance = this.context
.getBean(EurekaInstanceConfigBean.class);
assertTrue("Wrong status page: " + instance.getStatusPageUrl(),
instance.getStatusPageUrl().endsWith(":9999/manage/myStatusPage"));
}
@Test
public void healthCheckUrlPathAndManagementPortAndContextPathKebobCase() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",
"management.port=9999", "management.context-path=/manage",
"eureka.instance.healthCheckUrlPath=/myHealthCheck");
setupContext(RefreshAutoConfiguration.class);
EurekaInstanceConfigBean instance = this.context
.getBean(EurekaInstanceConfigBean.class);
assertTrue("Wrong health check: " + instance.getHealthCheckUrl(),
instance.getHealthCheckUrl().endsWith(":9999/manage/myHealthCheck"));
}
@Test @Test
public void statusPageUrlPathAndManagementPortKabobCase() { public void statusPageUrlPathAndManagementPortKabobCase() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989", EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",

Loading…
Cancel
Save