Commit 447da0dd by Nepxion

重构类结构

parent cbac75f0
......@@ -20,12 +20,13 @@ import org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerLis
import org.springframework.cloud.netflix.ribbon.eureka.EurekaRibbonClientConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.decorator.DiscoveryEnabledNIWSServerListDecorator;
import com.nepxion.discovery.plugin.framework.listener.impl.LoadBalanceListenerExecutor;
import com.netflix.client.config.IClientConfig;
import com.netflix.discovery.EurekaClient;
import com.netflix.loadbalancer.ServerList;
import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
@Configuration
@AutoConfigureAfter(EurekaRibbonClientConfiguration.class)
......@@ -39,6 +40,12 @@ public class EurekaLoadBalanceConfiguration {
@Autowired
private PropertiesFactory propertiesFactory;
@Autowired
private ConfigurableEnvironment environment;
@Autowired
private LoadBalanceListenerExecutor loadBalanceListenerExecutor;
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config, Provider<EurekaClient> eurekaClientProvider) {
......@@ -46,7 +53,11 @@ public class EurekaLoadBalanceConfiguration {
return this.propertiesFactory.get(ServerList.class, config, serviceId);
}
DiscoveryEnabledNIWSServerList discoveryServerList = new DiscoveryEnabledNIWSServerListDecorator(config, eurekaClientProvider);
DiscoveryEnabledNIWSServerListDecorator discoveryServerList = new DiscoveryEnabledNIWSServerListDecorator(config, eurekaClientProvider);
discoveryServerList.setEnvironment(environment);
discoveryServerList.setLoadBalanceListenerExecutor(loadBalanceListenerExecutor);
discoveryServerList.setServiceId(config.getClientName());
DomainExtractingServerList serverList = new DomainExtractingServerList(discoveryServerList, config, this.approximateZoneFromHostname);
return serverList;
......
......@@ -13,7 +13,6 @@ import java.util.List;
import javax.inject.Provider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
......@@ -24,14 +23,11 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
public class DiscoveryEnabledNIWSServerListDecorator extends DiscoveryEnabledNIWSServerList {
@Autowired
private ConfigurableEnvironment environment;
@Autowired
private LoadBalanceListenerExecutor loadBalanceListenerExecutor;
@Autowired
private IClientConfig clientConfig;
private String serviceId;
@Deprecated
public DiscoveryEnabledNIWSServerListDecorator() {
......@@ -77,8 +73,19 @@ public class DiscoveryEnabledNIWSServerListDecorator extends DiscoveryEnabledNIW
private void filter(List<DiscoveryEnabledServer> servers) {
Boolean discoveryControlEnabled = PluginContextAware.isDiscoveryControlEnabled(environment);
if (discoveryControlEnabled) {
String serviceId = clientConfig.getClientName();
loadBalanceListenerExecutor.onGetServers(serviceId, servers);
}
}
public void setEnvironment(ConfigurableEnvironment environment) {
this.environment = environment;
}
public void setLoadBalanceListenerExecutor(LoadBalanceListenerExecutor loadBalanceListenerExecutor) {
this.loadBalanceListenerExecutor = loadBalanceListenerExecutor;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
}
\ No newline at end of file
......@@ -12,11 +12,13 @@ package com.nepxion.discovery.plugin.framework.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.cloud.zookeeper.discovery.ZookeeperRibbonClientConfiguration;
import org.springframework.cloud.zookeeper.discovery.ZookeeperServerList;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperServiceRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.decorator.ZookeeperServerListDecorator;
import com.nepxion.discovery.plugin.framework.listener.impl.LoadBalanceListenerExecutor;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
......@@ -24,13 +26,22 @@ import com.netflix.loadbalancer.ServerList;
@AutoConfigureAfter(ZookeeperRibbonClientConfiguration.class)
public class ZookeeperLoadBalanceConfiguration {
@Autowired
private ConfigurableEnvironment environment;
@Autowired
private LoadBalanceListenerExecutor loadBalanceListenerExecutor;
@Autowired
private ZookeeperServiceRegistry registry;
@Bean
public ServerList<?> ribbonServerList(IClientConfig config) {
@SuppressWarnings("deprecation")
ZookeeperServerList serverList = new ZookeeperServerList(this.registry.getServiceDiscoveryRef().get());
ZookeeperServerListDecorator serverList = new ZookeeperServerListDecorator(this.registry.getServiceDiscoveryRef().get());
serverList.initWithNiwsConfig(config);
serverList.setEnvironment(environment);
serverList.setLoadBalanceListenerExecutor(loadBalanceListenerExecutor);
serverList.setServiceId(config.getClientName());
return serverList;
}
......
......@@ -12,7 +12,6 @@ package com.nepxion.discovery.plugin.framework.decorator;
import java.util.List;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance;
import org.springframework.cloud.zookeeper.discovery.ZookeeperServer;
import org.springframework.cloud.zookeeper.discovery.ZookeeperServerList;
......@@ -20,17 +19,13 @@ import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.listener.impl.LoadBalanceListenerExecutor;
import com.netflix.client.config.IClientConfig;
public class ZookeeperServerListDecorator extends ZookeeperServerList {
@Autowired
private ConfigurableEnvironment environment;
@Autowired
private LoadBalanceListenerExecutor loadBalanceListenerExecutor;
@Autowired
private IClientConfig clientConfig;
private String serviceId;
public ZookeeperServerListDecorator(ServiceDiscovery<ZookeeperInstance> serviceDiscovery) {
super(serviceDiscovery);
......@@ -57,8 +52,19 @@ public class ZookeeperServerListDecorator extends ZookeeperServerList {
private void filter(List<ZookeeperServer> servers) {
Boolean discoveryControlEnabled = PluginContextAware.isDiscoveryControlEnabled(environment);
if (discoveryControlEnabled) {
String serviceId = clientConfig.getClientName();
loadBalanceListenerExecutor.onGetServers(serviceId, servers);
}
}
public void setEnvironment(ConfigurableEnvironment environment) {
this.environment = environment;
}
public void setLoadBalanceListenerExecutor(LoadBalanceListenerExecutor loadBalanceListenerExecutor) {
this.loadBalanceListenerExecutor = loadBalanceListenerExecutor;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
}
\ No newline at end of file
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