updated to use new RibbonClient injection

fixes gh-8
This commit is contained in:
Spencer Gibb
2015-02-19 11:20:23 -07:00
parent 2313a635a3
commit 0a5d8ae2ba

View File

@@ -16,24 +16,23 @@
package org.springframework.cloud.consul.discovery;
import static com.netflix.client.config.CommonClientConfigKey.*;
import static com.netflix.client.config.CommonClientConfigKey.DeploymentContextBasedVipAddresses;
import static com.netflix.client.config.CommonClientConfigKey.EnableZoneAffinity;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
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.consul.client.CatalogClient;
import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.netflix.client.config.IClientConfig;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ZoneAvoidanceRule;
/**
* Preprocessor that configures defaults for eureka-discovered ribbon clients. Such as:
@@ -44,7 +43,7 @@ import com.netflix.loadbalancer.ZoneAvoidanceRule;
* @author Dave Syer
*/
@Configuration
public class ConsulRibbonClientConfiguration implements BeanPostProcessor {
public class ConsulRibbonClientConfiguration {
@Autowired
CatalogClient client;
@@ -63,46 +62,20 @@ public class ConsulRibbonClientConfiguration implements BeanPostProcessor {
this.serviceId = serviceId;
}
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config) {
ConsulServerList serverList = new ConsulServerList(client, serviceId);
return serverList;
}
@PostConstruct
public void preprocess() {
// TODO: should this look more like hibernate spring boot props?
setProp(this.serviceId, NIWSServerListClassName.key(),
ConsulServerList.class.getName());
// FIXME: what should this be?
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");
}
@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) {
// TODO: fix this set client hack
@SuppressWarnings("unchecked")
DynamicServerListLoadBalancer<ConsulServer> dynamic = (DynamicServerListLoadBalancer<ConsulServer>) balancer;
ServerList<ConsulServer> list = dynamic.getServerListImpl();
if (list instanceof ConsulServerList) {
ConsulServerList csl = (ConsulServerList) list;
csl.setClient(client);
}
}
protected void setProp(String serviceId, String suffix, String value) {
// how to set the namespace properly?
String key = getKey(serviceId, suffix);