Commit 447da0dd by Nepxion

重构类结构

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