Skip to content

Commit

Permalink
Merge pull request Netflix#396 from opuneet/issue394
Browse files Browse the repository at this point in the history
Adding in eureka based host supplier - issue 394
  • Loading branch information
opuneet committed Sep 10, 2013
2 parents 5a0dd4f + d0777af commit 976160e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
13 changes: 13 additions & 0 deletions astyanax-contrib/conf/log4j.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<root>
<priority value="info"></priority>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.netflix.astyanax.contrib.eureka;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.netflix.appinfo.AmazonInfo;
import com.netflix.appinfo.AmazonInfo.MetaDataKey;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.MyDataCenterInstanceConfig;
import com.netflix.astyanax.connectionpool.Host;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.DiscoveryManager;
import com.netflix.discovery.shared.Application;

/**
* Simple class that implements {@link Supplier}<{@link List}<{@link Host}>>. It provides a List<{@link Host}>
* using the {@link DiscoveryManager} which is the eureka client.
*
* Note that the class needs the eureka application name to discover all instances for that application.
*
* @author poberai
*/
public class EurekaBasedHostSupplier implements Supplier<List<Host>> {

private static final Logger LOG = LoggerFactory.getLogger(EurekaBasedHostSupplier.class);

// The C* cluster name for discovering nodes
private final String applicationName;

public EurekaBasedHostSupplier(String applicationName) {
this.applicationName = applicationName.toUpperCase();
// initialize eureka client. make sure eureka properties are properly configured in config.properties
DiscoveryManager.getInstance().initComponent(new MyDataCenterInstanceConfig(), new DefaultEurekaClientConfig());
}

@Override
public List<Host> get() {

DiscoveryClient discoveryClient = DiscoveryManager.getInstance().getDiscoveryClient();
if (discoveryClient == null) {
LOG.error("Error getting discovery client");
throw new RuntimeException("Failed to create discovery client");
}

Application app = discoveryClient.getApplication(applicationName);
List<Host> hosts = Lists.newArrayList();

if (app == null) {
return hosts;
}

List<InstanceInfo> ins = app.getInstances();

if (ins == null || ins.isEmpty()) {
return hosts;
}

hosts = Lists.newArrayList(Collections2.transform(
Collections2.filter(ins, new Predicate<InstanceInfo>() {
@Override
public boolean apply(InstanceInfo input) {
return input.getStatus() == InstanceInfo.InstanceStatus.UP;
}
}), new Function<InstanceInfo, Host>() {
@Override
public Host apply(InstanceInfo info) {
String[] parts = StringUtils.split(
StringUtils.split(info.getHostName(), ".")[0], '-');

Host host = new Host(info.getHostName(), info.getPort())
.addAlternateIpAddress(
StringUtils.join(new String[] { parts[1], parts[2], parts[3],
parts[4] }, "."))
.addAlternateIpAddress(info.getIPAddr())
.setId(info.getId());

try {
if (info.getDataCenterInfo() instanceof AmazonInfo) {
AmazonInfo amazonInfo = (AmazonInfo)info.getDataCenterInfo();
host.setRack(amazonInfo.get(MetaDataKey.availabilityZone));
}
}
catch (Throwable t) {
LOG.error("Error getting rack for host " + host.getName(), t);
}

return host;
}
}));


return hosts;
}
}
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ project(':astyanax') {
compile project(':astyanax-recipes')
compile project(':astyanax-entity-mapper')
compile project(':astyanax-examples')
compile project(':astyanax-contrib')
}
}

Expand Down Expand Up @@ -130,3 +131,11 @@ project(':astyanax-examples') {
testCompile project(':astyanax-thrift')
}
}

project(':astyanax-contrib') {
apply plugin: 'java'
dependencies {
compile project(':astyanax-core')
compile 'com.netflix.eureka:eureka-client:1.1.110'
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name='astyanax'
include 'astyanax-core', 'astyanax-queue', 'astyanax-thrift', 'astyanax-recipes', 'astyanax-cassandra', 'astyanax-entity-mapper', 'astyanax-examples', 'astyanax'
include 'astyanax-core', 'astyanax-queue', 'astyanax-thrift', 'astyanax-recipes', 'astyanax-cassandra', 'astyanax-entity-mapper', 'astyanax-examples', 'astyanax-contrib', 'astyanax'

0 comments on commit 976160e

Please sign in to comment.