Commit 39eb9815 by Spencer Gibb

Merge branch 'loadbalancer-builder'

parents 91e3bee5 af0b24d1
...@@ -24,10 +24,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -24,10 +24,7 @@ import org.springframework.context.annotation.Configuration;
import com.netflix.client.config.DefaultClientConfigImpl; import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.*;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerListFilter;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.client.http.RestClient; import com.netflix.niws.client.http.RestClient;
import com.netflix.servo.monitor.Monitors; import com.netflix.servo.monitor.Monitors;
...@@ -55,6 +52,29 @@ public class RibbonClientConfiguration { ...@@ -55,6 +52,29 @@ public class RibbonClientConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public IRule ribbonRule(IClientConfig config) {
ZoneAvoidanceRule rule = new ZoneAvoidanceRule();
rule.initWithNiwsConfig(config);
return rule;
}
@Bean
@ConditionalOnMissingBean
public IPing ribbonPing(IClientConfig config) {
// TODO: use PingUrl
return new NoOpPing();
}
@Bean
@ConditionalOnMissingBean
public ServerList<Server> ribbonServerList(IClientConfig config) {
ConfigurationBasedServerList serverList = new ConfigurationBasedServerList();
serverList.initWithNiwsConfig(config);
return serverList;
}
@Bean
@ConditionalOnMissingBean
public RestClient ribbonRestClient(IClientConfig config, ILoadBalancer loadBalancer) { public RestClient ribbonRestClient(IClientConfig config, ILoadBalancer loadBalancer) {
RestClient client = new RestClient(config); RestClient client = new RestClient(config);
client.setLoadBalancer(loadBalancer); client.setLoadBalancer(loadBalancer);
...@@ -65,9 +85,16 @@ public class RibbonClientConfiguration { ...@@ -65,9 +85,16 @@ public class RibbonClientConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config, public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
ServerListFilter<Server> filter) { ServerList<Server> serverList,
ZoneAwareLoadBalancer<Server> balancer = new ZoneAwareLoadBalancer<>(config); ServerListFilter<Server> serverListFilter,
balancer.setFilter(filter); IRule rule, IPing ping) {
ZoneAwareLoadBalancer<Server> balancer = LoadBalancerBuilder.newBuilder()
.withClientConfig(config)
.withRule(rule)
.withPing(ping)
.withServerListFilter(serverListFilter)
.withDynamicServerList(serverList)
.buildDynamicServerListLoadBalancer();
return balancer; return balancer;
} }
......
...@@ -34,15 +34,15 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer; ...@@ -34,15 +34,15 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
/** /**
* @author Dave Syer * @author Dave Syer
*/ */
public class DomainExtractingServerList implements ServerList<Server> { public class DomainExtractingServerList implements ServerList<DiscoveryEnabledServer> {
private ServerList<Server> list; private ServerList<DiscoveryEnabledServer> list;
private IClientConfig clientConfig; private IClientConfig clientConfig;
private boolean approximateZoneFromHostname; private boolean approximateZoneFromHostname;
public DomainExtractingServerList(ServerList<Server> list, public DomainExtractingServerList(ServerList<DiscoveryEnabledServer> list,
IClientConfig clientConfig, boolean approximateZoneFromHostname) { IClientConfig clientConfig, boolean approximateZoneFromHostname) {
this.list = list; this.list = list;
this.clientConfig = clientConfig; this.clientConfig = clientConfig;
...@@ -50,31 +50,26 @@ public class DomainExtractingServerList implements ServerList<Server> { ...@@ -50,31 +50,26 @@ public class DomainExtractingServerList implements ServerList<Server> {
} }
@Override @Override
public List<Server> getInitialListOfServers() { public List<DiscoveryEnabledServer> getInitialListOfServers() {
List<Server> servers = setZones(this.list.getInitialListOfServers()); List<DiscoveryEnabledServer> servers = setZones(this.list.getInitialListOfServers());
return servers; return servers;
} }
@Override @Override
public List<Server> getUpdatedListOfServers() { public List<DiscoveryEnabledServer> getUpdatedListOfServers() {
List<Server> servers = setZones(this.list.getUpdatedListOfServers()); List<DiscoveryEnabledServer> servers = setZones(this.list.getUpdatedListOfServers());
return servers; return servers;
} }
private List<Server> setZones(List<Server> servers) { private List<DiscoveryEnabledServer> setZones(List<DiscoveryEnabledServer> servers) {
List<Server> result = new ArrayList<>(); List<DiscoveryEnabledServer> result = new ArrayList<>();
boolean isSecure = this.clientConfig.getPropertyAsBoolean( boolean isSecure = this.clientConfig.getPropertyAsBoolean(
CommonClientConfigKey.IsSecure, Boolean.TRUE); CommonClientConfigKey.IsSecure, Boolean.TRUE);
boolean shouldUseIpAddr = this.clientConfig.getPropertyAsBoolean( boolean shouldUseIpAddr = this.clientConfig.getPropertyAsBoolean(
CommonClientConfigKey.UseIPAddrForServer, Boolean.FALSE); CommonClientConfigKey.UseIPAddrForServer, Boolean.FALSE);
for (Server server : servers) { for (DiscoveryEnabledServer server : servers) {
if (server instanceof DiscoveryEnabledServer) { result.add(new DomainExtractingServer(server,
result.add(new DomainExtractingServer((DiscoveryEnabledServer) server, isSecure, shouldUseIpAddr, this.approximateZoneFromHostname));
isSecure, shouldUseIpAddr, this.approximateZoneFromHostname));
}
else {
result.add(server);
}
} }
return result; return result;
} }
......
...@@ -16,31 +16,27 @@ ...@@ -16,31 +16,27 @@
package org.springframework.cloud.netflix.ribbon.eureka; package org.springframework.cloud.netflix.ribbon.eureka;
import static com.netflix.client.config.CommonClientConfigKey.DeploymentContextBasedVipAddresses;
import static com.netflix.client.config.CommonClientConfigKey.EnableZoneAffinity;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.netflix.client.config.IClientConfig;
import com.netflix.config.ConfigurationManager; import com.netflix.config.ConfigurationManager;
import com.netflix.config.DeploymentContext.ContextKey; import com.netflix.config.DeploymentContext.ContextKey;
import com.netflix.config.DynamicPropertyFactory; import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty; import com.netflix.config.DynamicStringProperty;
import com.netflix.discovery.EurekaClientConfig; import com.netflix.discovery.EurekaClientConfig;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer; import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList; import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ZoneAvoidanceRule;
import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList; import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
import com.netflix.niws.loadbalancer.NIWSDiscoveryPing;
import static com.netflix.client.config.CommonClientConfigKey.DeploymentContextBasedVipAddresses;
import static com.netflix.client.config.CommonClientConfigKey.EnableZoneAffinity;
import static com.netflix.client.config.CommonClientConfigKey.NFLoadBalancerRuleClassName;
import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListClassName;
import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListFilterClassName;
/** /**
* Preprocessor that configures defaults for eureka-discovered ribbon clients. Such as: * Preprocessor that configures defaults for eureka-discovered ribbon clients. Such as:
...@@ -51,7 +47,7 @@ import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListFilt ...@@ -51,7 +47,7 @@ import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListFilt
* @author Dave Syer * @author Dave Syer
*/ */
@Configuration @Configuration
public class EurekaRibbonClientConfiguration implements BeanPostProcessor { public class EurekaRibbonClientConfiguration {
@Value("${ribbon.eureka.approximateZoneFromHostname:false}") @Value("${ribbon.eureka.approximateZoneFromHostname:false}")
private boolean approximateZoneFromHostname = false; private boolean approximateZoneFromHostname = false;
...@@ -75,6 +71,22 @@ public class EurekaRibbonClientConfiguration implements BeanPostProcessor { ...@@ -75,6 +71,22 @@ public class EurekaRibbonClientConfiguration implements BeanPostProcessor {
this.serviceId = serviceId; this.serviceId = serviceId;
} }
@Bean
@ConditionalOnMissingBean
public IPing ribbonPing(IClientConfig config) {
NIWSDiscoveryPing ping = new NIWSDiscoveryPing();
ping.initWithNiwsConfig(config);
return ping;
}
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config) {
DiscoveryEnabledNIWSServerList discoveryServerList = new DiscoveryEnabledNIWSServerList(config);
DomainExtractingServerList serverList = new DomainExtractingServerList(discoveryServerList, config, this.approximateZoneFromHostname);
return serverList;
}
@PostConstruct @PostConstruct
public void preprocess() { public void preprocess() {
if (this.clientConfig != null if (this.clientConfig != null
...@@ -89,47 +101,10 @@ public class EurekaRibbonClientConfiguration implements BeanPostProcessor { ...@@ -89,47 +101,10 @@ public class EurekaRibbonClientConfiguration implements BeanPostProcessor {
zone); zone);
} }
} }
// TODO: should this look more like hibernate spring boot props?
setProp(this.serviceId, NIWSServerListClassName.key(),
DiscoveryEnabledNIWSServerList.class.getName());
// FIXME: what should this be?
setProp(this.serviceId, DeploymentContextBasedVipAddresses.key(), this.serviceId); setProp(this.serviceId, DeploymentContextBasedVipAddresses.key(), this.serviceId);
setProp(this.serviceId, NFLoadBalancerRuleClassName.key(),
ZoneAvoidanceRule.class.getName());
setProp(this.serviceId, NIWSServerListFilterClassName.key(),
ZonePreferenceServerListFilter.class.getName());
setProp(this.serviceId, EnableZoneAffinity.key(), "true"); setProp(this.serviceId, EnableZoneAffinity.key(), "true");
} }
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof DynamicServerListLoadBalancer) {
wrapServerList((DynamicServerListLoadBalancer<?>) bean);
}
return bean;
}
private void wrapServerList(DynamicServerListLoadBalancer<?> balancer) {
@SuppressWarnings("unchecked")
DynamicServerListLoadBalancer<Server> dynamic = (DynamicServerListLoadBalancer<Server>) balancer;
ServerList<Server> list = dynamic.getServerListImpl();
if (!(list instanceof DomainExtractingServerList)) {
// This is optional: you can use the native Eureka AWS features as long as
// the server zone is populated. TODO: verify that we back off if AWS
// metadata *is* available.
// @see com.netflix.appinfo.AmazonInfo.Builder
dynamic.setServerListImpl(new DomainExtractingServerList(list, dynamic
.getClientConfig(), this.approximateZoneFromHostname));
}
}
protected void setProp(String serviceId, String suffix, String value) { protected void setProp(String serviceId, String suffix, String value) {
// how to set the namespace properly? // how to set the namespace properly?
String key = getKey(serviceId, suffix); String key = getKey(serviceId, suffix);
......
...@@ -16,8 +16,6 @@ ...@@ -16,8 +16,6 @@
package org.springframework.cloud.netflix.ribbon; package org.springframework.cloud.netflix.ribbon;
import static org.junit.Assert.assertEquals;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -25,15 +23,12 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati ...@@ -25,15 +23,12 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration; import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClientPreprocessorIntegrationTests.PlainConfiguration; import org.springframework.cloud.netflix.ribbon.RibbonClientPreprocessorIntegrationTests.PlainConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.netflix.loadbalancer.AvailabilityFilteringRule; import com.netflix.loadbalancer.*;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
/** /**
* @author Dave Syer * @author Dave Syer
...@@ -47,38 +42,35 @@ public class RibbonClientPreprocessorIntegrationTests { ...@@ -47,38 +42,35 @@ public class RibbonClientPreprocessorIntegrationTests {
private SpringClientFactory factory; private SpringClientFactory factory;
@Test @Test
public void ruleDefaultsToAvailability() throws Exception { public void ruleDefaultsToZoneAvoidance() throws Exception {
@SuppressWarnings("unchecked") ZoneAvoidanceRule.class.cast(getLoadBalancer().getRule());
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory }
.getLoadBalancer("foo");
AvailabilityFilteringRule.class.cast(loadBalancer.getRule()); @Test
public void serverListFilterDefaultsToZonePreference() throws Exception {
ZonePreferenceServerListFilter.class.cast(getLoadBalancer().getFilter());
}
@Test
public void pingDefaultsToNoOp() throws Exception {
NoOpPing.class.cast(getLoadBalancer().getPing());
} }
@Test @Test
public void serverListFilterOverride() throws Exception { public void serverListDefaultsToConfigurationBased() throws Exception {
@SuppressWarnings("unchecked") ConfigurationBasedServerList.class.cast(getLoadBalancer().getServerListImpl());
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory }
.getLoadBalancer("foo");
assertEquals("myTestZone", @SuppressWarnings("unchecked")
ZonePreferenceServerListFilter.class.cast(loadBalancer.getFilter()) private ZoneAwareLoadBalancer<Server> getLoadBalancer() {
.getZone()); return (ZoneAwareLoadBalancer<Server>) this.factory.getLoadBalancer("foo");
} }
@Configuration @Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class) @RibbonClient(name = "foo")
@Import({ PropertyPlaceholderAutoConfiguration.class, @Import({ PropertyPlaceholderAutoConfiguration.class,
ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class }) ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class })
protected static class PlainConfiguration { protected static class PlainConfiguration {
} }
@Configuration
protected static class FooConfiguration {
@Bean
public ZonePreferenceServerListFilter serverListFilter() {
ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
filter.setZone("myTestZone");
return filter;
}
}
} }
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.ribbon;
import static org.junit.Assert.assertEquals;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RibbonClientPreprocessorOverridesIntegrationTests.TestConfiguration.class)
@DirtiesContext
public class RibbonClientPreprocessorOverridesIntegrationTests {
@Autowired
private SpringClientFactory factory;
@Test
public void ruleOverridesToRandom() throws Exception {
RandomRule.class.cast(getLoadBalancer().getRule());
}
@Test
public void pingOverridesToDummy() throws Exception {
DummyPing.class.cast(getLoadBalancer().getPing());
}
@Test
public void serverListOverridesToMy() throws Exception {
MyServiceList.class.cast(getLoadBalancer().getServerListImpl());
}
@SuppressWarnings("unchecked")
private ZoneAwareLoadBalancer<Server> getLoadBalancer() {
return (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo");
}
@Test
public void serverListFilterOverride() throws Exception {
ServerListFilter<Server> filter = getLoadBalancer().getFilter();
assertEquals("MyTestZone",
ZonePreferenceServerListFilter.class.cast(filter)
.getZone());
}
@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class)
@Import({ PropertyPlaceholderAutoConfiguration.class,
ArchaiusAutoConfiguration.class, EurekaClientAutoConfiguration.class,
RibbonAutoConfiguration.class})
protected static class TestConfiguration {
}
@Configuration
public static class FooConfiguration {
public FooConfiguration() {
System.out.println("here");
}
@Bean
public IRule ribbonRule() {
return new RandomRule();
}
@Bean
public IPing ribbonPing() {
return new DummyPing();
}
@Bean
public ServerList<Server> ribbonServerList(IClientConfig config) {
return new MyServiceList(config);
}
@Bean
public ZonePreferenceServerListFilter serverListFilter() {
ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
filter.setZone("MyTestZone");
return filter;
}
}
public static class MyServiceList extends ConfigurationBasedServerList {
public MyServiceList(IClientConfig config) {
super.initWithNiwsConfig(config);
}
}
}
...@@ -24,8 +24,6 @@ import org.springframework.boot.test.SpringApplicationConfiguration; ...@@ -24,8 +24,6 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration; import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration; import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClientsPreprocessorIntegrationTests.TestConfiguration; import org.springframework.cloud.netflix.ribbon.RibbonClientsPreprocessorIntegrationTests.TestConfiguration;
import org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList;
import org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
...@@ -50,28 +48,20 @@ public class RibbonClientsPreprocessorIntegrationTests { ...@@ -50,28 +48,20 @@ public class RibbonClientsPreprocessorIntegrationTests {
private SpringClientFactory factory; private SpringClientFactory factory;
@Test @Test
public void serverListIsWrapped() throws Exception { public void ruleDefaultsToZoneAvoidance() throws Exception {
@SuppressWarnings("unchecked") ZoneAvoidanceRule.class.cast(getLoadBalancer().getRule());
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo");
DomainExtractingServerList.class.cast(loadBalancer.getServerListImpl());
} }
@Test @SuppressWarnings("unchecked")
public void ruleDefaultsToZoneAvoidance() throws Exception { private ZoneAwareLoadBalancer<Server> getLoadBalancer() {
@SuppressWarnings("unchecked") return (ZoneAwareLoadBalancer<Server>) this.factory
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo"); .getLoadBalancer("foo");
ZoneAvoidanceRule.class.cast(loadBalancer.getRule());
} }
@Test @Test
public void serverListFilterOverride() throws Exception { public void serverListFilterOverride() throws Exception {
@SuppressWarnings("unchecked")
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo");
assertEquals("myTestZone", assertEquals("myTestZone",
ZonePreferenceServerListFilter.class.cast(loadBalancer.getFilter()) ZonePreferenceServerListFilter.class.cast(getLoadBalancer().getFilter())
.getZone()); .getZone());
} }
...@@ -79,7 +69,7 @@ public class RibbonClientsPreprocessorIntegrationTests { ...@@ -79,7 +69,7 @@ public class RibbonClientsPreprocessorIntegrationTests {
@RibbonClients(@RibbonClient(name = "foo", configuration = FooConfiguration.class)) @RibbonClients(@RibbonClient(name = "foo", configuration = FooConfiguration.class))
@Import({ PropertyPlaceholderAutoConfiguration.class, @Import({ PropertyPlaceholderAutoConfiguration.class,
ArchaiusAutoConfiguration.class, EurekaClientAutoConfiguration.class, ArchaiusAutoConfiguration.class, EurekaClientAutoConfiguration.class,
RibbonAutoConfiguration.class, RibbonEurekaAutoConfiguration.class }) RibbonAutoConfiguration.class})
protected static class TestConfiguration { protected static class TestConfiguration {
} }
......
...@@ -54,7 +54,7 @@ public class DomainExtractingServerListTests { ...@@ -54,7 +54,7 @@ public class DomainExtractingServerListTests {
public void testDomainExtractingServer() { public void testDomainExtractingServer() {
DomainExtractingServerList serverList = getDomainExtractingServerList( DomainExtractingServerList serverList = getDomainExtractingServerList(
new DefaultClientConfigImpl(), true); new DefaultClientConfigImpl(), true);
List<Server> servers = serverList.getInitialListOfServers(); List<DiscoveryEnabledServer> servers = serverList.getInitialListOfServers();
assertNotNull("servers was null", servers); assertNotNull("servers was null", servers);
assertEquals("servers was not size 1", 1, servers.size()); assertEquals("servers was not size 1", 1, servers.size());
DomainExtractingServer des = assertDomainExtractingServer(servers, ZONE); DomainExtractingServer des = assertDomainExtractingServer(servers, ZONE);
...@@ -65,14 +65,14 @@ public class DomainExtractingServerListTests { ...@@ -65,14 +65,14 @@ public class DomainExtractingServerListTests {
public void testDomainExtractingServerDontApproximateZone() { public void testDomainExtractingServerDontApproximateZone() {
DomainExtractingServerList serverList = getDomainExtractingServerList( DomainExtractingServerList serverList = getDomainExtractingServerList(
new DefaultClientConfigImpl(), false); new DefaultClientConfigImpl(), false);
List<Server> servers = serverList.getInitialListOfServers(); List<DiscoveryEnabledServer> servers = serverList.getInitialListOfServers();
assertNotNull("servers was null", servers); assertNotNull("servers was null", servers);
assertEquals("servers was not size 1", 1, servers.size()); assertEquals("servers was not size 1", 1, servers.size());
DomainExtractingServer des = assertDomainExtractingServer(servers, null); DomainExtractingServer des = assertDomainExtractingServer(servers, null);
assertEquals("hostPort was wrong", HOST_NAME + ":" + PORT, des.getHostPort()); assertEquals("hostPort was wrong", HOST_NAME + ":" + PORT, des.getHostPort());
} }
protected DomainExtractingServer assertDomainExtractingServer(List<Server> servers, protected DomainExtractingServer assertDomainExtractingServer(List<DiscoveryEnabledServer> servers,
String zone) { String zone) {
Server actualServer = servers.get(0); Server actualServer = servers.get(0);
assertTrue("server was not a DomainExtractingServer", assertTrue("server was not a DomainExtractingServer",
...@@ -89,7 +89,7 @@ public class DomainExtractingServerListTests { ...@@ -89,7 +89,7 @@ public class DomainExtractingServerListTests {
config.setProperty(CommonClientConfigKey.UseIPAddrForServer, true); config.setProperty(CommonClientConfigKey.UseIPAddrForServer, true);
DomainExtractingServerList serverList = getDomainExtractingServerList(config, DomainExtractingServerList serverList = getDomainExtractingServerList(config,
true); true);
List<Server> servers = serverList.getInitialListOfServers(); List<DiscoveryEnabledServer> servers = serverList.getInitialListOfServers();
assertNotNull("servers was null", servers); assertNotNull("servers was null", servers);
assertEquals("servers was not size 1", 1, servers.size()); assertEquals("servers was not size 1", 1, servers.size());
DomainExtractingServer des = assertDomainExtractingServer(servers, ZONE); DomainExtractingServer des = assertDomainExtractingServer(servers, ZONE);
...@@ -100,7 +100,7 @@ public class DomainExtractingServerListTests { ...@@ -100,7 +100,7 @@ public class DomainExtractingServerListTests {
DefaultClientConfigImpl config, boolean approximateZoneFromHostname) { DefaultClientConfigImpl config, boolean approximateZoneFromHostname) {
DiscoveryEnabledServer server = mock(DiscoveryEnabledServer.class); DiscoveryEnabledServer server = mock(DiscoveryEnabledServer.class);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ServerList<Server> originalServerList = mock(ServerList.class); ServerList<DiscoveryEnabledServer> originalServerList = mock(ServerList.class);
InstanceInfo instanceInfo = mock(InstanceInfo.class); InstanceInfo instanceInfo = mock(InstanceInfo.class);
given(server.getInstanceInfo()).willReturn(instanceInfo); given(server.getInstanceInfo()).willReturn(instanceInfo);
given(server.getHost()).willReturn(HOST_NAME); given(server.getHost()).willReturn(HOST_NAME);
...@@ -111,7 +111,7 @@ public class DomainExtractingServerListTests { ...@@ -111,7 +111,7 @@ public class DomainExtractingServerListTests {
given(instanceInfo.getIPAddr()).willReturn(IP_ADDR); given(instanceInfo.getIPAddr()).willReturn(IP_ADDR);
given(instanceInfo.getPort()).willReturn(PORT); given(instanceInfo.getPort()).willReturn(PORT);
given(originalServerList.getInitialListOfServers()).willReturn( given(originalServerList.getInitialListOfServers()).willReturn(
Arrays.<Server> asList(server)); Arrays.asList(server));
return new DomainExtractingServerList(originalServerList, config, return new DomainExtractingServerList(originalServerList, config,
approximateZoneFromHostname); approximateZoneFromHostname);
} }
......
...@@ -26,12 +26,10 @@ import com.netflix.config.ConfigurationManager; ...@@ -26,12 +26,10 @@ import com.netflix.config.ConfigurationManager;
import com.netflix.config.DeploymentContext.ContextKey; import com.netflix.config.DeploymentContext.ContextKey;
import com.netflix.config.DynamicStringProperty; import com.netflix.config.DynamicStringProperty;
import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.cloud.netflix.ribbon.eureka.EurekaRibbonClientConfiguration.VALUE_NOT_SET; import static org.springframework.cloud.netflix.ribbon.eureka.EurekaRibbonClientConfiguration.VALUE_NOT_SET;
/** /**
...@@ -56,7 +54,7 @@ public class EurekaRibbonClientConfigurationTests { ...@@ -56,7 +54,7 @@ public class EurekaRibbonClientConfigurationTests {
ILoadBalancer balancer = clientFactory.getLoadBalancer("service"); ILoadBalancer balancer = clientFactory.getLoadBalancer("service");
assertNotNull(balancer); assertNotNull(balancer);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ZoneAwareLoadBalancer<Server> aware = (ZoneAwareLoadBalancer<Server>) balancer; ZoneAwareLoadBalancer<DiscoveryEnabledServer> aware = (ZoneAwareLoadBalancer<DiscoveryEnabledServer>) balancer;
assertTrue(aware.getServerListImpl() instanceof DomainExtractingServerList); assertTrue(aware.getServerListImpl() instanceof DomainExtractingServerList);
assertEquals("foo", assertEquals("foo",
ConfigurationManager.getDeploymentContext().getValue(ContextKey.zone)); ConfigurationManager.getDeploymentContext().getValue(ContextKey.zone));
......
...@@ -35,6 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; ...@@ -35,6 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAvoidanceRule; import com.netflix.loadbalancer.ZoneAvoidanceRule;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.loadbalancer.NIWSDiscoveryPing;
/** /**
* @author Dave Syer * @author Dave Syer
...@@ -48,11 +49,24 @@ public class EurekaRibbonClientPreprocessorIntegrationTests { ...@@ -48,11 +49,24 @@ public class EurekaRibbonClientPreprocessorIntegrationTests {
private SpringClientFactory factory; private SpringClientFactory factory;
@Test @Test
public void serverListDefaultsToDomainExtracting() throws Exception {
DomainExtractingServerList.class.cast(getLoadBalancer().getServerListImpl());
}
@Test
public void ruleDefaultsToZoneAvoidance() throws Exception { public void ruleDefaultsToZoneAvoidance() throws Exception {
@SuppressWarnings("unchecked") ZoneAvoidanceRule.class.cast(getLoadBalancer().getRule());
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory }
@Test
public void pingDefaultsToDiscoveryPing() throws Exception {
NIWSDiscoveryPing.class.cast(getLoadBalancer().getPing());
}
@SuppressWarnings("unchecked")
private ZoneAwareLoadBalancer<Server> getLoadBalancer() {
return (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo"); .getLoadBalancer("foo");
ZoneAvoidanceRule.class.cast(loadBalancer.getRule());
} }
@Configuration @Configuration
......
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