Commit 03a00503 by Micheal Swiggs

Enables hystrix monitoring of multiple services on a single server.

parent f46e8e8e
......@@ -49,10 +49,12 @@ public class EurekaInstanceDiscovery extends CommonsInstanceDiscovery {
private static final String EUREKA_DEFAULT_CLUSTER_NAME_EXPRESSION = "appName";
private final EurekaClient eurekaClient;
private final boolean combineHostPort;
public EurekaInstanceDiscovery(TurbineProperties turbineProperties, EurekaClient eurekaClient) {
super(turbineProperties, EUREKA_DEFAULT_CLUSTER_NAME_EXPRESSION);
this.eurekaClient = eurekaClient;
this.combineHostPort = turbineProperties.isCombineHostPort();
}
/**
......@@ -98,10 +100,12 @@ public class EurekaInstanceDiscovery extends CommonsInstanceDiscovery {
*/
Instance marshall(InstanceInfo instanceInfo) {
String hostname = instanceInfo.getHostName();
String port = String.valueOf(instanceInfo.getPort());
String cluster = getClusterName(instanceInfo);
Boolean status = parseInstanceStatus(instanceInfo.getStatus());
if (hostname != null && cluster != null && status != null) {
Instance instance = new Instance(hostname, cluster, status);
String hostPart = combineHostPort ? hostname+":"+port : hostname;
Instance instance = new Instance(hostPart, cluster, status);
// add metadata
Map<String, String> metadata = instanceInfo.getMetadata();
......@@ -126,6 +130,11 @@ public class EurekaInstanceDiscovery extends CommonsInstanceDiscovery {
if (securePortEnabled) {
instance.getAttributes().put("securePort", String.valueOf(instanceInfo.getSecurePort()));
}
if (combineHostPort) {
String fusedHostPort = securePortEnabled ? hostname+":"+String.valueOf(instanceInfo.getSecurePort()) : hostPart ;
instance.getAttributes().put("fusedHostPort", fusedHostPort);
}
return instance;
}
else {
......
......@@ -87,6 +87,10 @@ public class SpringClusterMonitor extends AggregateClusterMonitor {
scheme = "http";
}
if (host.getAttributes().containsKey("fusedHostPort")) {
return String.format("%s://%s/%s", scheme, host.getAttributes().get("fusedHostPort"), url);
}
// determine if to insert port
String insertPortKey = "turbine.instanceInsertPort." + host.getCluster();
DynamicStringProperty insertPortProp = DynamicPropertyFactory.getInstance()
......
......@@ -35,6 +35,8 @@ public class TurbineProperties {
private String appConfig;
private boolean combineHostPort = false;
public List<String> getAppConfigList() {
if (!StringUtils.hasText(appConfig)) {
return null;
......
......@@ -43,6 +43,52 @@ public class EurekaInstanceDiscoveryTest {
}
@Test
public void testSecureCombineHostPort() {
turbineProperties.setCombineHostPort(true);
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.marshall(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 testCombineHostPort() {
turbineProperties.setCombineHostPort(true);
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.marshall(instanceInfo);
assertEquals("hostname is wrong", hostName+":"+port, instance.getHostname());
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);
String clusterName = discovery.getClusterName(instanceInfo);
assertEquals("clusterName is wrong", appName.toUpperCase(), clusterName);
}
@Test
public void testGetClusterName() {
EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(
turbineProperties, eurekaClient);
......
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