Commit 6b01e61d by Dave Syer

Untangle ribbon/eureka

A cleaner separation of ribbon and the eureka-dependent ribbon configuration is achieved by adding a BeanPostProcessor to do the ServerList wrapping, instead of doing it for every single ribbon client. See gh-172
parent ea4ea4a5
...@@ -19,17 +19,13 @@ package org.springframework.cloud.netflix.ribbon; ...@@ -19,17 +19,13 @@ package org.springframework.cloud.netflix.ribbon;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList;
import org.springframework.cloud.netflix.ribbon.eureka.ZonePreferenceServerListFilter;
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 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.DynamicServerListLoadBalancer;
import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ServerListFilter; import com.netflix.loadbalancer.ServerListFilter;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.client.http.RestClient; import com.netflix.niws.client.http.RestClient;
...@@ -46,9 +42,6 @@ public class RibbonClientConfiguration { ...@@ -46,9 +42,6 @@ public class RibbonClientConfiguration {
@Value("${ribbon.client.name}") @Value("${ribbon.client.name}")
private String name = "client"; private String name = "client";
@Value("${ribbon.eureka.approximateZoneFromHostname:false}")
private boolean approximateZoneFromHostname = false;
// TODO: maybe re-instate autowired load balancers: identified by name they could be // TODO: maybe re-instate autowired load balancers: identified by name they could be
// associated with ribbon clients // associated with ribbon clients
...@@ -75,7 +68,6 @@ public class RibbonClientConfiguration { ...@@ -75,7 +68,6 @@ public class RibbonClientConfiguration {
public ILoadBalancer ribbonLoadBalancer(IClientConfig config, public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
ServerListFilter<Server> filter) { ServerListFilter<Server> filter) {
ZoneAwareLoadBalancer<Server> balancer = new ZoneAwareLoadBalancer<>(config); ZoneAwareLoadBalancer<Server> balancer = new ZoneAwareLoadBalancer<>(config);
wrapServerList(balancer);
balancer.setFilter(filter); balancer.setFilter(filter);
return balancer; return balancer;
} }
...@@ -95,20 +87,4 @@ public class RibbonClientConfiguration { ...@@ -95,20 +87,4 @@ public class RibbonClientConfiguration {
return new RibbonLoadBalancerContext(loadBalancer, config); return new RibbonLoadBalancerContext(loadBalancer, config);
} }
private void wrapServerList(ILoadBalancer balancer) {
if (balancer instanceof DynamicServerListLoadBalancer) {
@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));
}
}
}
} }
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.cloud.netflix.ribbon.eureka; package org.springframework.cloud.netflix.ribbon;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
......
...@@ -18,8 +18,11 @@ package org.springframework.cloud.netflix.ribbon.eureka; ...@@ -18,8 +18,11 @@ package org.springframework.cloud.netflix.ribbon.eureka;
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.cloud.netflix.ribbon.ZonePreferenceServerListFilter;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.netflix.config.ConfigurationManager; import com.netflix.config.ConfigurationManager;
...@@ -27,6 +30,9 @@ import com.netflix.config.DeploymentContext.ContextKey; ...@@ -27,6 +30,9 @@ 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.Server;
import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ZoneAvoidanceRule; import com.netflix.loadbalancer.ZoneAvoidanceRule;
import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList; import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
...@@ -45,7 +51,10 @@ import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListFilt ...@@ -45,7 +51,10 @@ import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListFilt
* @author Dave Syer * @author Dave Syer
*/ */
@Configuration @Configuration
public class EurekaRibbonClientConfiguration { public class EurekaRibbonClientConfiguration implements BeanPostProcessor {
@Value("${ribbon.eureka.approximateZoneFromHostname:false}")
private boolean approximateZoneFromHostname = false;
@Value("${ribbon.client.name}") @Value("${ribbon.client.name}")
private String serviceId = "client"; private String serviceId = "client";
...@@ -92,6 +101,35 @@ public class EurekaRibbonClientConfiguration { ...@@ -92,6 +101,35 @@ public class EurekaRibbonClientConfiguration {
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);
......
...@@ -23,12 +23,12 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati ...@@ -23,12 +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.PlainRibbonClientPreprocessorIntegrationTests.TestConfiguration; import org.springframework.cloud.netflix.ribbon.PlainRibbonClientPreprocessorIntegrationTests.TestConfiguration;
import org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList;
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.ConfigurationBasedServerList;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
...@@ -44,17 +44,17 @@ public class PlainRibbonClientPreprocessorIntegrationTests { ...@@ -44,17 +44,17 @@ public class PlainRibbonClientPreprocessorIntegrationTests {
private SpringClientFactory factory; private SpringClientFactory factory;
@Test @Test
public void serverListIsWrapped() throws Exception { public void serverListIsConfigured() throws Exception {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo"); .getLoadBalancer("foo");
DomainExtractingServerList.class.cast(loadBalancer.getServerListImpl()); ConfigurationBasedServerList.class.cast(loadBalancer.getServerListImpl());
} }
@Configuration @Configuration
@RibbonClient("foo") @RibbonClient("foo")
@Import({ PropertyPlaceholderAutoConfiguration.class, @Import({ PropertyPlaceholderAutoConfiguration.class,
ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class }) ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class})
protected static class TestConfiguration { protected static class TestConfiguration {
} }
......
...@@ -16,15 +16,15 @@ ...@@ -16,15 +16,15 @@
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;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
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.TestConfiguration; import org.springframework.cloud.netflix.ribbon.RibbonClientPreprocessorIntegrationTests.PlainConfiguration;
import org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList;
import org.springframework.cloud.netflix.ribbon.eureka.ZonePreferenceServerListFilter;
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;
...@@ -35,13 +35,11 @@ import com.netflix.loadbalancer.AvailabilityFilteringRule; ...@@ -35,13 +35,11 @@ import com.netflix.loadbalancer.AvailabilityFilteringRule;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import static org.junit.Assert.assertEquals;
/** /**
* @author Dave Syer * @author Dave Syer
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfiguration.class) @SpringApplicationConfiguration(classes = PlainConfiguration.class)
@DirtiesContext @DirtiesContext
public class RibbonClientPreprocessorIntegrationTests { public class RibbonClientPreprocessorIntegrationTests {
...@@ -49,14 +47,6 @@ public class RibbonClientPreprocessorIntegrationTests { ...@@ -49,14 +47,6 @@ public class RibbonClientPreprocessorIntegrationTests {
private SpringClientFactory factory; private SpringClientFactory factory;
@Test @Test
public void serverListIsWrapped() throws Exception {
@SuppressWarnings("unchecked")
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo");
DomainExtractingServerList.class.cast(loadBalancer.getServerListImpl());
}
@Test
public void ruleDefaultsToAvailability() throws Exception { public void ruleDefaultsToAvailability() throws Exception {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
...@@ -75,17 +65,10 @@ public class RibbonClientPreprocessorIntegrationTests { ...@@ -75,17 +65,10 @@ public class RibbonClientPreprocessorIntegrationTests {
} }
@Configuration @Configuration
@RibbonClient("foo")
@Import({ PropertyPlaceholderAutoConfiguration.class,
ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class })
protected static class PlainConfiguration {
}
@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class) @RibbonClient(name = "foo", configuration = FooConfiguration.class)
@Import({ PropertyPlaceholderAutoConfiguration.class, @Import({ PropertyPlaceholderAutoConfiguration.class,
ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class }) ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class })
protected static class TestConfiguration { protected static class PlainConfiguration {
} }
@Configuration @Configuration
......
...@@ -26,7 +26,6 @@ import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration; ...@@ -26,7 +26,6 @@ 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.DomainExtractingServerList;
import org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration; import org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.eureka.ZonePreferenceServerListFilter;
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;
......
...@@ -14,13 +14,14 @@ ...@@ -14,13 +14,14 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.cloud.netflix.ribbon.eureka; package org.springframework.cloud.netflix.ribbon;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
......
/*
* 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.eureka;
import static org.junit.Assert.assertEquals;
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.ribbon.RibbonAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter;
import org.springframework.cloud.netflix.ribbon.eureka.RibbonClientPreprocessorIntegrationTests.TestConfiguration;
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;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAvoidanceRule;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
/**
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfiguration.class)
@DirtiesContext
public class RibbonClientPreprocessorIntegrationTests {
@Autowired
private SpringClientFactory factory;
@Test
public void serverListIsWrapped() throws Exception {
@SuppressWarnings("unchecked")
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo");
DomainExtractingServerList.class.cast(loadBalancer.getServerListImpl());
}
@Test
public void ruleDefaultsToAvoidance() throws Exception {
@SuppressWarnings("unchecked")
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo");
ZoneAvoidanceRule.class.cast(loadBalancer.getRule());
}
@Test
public void serverListFilterOverride() throws Exception {
@SuppressWarnings("unchecked")
ZoneAwareLoadBalancer<Server> loadBalancer = (ZoneAwareLoadBalancer<Server>) this.factory
.getLoadBalancer("foo");
assertEquals("myTestZone",
ZonePreferenceServerListFilter.class.cast(loadBalancer.getFilter())
.getZone());
}
@Configuration
@RibbonClient("foo")
@Import({ PropertyPlaceholderAutoConfiguration.class,
ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class })
protected static class PlainConfiguration {
}
@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class)
@Import({ PropertyPlaceholderAutoConfiguration.class,
ArchaiusAutoConfiguration.class, RibbonAutoConfiguration.class, RibbonEurekaAutoConfiguration.class })
protected static class TestConfiguration {
}
@Configuration
protected static class FooConfiguration {
@Bean
public ZonePreferenceServerListFilter serverListFilter() {
ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
filter.setZone("myTestZone");
return filter;
}
}
}
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