Commit 162eb1a2 by Spencer Gibb

allow ipAddress to be used based on ribbon.UseIPAddrForServer.

fixes gh-67
parent b6426e1b
...@@ -18,6 +18,8 @@ package org.springframework.cloud.netflix.ribbon.eureka; ...@@ -18,6 +18,8 @@ package org.springframework.cloud.netflix.ribbon.eureka;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
...@@ -34,73 +36,79 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer; ...@@ -34,73 +36,79 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
*/ */
public class DomainExtractingServerList implements ServerList<Server> { public class DomainExtractingServerList implements ServerList<Server> {
private ServerList<Server> list; private ServerList<Server> list;
private IClientConfig clientConfig;
public DomainExtractingServerList(ServerList<Server> list) {
this.list = list; public DomainExtractingServerList(ServerList<Server> list, IClientConfig clientConfig) {
} this.list = list;
this.clientConfig = clientConfig;
@Override }
public List<Server> getInitialListOfServers() {
List<Server> servers = setZones(list.getInitialListOfServers()); @Override
return servers; public List<Server> getInitialListOfServers() {
} List<Server> servers = setZones(list.getInitialListOfServers());
return servers;
@Override }
public List<Server> getUpdatedListOfServers() {
List<Server> servers = setZones(list.getUpdatedListOfServers()); @Override
return servers; public List<Server> getUpdatedListOfServers() {
} List<Server> servers = setZones(list.getUpdatedListOfServers());
return servers;
private List<Server> setZones(List<Server> servers) { }
List<Server> result = new ArrayList<Server>();
for (Server server : servers) { private List<Server> setZones(List<Server> servers) {
if (server instanceof DiscoveryEnabledServer) { List<Server> result = new ArrayList<>();
result.add(new DomainExtractingServer((DiscoveryEnabledServer) server)); boolean isSecure = clientConfig.getPropertyAsBoolean(CommonClientConfigKey.IsSecure, Boolean.TRUE);
} boolean shouldUseIpAddr = clientConfig.getPropertyAsBoolean(CommonClientConfigKey.UseIPAddrForServer, Boolean.FALSE);
else { for (Server server : servers) {
result.add(server); if (server instanceof DiscoveryEnabledServer) {
} result.add(new DomainExtractingServer((DiscoveryEnabledServer) server,
} isSecure, shouldUseIpAddr));
return result; }
} else {
result.add(server);
}
}
return result;
}
} }
class DomainExtractingServer extends DiscoveryEnabledServer { class DomainExtractingServer extends DiscoveryEnabledServer {
@Getter @Getter
@Setter @Setter
private String id; private String id;
public DomainExtractingServer(DiscoveryEnabledServer server) { public DomainExtractingServer(DiscoveryEnabledServer server, boolean useSecurePort, boolean useIpAddr) {
super(server.getInstanceInfo(), true, false); super(server.getInstanceInfo(), useSecurePort, useIpAddr);
setZone(extractApproximateZone(server)); setZone(extractApproximateZone(server));
setId(extractId(server)); setId(extractId(server));
} }
private String extractId(Server server) { private String extractId(Server server) {
if (server instanceof DiscoveryEnabledServer) { if (server instanceof DiscoveryEnabledServer) {
DiscoveryEnabledServer enabled = (DiscoveryEnabledServer) server; DiscoveryEnabledServer enabled = (DiscoveryEnabledServer) server;
InstanceInfo instance = enabled.getInstanceInfo(); InstanceInfo instance = enabled.getInstanceInfo();
if (instance.getMetadata().containsKey("instanceId")) { if (instance.getMetadata().containsKey("instanceId")) {
return instance.getMetadata().get("instanceId"); return instance.getMetadata().get("instanceId");
} }
} }
return server.getId(); return super.getId();
} }
private String extractApproximateZone(Server server) { private String extractApproximateZone(Server server) {
String host = server.getHost(); String host = server.getHost();
if (!host.contains(".")) { if (!host.contains(".")) {
return host; return host;
} }
String[] split = StringUtils.split(host, "."); String[] split = StringUtils.split(host, ".");
StringBuilder builder = new StringBuilder(split[1]); StringBuilder builder = new StringBuilder(split[1]);
for (int i = 2; i < split.length; i++) { for (int i = 2; i < split.length; i++) {
builder.append(".").append(split[i]); builder.append(".").append(split[i]);
} }
return builder.toString(); return builder.toString();
} }
} }
...@@ -81,7 +81,7 @@ public class EurekaRibbonClientPreprocessor implements RibbonClientPreprocessor ...@@ -81,7 +81,7 @@ public class EurekaRibbonClientPreprocessor implements RibbonClientPreprocessor
// the server zone is populated. TODO: find a way to back off if AWS // the server zone is populated. TODO: find a way to back off if AWS
// metadata *is* available. // metadata *is* available.
// @see com.netflix.appinfo.AmazonInfo.Builder // @see com.netflix.appinfo.AmazonInfo.Builder
dynamic.setServerListImpl(new DomainExtractingServerList(list)); dynamic.setServerListImpl(new DomainExtractingServerList(list, dynamic.getClientConfig()));
} }
} }
} }
......
package org.springframework.cloud.netflix.ribbon.eureka;
import com.google.common.collect.ImmutableMap;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
/**
* @author Spencer Gibb
*/
public class DomainExtractingServerListTests {
static final String IP_ADDR = "10.0.0.2";
static final int PORT = 8080;
static final String ZONE = "myzone.mydomain.com";
static final String HOST_NAME = "myHostName."+ZONE;
static final String INSTANCE_ID = "myInstanceId";
@Test
public void testDomainExtractingServer() {
DomainExtractingServerList serverList = getDomainExtractingServerList(new DefaultClientConfigImpl());
List<Server> servers = serverList.getInitialListOfServers();
assertNotNull("servers was null", servers);
assertEquals("servers was not size 1", 1, servers.size());
DomainExtractingServer des = assertDomainExtractingServer(servers);
assertEquals("hostPort was wrong", HOST_NAME+":"+PORT, des.getHostPort());
}
protected DomainExtractingServer assertDomainExtractingServer(List<Server> servers) {
Server actualServer = servers.get(0);
assertTrue("server was not a DomainExtractingServer", actualServer instanceof DomainExtractingServer);
DomainExtractingServer des = DomainExtractingServer.class.cast(actualServer);
assertEquals("zone was wrong", ZONE, des.getZone());
assertEquals("instanceId was wrong", INSTANCE_ID, des.getId());
return des;
}
@Test
public void testDomainExtractingServerUseIpAddress() {
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.setProperty(CommonClientConfigKey.UseIPAddrForServer, true);
DomainExtractingServerList serverList = getDomainExtractingServerList(config);
List<Server> servers = serverList.getInitialListOfServers();
assertNotNull("servers was null", servers);
assertEquals("servers was not size 1", 1, servers.size());
DomainExtractingServer des = assertDomainExtractingServer(servers);
assertEquals("hostPort was wrong", IP_ADDR+":"+PORT, des.getHostPort());
}
protected DomainExtractingServerList getDomainExtractingServerList(DefaultClientConfigImpl config) {
DiscoveryEnabledServer server = mock(DiscoveryEnabledServer.class);
ServerList originalServerList = mock(ServerList.class);
InstanceInfo instanceInfo = mock(InstanceInfo.class);
when(server.getInstanceInfo()).thenReturn(instanceInfo);
when(server.getHost()).thenReturn(HOST_NAME);
when(instanceInfo.getMetadata()).thenReturn(ImmutableMap.<String, String>builder().put("instanceId", INSTANCE_ID).build());
when(instanceInfo.getHostName()).thenReturn(HOST_NAME);
when(instanceInfo.getIPAddr()).thenReturn(IP_ADDR);
when(instanceInfo.getPort()).thenReturn(PORT);
when(originalServerList.getInitialListOfServers()).thenReturn(Arrays.asList(server));
return new DomainExtractingServerList(originalServerList, config);
}
}
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