Commit b10bf9c3 by Spencer Gibb

Use eureka securePort if enabled for turbine http aggregation.

fixes gh-417
parent 7cf0fa48
...@@ -146,20 +146,30 @@ public class EurekaInstanceDiscovery implements InstanceDiscovery { ...@@ -146,20 +146,30 @@ public class EurekaInstanceDiscovery implements InstanceDiscovery {
Boolean status = parseInstanceStatus(instanceInfo.getStatus()); Boolean status = parseInstanceStatus(instanceInfo.getStatus());
if (hostname != null && cluster != null && status != null) { if (hostname != null && cluster != null && status != null) {
Instance instance = new Instance(hostname, cluster, status); Instance instance = new Instance(hostname, cluster, status);
// add metadata
Map<String, String> metadata = instanceInfo.getMetadata(); Map<String, String> metadata = instanceInfo.getMetadata();
if (metadata != null) { if (metadata != null) {
instance.getAttributes().putAll(metadata); instance.getAttributes().putAll(metadata);
} }
// add amazon metadata
String asgName = instanceInfo.getASGName(); String asgName = instanceInfo.getASGName();
if (asgName != null) { if (asgName != null) {
instance.getAttributes().put("asg", asgName); instance.getAttributes().put("asg", asgName);
} }
instance.getAttributes().put("port", String.valueOf(instanceInfo.getPort()));
DataCenterInfo dcInfo = instanceInfo.getDataCenterInfo(); DataCenterInfo dcInfo = instanceInfo.getDataCenterInfo();
if (dcInfo != null && dcInfo.getName().equals(DataCenterInfo.Name.Amazon)) { if (dcInfo != null && dcInfo.getName().equals(DataCenterInfo.Name.Amazon)) {
AmazonInfo amznInfo = (AmazonInfo) dcInfo; AmazonInfo amznInfo = (AmazonInfo) dcInfo;
instance.getAttributes().putAll(amznInfo.getMetadata()); instance.getAttributes().putAll(amznInfo.getMetadata());
} }
// add ports
instance.getAttributes().put("port", String.valueOf(instanceInfo.getPort()));
boolean securePortEnabled = instanceInfo.isPortEnabled(InstanceInfo.PortType.SECURE);
if (securePortEnabled) {
instance.getAttributes().put("securePort", String.valueOf(instanceInfo.getSecurePort()));
}
return instance; return instance;
} }
else { else {
......
...@@ -61,6 +61,8 @@ public class SpringClusterMonitor extends AggregateClusterMonitor { ...@@ -61,6 +61,8 @@ public class SpringClusterMonitor extends AggregateClusterMonitor {
throw new RuntimeException( throw new RuntimeException(
"Host must have cluster name in order to use ClusterConfigBasedUrlClosure"); "Host must have cluster name in order to use ClusterConfigBasedUrlClosure");
} }
// find url
String key = "turbine.instanceUrlSuffix." + host.getCluster(); String key = "turbine.instanceUrlSuffix." + host.getCluster();
DynamicStringProperty urlClosureConfig = DynamicPropertyFactory.getInstance() DynamicStringProperty urlClosureConfig = DynamicPropertyFactory.getInstance()
.getStringProperty(key, null); .getStringProperty(key, null);
...@@ -73,6 +75,19 @@ public class SpringClusterMonitor extends AggregateClusterMonitor { ...@@ -73,6 +75,19 @@ public class SpringClusterMonitor extends AggregateClusterMonitor {
+ urlClosureConfig.getName() + " or " + urlClosureConfig.getName() + " or "
+ this.defaultUrlClosureConfig.getName() + " must be set"); + this.defaultUrlClosureConfig.getName() + " must be set");
} }
// find port and scheme
String port;
String scheme;
if (host.getAttributes().containsKey("securePort")) {
port = host.getAttributes().get("securePort");
scheme = "https";
} else {
port = host.getAttributes().get("port");
scheme = "http";
}
// determine if to insert port
String insertPortKey = "turbine.instanceInsertPort." + host.getCluster(); String insertPortKey = "turbine.instanceInsertPort." + host.getCluster();
DynamicStringProperty insertPortProp = DynamicPropertyFactory.getInstance() DynamicStringProperty insertPortProp = DynamicPropertyFactory.getInstance()
.getStringProperty(insertPortKey, null); .getStringProperty(insertPortKey, null);
...@@ -83,18 +98,22 @@ public class SpringClusterMonitor extends AggregateClusterMonitor { ...@@ -83,18 +98,22 @@ public class SpringClusterMonitor extends AggregateClusterMonitor {
else { else {
insertPort = Boolean.parseBoolean(insertPortProp.get()); insertPort = Boolean.parseBoolean(insertPortProp.get());
} }
// format url with port
if (insertPort) { if (insertPort) {
if (url.startsWith("/")) { if (url.startsWith("/")) {
url = url.substring(1); url = url.substring(1);
} }
if (!host.getAttributes().containsKey("port")) { if (port == null) {
throw new RuntimeException( throw new RuntimeException(
"Configured to use port, but port is not in host attributes"); "Configured to use port, but port or securePort is not in host attributes");
} }
return String.format("http://%s:%s/%s", host.getHostname(), host
.getAttributes().get("port"), url); return String.format("%s://%s:%s/%s", scheme, host.getHostname(), port, url);
} }
return "http://" + host.getHostname() + url;
//format url without port
return scheme + "://" + host.getHostname() + url;
} }
}; };
......
...@@ -19,6 +19,8 @@ package org.springframework.cloud.netflix.turbine; ...@@ -19,6 +19,8 @@ package org.springframework.cloud.netflix.turbine;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import com.netflix.turbine.discovery.Instance;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
...@@ -30,26 +32,74 @@ import static org.junit.Assert.assertEquals; ...@@ -30,26 +32,74 @@ import static org.junit.Assert.assertEquals;
*/ */
public class EurekaInstanceDiscoveryTest { public class EurekaInstanceDiscoveryTest {
private EurekaClient eurekaClient;
private TurbineProperties turbineProperties;
private InstanceInfo.Builder builder;
@Before
public void setUp() throws Exception {
eurekaClient = mock(EurekaClient.class);
turbineProperties = new TurbineProperties();
builder = InstanceInfo.Builder.newBuilder();
}
@Test @Test
public void testGetClusterName() { public void testGetClusterName() {
EurekaClient eurekaClient = mock(EurekaClient.class);
String appName = "testAppName";
EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery( EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(
new TurbineProperties(), eurekaClient); turbineProperties, eurekaClient);
InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName(appName) String appName = "testAppName";
InstanceInfo instanceInfo = builder.setAppName(appName)
.build(); .build();
String clusterName = discovery.getClusterName(instanceInfo); String clusterName = discovery.getClusterName(instanceInfo);
assertEquals("clusterName is wrong", appName.toUpperCase(), clusterName); assertEquals("clusterName is wrong", appName.toUpperCase(), clusterName);
} }
@Test @Test
public void testGetPort() {
EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(
turbineProperties, eurekaClient);
String appName = "testAppName";
int port = 8080;
String hostName = "myhost";
InstanceInfo instanceInfo = builder.setAppName(appName)
.setHostName(hostName)
.setPort(port)
.build();
Instance instance = discovery.marshallInstanceInfo(instanceInfo);
assertEquals("port is wrong", String.valueOf(port), instance.getAttributes().get("port"));
String urlPath = SpringClusterMonitor.ClusterConfigBasedUrlClosure.getUrlPath(instance);
assertEquals("url is wrong", "http://"+hostName+":"+port+"/hystrix.stream", urlPath);
}
@Test
public void testGetSecurePort() {
EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(
turbineProperties, eurekaClient);
String appName = "testAppName";
int port = 8080;
int securePort = 8443;
String hostName = "myhost";
InstanceInfo instanceInfo = builder.setAppName(appName)
.setHostName(hostName)
.setPort(port)
.setSecurePort(securePort)
.enablePort(InstanceInfo.PortType.SECURE, true)
.build();
Instance instance = discovery.marshallInstanceInfo(instanceInfo);
assertEquals("port is wrong", String.valueOf(port), instance.getAttributes().get("port"));
assertEquals("securePort is wrong", String.valueOf(securePort), instance.getAttributes().get("securePort"));
String urlPath = SpringClusterMonitor.ClusterConfigBasedUrlClosure.getUrlPath(instance);
assertEquals("url is wrong", "https://"+hostName+":"+securePort+"/hystrix.stream", urlPath);
}
@Test
public void testGetClusterNameCustomExpression() { public void testGetClusterNameCustomExpression() {
EurekaClient eurekaClient = mock(EurekaClient.class);
TurbineProperties turbineProperties = new TurbineProperties();
turbineProperties.setClusterNameExpression("aSGName"); turbineProperties.setClusterNameExpression("aSGName");
EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(turbineProperties, eurekaClient); EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(turbineProperties, eurekaClient);
String asgName = "myAsgName"; String asgName = "myAsgName";
InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder() InstanceInfo instanceInfo = builder
.setAppName("testApp").setASGName(asgName).build(); .setAppName("testApp").setASGName(asgName).build();
String clusterName = discovery.getClusterName(instanceInfo); String clusterName = discovery.getClusterName(instanceInfo);
assertEquals("clusterName is wrong", asgName, clusterName); assertEquals("clusterName is wrong", asgName, clusterName);
...@@ -57,12 +107,10 @@ public class EurekaInstanceDiscoveryTest { ...@@ -57,12 +107,10 @@ public class EurekaInstanceDiscoveryTest {
@Test @Test
public void testGetClusterNameInstanceMetadataMapExpression() { public void testGetClusterNameInstanceMetadataMapExpression() {
EurekaClient eurekaClient = mock(EurekaClient.class);
TurbineProperties turbineProperties = new TurbineProperties();
turbineProperties.setClusterNameExpression("metadata['cluster']"); turbineProperties.setClusterNameExpression("metadata['cluster']");
EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(turbineProperties, eurekaClient); EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(turbineProperties, eurekaClient);
String metadataProperty = "myCluster"; String metadataProperty = "myCluster";
InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder() InstanceInfo instanceInfo = builder
.setAppName("testApp").add("cluster", metadataProperty).build(); .setAppName("testApp").add("cluster", metadataProperty).build();
String clusterName = discovery.getClusterName(instanceInfo); String clusterName = discovery.getClusterName(instanceInfo);
assertEquals("clusterName is wrong", metadataProperty, clusterName); assertEquals("clusterName is wrong", metadataProperty, clusterName);
......
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