Skip to content

Commit

Permalink
Enables hystrix monitoring of multiple services on a single server.
Browse files Browse the repository at this point in the history
  • Loading branch information
micheal-swiggs committed Nov 22, 2015
1 parent f46e8e8 commit 03a0050
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -98,10 +100,12 @@ protected List<Instance> getInstancesForApp(String serviceId) throws Exception {
*/
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();
Expand All @@ -126,6 +130,11 @@ Instance marshall(InstanceInfo instanceInfo) {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public String getUrlPath(Instance host) {
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class TurbineProperties {

private String appConfig;

private boolean combineHostPort = false;

public List<String> getAppConfigList() {
if (!StringUtils.hasText(appConfig)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,52 @@ public void setUp() throws Exception {
builder = InstanceInfo.Builder.newBuilder();
}

@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(
Expand Down

0 comments on commit 03a0050

Please sign in to comment.