Unverified Commit 8af3ae8a by Ryan Baxter Committed by GitHub

Use management metadata in sidecar. Fixes 2859. (#3028)

parent ab7b0fb3
......@@ -40,7 +40,7 @@ public class DefaultManagementMetadataProvider implements ManagementMetadataProv
return port != null && port == RANDOM_PORT;
}
private String getHealthCheckUrl(EurekaInstanceConfigBean instance, int serverPort, String serverContextPath,
protected String getHealthCheckUrl(EurekaInstanceConfigBean instance, int serverPort, String serverContextPath,
String managementContextPath, Integer managementPort, boolean isSecure) {
String healthCheckUrlPath = instance.getHealthCheckUrlPath();
String healthCheckUrl = getUrl(instance, serverPort, serverContextPath, managementContextPath,
......
......@@ -20,14 +20,17 @@ import static org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceI
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.metadata.DefaultManagementMetadataProvider;
import org.springframework.cloud.netflix.eureka.metadata.ManagementMetadata;
import org.springframework.cloud.netflix.eureka.metadata.ManagementMetadataProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
......@@ -36,8 +39,7 @@ import org.springframework.util.StringUtils;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.discovery.EurekaClientConfig;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
/**
* Sidecar Configuration that setting up {@link com.netflix.appinfo.EurekaInstanceConfig}.
......@@ -80,8 +82,17 @@ public class SidecarConfiguration {
@Autowired
private InetUtils inetUtils;
@Value("${management.port:${MANAGEMENT_PORT:${server.port:${SERVER_PORT:${PORT:8080}}}}}")
private int managementPort = 8080;
@Value(value = "${management.port:${MANAGEMENT_PORT:#{null}}}")
private Integer managementPort;
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
private int serverPort = 8080;
@Value("${management.context-path:${MANAGEMENT_CONTEXT_PATH:#{null}}}")
private String managementContextPath;
@Value("${server.context-path:${SERVER_CONTEXT_PATH:/}}")
private String serverContextPath = "/";
@Value("${eureka.instance.hostname:${EUREKA_INSTANCE_HOSTNAME:}}")
private String hostname;
......@@ -90,7 +101,13 @@ public class SidecarConfiguration {
private ConfigurableEnvironment env;
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
@ConditionalOnMissingBean
public ManagementMetadataProvider serviceManagementMetadataProvider() {
return new DefaultManagementMetadataProvider();
}
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean(ManagementMetadataProvider managementMetadataProvider) {
EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(env, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name");
......@@ -114,10 +131,20 @@ public class SidecarConfiguration {
config.setIpAddress(ipAddress);
}
String scheme = config.getSecurePortEnabled() ? "https" : "http";
config.setStatusPageUrl(scheme + "://" + config.getHostname() + ":"
+ this.managementPort + config.getStatusPageUrlPath());
config.setHealthCheckUrl(scheme + "://" + config.getHostname() + ":"
+ this.managementPort + config.getHealthCheckUrlPath());
ManagementMetadata metadata = managementMetadataProvider.get(config, serverPort,
serverContextPath, managementContextPath, managementPort);
if(metadata != null) {
config.setStatusPageUrl(metadata.getStatusPageUrl());
config.setHealthCheckUrl(metadata.getHealthCheckUrl());
if(config.isSecurePortEnabled()) {
config.setSecureHealthCheckUrl(metadata.getSecureHealthCheckUrl());
}
Map<String, String> metadataMap = config.getMetadataMap();
if (metadataMap.get("management.port") == null) {
metadataMap.put("management.port", String.valueOf(metadata.getManagementPort()));
}
}
config.setHomePageUrl(scheme + "://" + config.getHostname() + ":" + port
+ config.getHomePageUrlPath());
return config;
......
......@@ -18,6 +18,11 @@ package org.springframework.cloud.netflix.sidecar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.metadata.DefaultManagementMetadataProvider;
import org.springframework.cloud.netflix.eureka.metadata.ManagementMetadata;
import org.springframework.cloud.netflix.eureka.metadata.ManagementMetadataProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
......@@ -29,4 +34,25 @@ public class SidecarApplication {
SpringApplication.run(SidecarApplication.class, args);
}
@Bean
public ManagementMetadataProvider managementMetadataProvider() {
//The default management metadata provider checks for random ports, we dont care about this in tests
return new DefaultManagementMetadataProvider() {
@Override
public ManagementMetadata get(EurekaInstanceConfigBean instance, int serverPort, String serverContextPath, String managementContextPath, Integer managementPort) {
String healthCheckUrl = getHealthCheckUrl(instance, serverPort, serverContextPath,
managementContextPath, managementPort, false);
String statusPageUrl = getStatusPageUrl(instance, serverPort, serverContextPath,
managementContextPath, managementPort);
ManagementMetadata metadata = new ManagementMetadata(healthCheckUrl, statusPageUrl, managementPort == null ? serverPort : managementPort);
if(instance.isSecurePortEnabled()) {
metadata.setSecureHealthCheckUrl(getHealthCheckUrl(instance, serverPort, serverContextPath,
managementContextPath, managementPort, true));
}
return metadata;
}
};
}
}
......@@ -80,4 +80,35 @@ public class SidecarApplicationTests {
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SidecarApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"spring.application.name=mytest", "spring.cloud.client.hostname=mhhost", "spring.application.instance_id=1",
"eureka.instance.hostname=mhhost1", "sidecar.hostname=mhhost2", "sidecar.port=7000", "sidecar.ipAddress=127.0.0.1",
"management.context-path=/foo"})
public static class ManagementContextPathStatusAndHealthCheckUrls {
@Autowired
EurekaInstanceConfigBean config;
@Test
public void testStatusAndHealthCheckUrls() {
assertThat(this.config.getStatusPageUrl(), equalTo("http://mhhost2:0/foo/info"));
assertThat(this.config.getHealthCheckUrl(), equalTo("http://mhhost2:0/foo/health"));
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SidecarApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"spring.application.name=mytest", "spring.cloud.client.hostname=mhhost", "spring.application.instance_id=1",
"eureka.instance.hostname=mhhost1", "sidecar.hostname=mhhost2", "sidecar.port=7000", "sidecar.ipAddress=127.0.0.1",
"server.context-path=/foo"})
public static class ServerContextPathStatusAndHealthCheckUrls {
@Autowired
EurekaInstanceConfigBean config;
@Test
public void testStatusAndHealthCheckUrls() {
assertThat(this.config.getStatusPageUrl(), equalTo("http://mhhost2:0/foo/info"));
assertThat(this.config.getHealthCheckUrl(), equalTo("http://mhhost2:0/foo/health"));
}
}
}
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