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;
import java.util.ArrayList;
import java.util.List;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig;
import lombok.Getter;
import lombok.Setter;
......@@ -35,9 +37,11 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
public class DomainExtractingServerList implements ServerList<Server> {
private ServerList<Server> list;
private IClientConfig clientConfig;
public DomainExtractingServerList(ServerList<Server> list) {
public DomainExtractingServerList(ServerList<Server> list, IClientConfig clientConfig) {
this.list = list;
this.clientConfig = clientConfig;
}
@Override
......@@ -53,10 +57,13 @@ public class DomainExtractingServerList implements ServerList<Server> {
}
private List<Server> setZones(List<Server> servers) {
List<Server> result = new ArrayList<Server>();
List<Server> result = new ArrayList<>();
boolean isSecure = clientConfig.getPropertyAsBoolean(CommonClientConfigKey.IsSecure, Boolean.TRUE);
boolean shouldUseIpAddr = clientConfig.getPropertyAsBoolean(CommonClientConfigKey.UseIPAddrForServer, Boolean.FALSE);
for (Server server : servers) {
if (server instanceof DiscoveryEnabledServer) {
result.add(new DomainExtractingServer((DiscoveryEnabledServer) server));
result.add(new DomainExtractingServer((DiscoveryEnabledServer) server,
isSecure, shouldUseIpAddr));
}
else {
result.add(server);
......@@ -73,8 +80,8 @@ class DomainExtractingServer extends DiscoveryEnabledServer {
@Setter
private String id;
public DomainExtractingServer(DiscoveryEnabledServer server) {
super(server.getInstanceInfo(), true, false);
public DomainExtractingServer(DiscoveryEnabledServer server, boolean useSecurePort, boolean useIpAddr) {
super(server.getInstanceInfo(), useSecurePort, useIpAddr);
setZone(extractApproximateZone(server));
setId(extractId(server));
}
......@@ -87,7 +94,7 @@ class DomainExtractingServer extends DiscoveryEnabledServer {
return instance.getMetadata().get("instanceId");
}
}
return server.getId();
return super.getId();
}
private String extractApproximateZone(Server server) {
......@@ -104,3 +111,4 @@ class DomainExtractingServer extends DiscoveryEnabledServer {
}
}
......@@ -81,7 +81,7 @@ public class EurekaRibbonClientPreprocessor implements RibbonClientPreprocessor
// the server zone is populated. TODO: find a way to back off if AWS
// metadata *is* available.
// @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