Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables hystrix monitoring of multiple services on a single server. #661

Merged
merged 1 commit into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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