Commit 03d4b9ed by Nepxion

负载均衡过滤

parent 4eba424f
......@@ -15,12 +15,14 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.consul.discovery.ConsulServer;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.constant.ConsulConstant;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.netflix.loadbalancer.Server;
public class ConsulAdapter extends AbstractPluginAdapter {
@Autowired
......@@ -61,7 +63,7 @@ public class ConsulAdapter extends AbstractPluginAdapter {
return consulRegistration.getService().getAddress();
}
throw new PluginException("Registration instance isn't the type of Consul");
throw new PluginException("Registration instance isn't the type of ConsulRegistration");
}
@Override
......@@ -72,7 +74,18 @@ public class ConsulAdapter extends AbstractPluginAdapter {
return consulRegistration.getService().getPort();
}
throw new PluginException("Registration instance isn't the type of Consul");
throw new PluginException("Registration instance isn't the type of ConsulRegistration");
}
@Override
public String getServerVersion(Server server) {
if (server instanceof ConsulServer) {
ConsulServer consulServer = (ConsulServer) server;
return consulServer.getMetadata().get(PluginConstant.VERSION);
}
throw new PluginException("Server instance isn't the type of ConsulServer");
}
@Override
......
......@@ -9,6 +9,7 @@ package com.nepxion.discovery.plugin.framework.configuration;
* @version 1.0
*/
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -16,7 +17,22 @@ import com.nepxion.discovery.plugin.framework.adapter.ConsulAdapter;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
@Configuration
@RibbonClients(defaultConfiguration = ConsulLoadBalanceConfiguration.class)
public class ConsulAutoConfiguration {
static {
System.out.println("");
System.out.println("╔═══╗");
System.out.println("╚╗╔╗║");
System.out.println(" ║║║╠╦══╦══╦══╦╗╔╦══╦═╦╗ ╔╗");
System.out.println(" ║║║╠╣══╣╔═╣╔╗║╚╝║║═╣╔╣║ ║║");
System.out.println("╔╝╚╝║╠══║╚═╣╚╝╠╗╔╣║═╣║║╚═╝║");
System.out.println("╚═══╩╩══╩══╩══╝╚╝╚══╩╝╚═╗╔╝");
System.out.println(" ╔═╝║");
System.out.println(" ╚══╝");
System.out.println("Nepxion Discovery - Consul Plugin v3.2.2");
System.out.println("");
}
@Bean
public PluginAdapter pluginAdapter() {
return new ConsulAdapter();
......
package com.nepxion.discovery.plugin.framework.configuration;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.discovery.ConsulRibbonClientConfiguration;
import org.springframework.cloud.consul.discovery.ConsulServerList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ecwid.consul.v1.ConsulClient;
import com.nepxion.discovery.plugin.framework.decorator.ConsulServerListDecorator;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
@Configuration
@AutoConfigureAfter(ConsulRibbonClientConfiguration.class)
public class ConsulLoadBalanceConfiguration {
@Autowired
private ConsulClient client;
@Bean
public ServerList<?> ribbonServerList(IClientConfig config, ConsulDiscoveryProperties properties) {
ConsulServerList serverList = new ConsulServerListDecorator(client, properties);
serverList.initWithNiwsConfig(config);
return serverList;
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.decorator;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.discovery.ConsulServer;
import org.springframework.cloud.consul.discovery.ConsulServerList;
import org.springframework.core.env.ConfigurableEnvironment;
import com.ecwid.consul.v1.ConsulClient;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.listener.impl.LoadBalanceListenerExecutor;
public class ConsulServerListDecorator extends ConsulServerList {
@Autowired
private ConfigurableEnvironment environment;
@Autowired
private LoadBalanceListenerExecutor loadBalanceListenerExecutor;
public ConsulServerListDecorator(ConsulClient client, ConsulDiscoveryProperties properties) {
super(client, properties);
}
@Override
public List<ConsulServer> getInitialListOfServers() {
List<ConsulServer> servers = super.getInitialListOfServers();
filter(servers);
return servers;
}
@Override
public List<ConsulServer> getUpdatedListOfServers() {
List<ConsulServer> servers = super.getUpdatedListOfServers();
filter(servers);
return servers;
}
private void filter(List<ConsulServer> servers) {
Boolean discoveryControlEnabled = PluginContextAware.isDiscoveryControlEnabled(environment);
if (discoveryControlEnabled) {
String serviceId = getServiceId();
loadBalanceListenerExecutor.onGetServers(serviceId, servers);
}
}
}
\ No newline at end of file
......@@ -15,7 +15,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.listener.RegisterListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.register.RegisterListenerExecutor;
public class ConsulServiceRegistryDecorator extends ConsulServiceRegistry {
private ConsulServiceRegistry serviceRegistry;
......
......@@ -15,7 +15,10 @@ import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistrati
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.constant.EurekaConstant;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.netflix.loadbalancer.Server;
import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
public class EurekaAdapter extends AbstractPluginAdapter {
@Autowired
......@@ -29,7 +32,7 @@ public class EurekaAdapter extends AbstractPluginAdapter {
return eurekaRegistration.getInstanceConfig().getIpAddress();
}
throw new PluginException("Registration instance isn't the type of Eureka");
throw new PluginException("Registration instance isn't the type of EurekaRegistration");
}
@Override
......@@ -40,7 +43,18 @@ public class EurekaAdapter extends AbstractPluginAdapter {
return eurekaRegistration.getInstanceConfig().getNonSecurePort();
}
throw new PluginException("Registration instance isn't the type of Eureka");
throw new PluginException("Registration instance isn't the type of EurekaRegistration");
}
@Override
public String getServerVersion(Server server) {
if (server instanceof DiscoveryEnabledServer) {
DiscoveryEnabledServer discoveryEnabledServer = (DiscoveryEnabledServer) server;
return discoveryEnabledServer.getInstanceInfo().getMetadata().get(PluginConstant.VERSION);
}
throw new PluginException("Server instance isn't the type of DiscoveryEnabledServer");
}
@Override
......
......@@ -9,6 +9,7 @@ package com.nepxion.discovery.plugin.framework.configuration;
* @version 1.0
*/
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -16,7 +17,22 @@ import com.nepxion.discovery.plugin.framework.adapter.EurekaAdapter;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
@Configuration
@RibbonClients(defaultConfiguration = EurekaLoadBalanceConfiguration.class)
public class EurekaAutoConfiguration {
static {
System.out.println("");
System.out.println("╔═══╗");
System.out.println("╚╗╔╗║");
System.out.println(" ║║║╠╦══╦══╦══╦╗╔╦══╦═╦╗ ╔╗");
System.out.println(" ║║║╠╣══╣╔═╣╔╗║╚╝║║═╣╔╣║ ║║");
System.out.println("╔╝╚╝║╠══║╚═╣╚╝╠╗╔╣║═╣║║╚═╝║");
System.out.println("╚═══╩╩══╩══╩══╝╚╝╚══╩╝╚═╗╔╝");
System.out.println(" ╔═╝║");
System.out.println(" ╚══╝");
System.out.println("Nepxion Discovery - Eureka Plugin v3.2.2");
System.out.println("");
}
@Bean
public PluginAdapter pluginAdapter() {
return new EurekaAdapter();
......
package com.nepxion.discovery.plugin.framework.configuration;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import javax.inject.Provider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.ribbon.PropertiesFactory;
import org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList;
import org.springframework.cloud.netflix.ribbon.eureka.EurekaRibbonClientConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.plugin.framework.decorator.DiscoveryEnabledNIWSServerListDecorator;
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)
public class EurekaLoadBalanceConfiguration {
@Value("${ribbon.eureka.approximateZoneFromHostname:false}")
private boolean approximateZoneFromHostname = false;
@Value("${ribbon.client.name}")
private String serviceId = "client";
@Autowired
private PropertiesFactory propertiesFactory;
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config, Provider<EurekaClient> eurekaClientProvider) {
if (this.propertiesFactory.isSet(ServerList.class, serviceId)) {
return this.propertiesFactory.get(ServerList.class, config, serviceId);
}
DiscoveryEnabledNIWSServerList discoveryServerList = new DiscoveryEnabledNIWSServerListDecorator(config, eurekaClientProvider);
DomainExtractingServerList serverList = new DomainExtractingServerList(discoveryServerList, config, this.approximateZoneFromHostname);
return serverList;
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.decorator;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
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;
import com.nepxion.discovery.plugin.framework.listener.impl.LoadBalanceListenerExecutor;
import com.netflix.client.config.IClientConfig;
import com.netflix.discovery.EurekaClient;
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;
@Deprecated
public DiscoveryEnabledNIWSServerListDecorator() {
super();
}
@Deprecated
public DiscoveryEnabledNIWSServerListDecorator(String vipAddresses) {
super(vipAddresses);
}
@Deprecated
public DiscoveryEnabledNIWSServerListDecorator(IClientConfig clientConfig) {
super(clientConfig);
}
public DiscoveryEnabledNIWSServerListDecorator(String vipAddresses, Provider<EurekaClient> eurekaClientProvider) {
super(vipAddresses, eurekaClientProvider);
}
public DiscoveryEnabledNIWSServerListDecorator(IClientConfig clientConfig, Provider<EurekaClient> eurekaClientProvider) {
super(clientConfig, eurekaClientProvider);
}
@Override
public List<DiscoveryEnabledServer> getInitialListOfServers() {
List<DiscoveryEnabledServer> servers = super.getInitialListOfServers();
filter(servers);
return servers;
}
@Override
public List<DiscoveryEnabledServer> getUpdatedListOfServers() {
List<DiscoveryEnabledServer> servers = super.getUpdatedListOfServers();
filter(servers);
return servers;
}
private void filter(List<DiscoveryEnabledServer> servers) {
Boolean discoveryControlEnabled = PluginContextAware.isDiscoveryControlEnabled(environment);
if (discoveryControlEnabled) {
String serviceId = clientConfig.getClientName();
loadBalanceListenerExecutor.onGetServers(serviceId, servers);
}
}
}
\ No newline at end of file
......@@ -15,7 +15,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.listener.RegisterListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.register.RegisterListenerExecutor;
public class EurekaServiceRegistryDecorator extends EurekaServiceRegistry {
private EurekaServiceRegistry serviceRegistry;
......
......@@ -11,11 +11,14 @@ package com.nepxion.discovery.plugin.framework.adapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.zookeeper.discovery.ZookeeperServer;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.constant.ZookeeperConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.netflix.loadbalancer.Server;
public class ZookeeperAdapter extends AbstractPluginAdapter {
@Autowired
......@@ -29,7 +32,7 @@ public class ZookeeperAdapter extends AbstractPluginAdapter {
return zookeeperRegistration.getServiceInstance().getAddress();
}
throw new PluginException("Registration instance isn't the type of Zookeeper");
throw new PluginException("Registration instance isn't the type of ZookeeperRegistration");
}
@Override
......@@ -40,7 +43,18 @@ public class ZookeeperAdapter extends AbstractPluginAdapter {
return zookeeperRegistration.getServiceInstance().getPort();
}
throw new PluginException("Registration instance isn't the type of Zookeeper");
throw new PluginException("Registration instance isn't the type of ZookeeperRegistration");
}
@Override
public String getServerVersion(Server server) {
if (server instanceof ZookeeperServer) {
ZookeeperServer zookeeperServer = (ZookeeperServer) server;
return zookeeperServer.getInstance().getPayload().getMetadata().get(PluginConstant.VERSION);
}
throw new PluginException("Server instance isn't the type of ZookeeperServer");
}
@Override
......
......@@ -9,14 +9,30 @@ package com.nepxion.discovery.plugin.framework.configuration;
* @version 1.0
*/
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.plugin.framework.adapter.ZookeeperAdapter;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.adapter.ZookeeperAdapter;
@Configuration
@RibbonClients(defaultConfiguration = ZookeeperLoadBalanceConfiguration.class)
public class ZookeeperAutoConfiguration {
static {
System.out.println("");
System.out.println("╔═══╗");
System.out.println("╚╗╔╗║");
System.out.println(" ║║║╠╦══╦══╦══╦╗╔╦══╦═╦╗ ╔╗");
System.out.println(" ║║║╠╣══╣╔═╣╔╗║╚╝║║═╣╔╣║ ║║");
System.out.println("╔╝╚╝║╠══║╚═╣╚╝╠╗╔╣║═╣║║╚═╝║");
System.out.println("╚═══╩╩══╩══╩══╝╚╝╚══╩╝╚═╗╔╝");
System.out.println(" ╔═╝║");
System.out.println(" ╚══╝");
System.out.println("Nepxion Discovery - Zookeeper Plugin v3.2.2");
System.out.println("");
}
@Bean
public PluginAdapter pluginAdapter() {
return new ZookeeperAdapter();
......
package com.nepxion.discovery.plugin.framework.configuration;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
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 com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
@Configuration
@AutoConfigureAfter(ZookeeperRibbonClientConfiguration.class)
public class ZookeeperLoadBalanceConfiguration {
@Autowired
private ZookeeperServiceRegistry registry;
@Bean
public ServerList<?> ribbonServerList(IClientConfig config) {
@SuppressWarnings("deprecation")
ZookeeperServerList serverList = new ZookeeperServerList(this.registry.getServiceDiscoveryRef().get());
serverList.initWithNiwsConfig(config);
return serverList;
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.decorator;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
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;
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;
public ZookeeperServerListDecorator(ServiceDiscovery<ZookeeperInstance> serviceDiscovery) {
super(serviceDiscovery);
}
@Override
public List<ZookeeperServer> getInitialListOfServers() {
List<ZookeeperServer> servers = super.getInitialListOfServers();
filter(servers);
return servers;
}
@Override
public List<ZookeeperServer> getUpdatedListOfServers() {
List<ZookeeperServer> servers = super.getUpdatedListOfServers();
filter(servers);
return servers;
}
private void filter(List<ZookeeperServer> servers) {
Boolean discoveryControlEnabled = PluginContextAware.isDiscoveryControlEnabled(environment);
if (discoveryControlEnabled) {
String serviceId = clientConfig.getClientName();
loadBalanceListenerExecutor.onGetServers(serviceId, servers);
}
}
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.listener.RegisterListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.register.RegisterListenerExecutor;
public class ZookeeperServiceRegistryDecorator extends ZookeeperServiceRegistry {
private ZookeeperServiceRegistry serviceRegistry;
......
......@@ -36,6 +36,11 @@
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
......
......@@ -11,11 +11,15 @@ package com.nepxion.discovery.plugin.framework.adapter;
import org.springframework.cloud.client.serviceregistry.Registration;
import com.netflix.loadbalancer.Server;
public interface PluginAdapter {
String getIpAddress(Registration registration);
int getPort(Registration registration);
String getServerVersion(Server server);
String getVersion();
String getLocalVersion();
......
......@@ -19,29 +19,18 @@ import com.nepxion.discovery.plugin.framework.context.PluginContainerInitialized
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.listener.DiscoveryListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.RegisterListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.impl.CountFilterRegisterListener;
import com.nepxion.discovery.plugin.framework.listener.impl.IpAddressFilterDiscoveryListener;
import com.nepxion.discovery.plugin.framework.listener.impl.IpAddressFilterRegisterListener;
import com.nepxion.discovery.plugin.framework.listener.impl.VersionFilterDiscoveryListener;
import com.nepxion.discovery.plugin.framework.listener.discovery.DiscoveryListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.discovery.IpAddressFilterDiscoveryListener;
import com.nepxion.discovery.plugin.framework.listener.discovery.VersionFilterDiscoveryListener;
import com.nepxion.discovery.plugin.framework.listener.impl.IpAddressFilterLoadBalanceListener;
import com.nepxion.discovery.plugin.framework.listener.impl.LoadBalanceListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.impl.VersionFilterLoadBalanceListener;
import com.nepxion.discovery.plugin.framework.listener.register.CountFilterRegisterListener;
import com.nepxion.discovery.plugin.framework.listener.register.IpAddressFilterRegisterListener;
import com.nepxion.discovery.plugin.framework.listener.register.RegisterListenerExecutor;
@Configuration
public class PluginAutoConfiguration {
static {
System.out.println("");
System.out.println("╔═══╗");
System.out.println("╚╗╔╗║");
System.out.println(" ║║║╠╦══╦══╦══╦╗╔╦══╦═╦╗ ╔╗");
System.out.println(" ║║║╠╣══╣╔═╣╔╗║╚╝║║═╣╔╣║ ║║");
System.out.println("╔╝╚╝║╠══║╚═╣╚╝╠╗╔╣║═╣║║╚═╝║");
System.out.println("╚═══╩╩══╩══╩══╝╚╝╚══╩╝╚═╗╔╝");
System.out.println(" ╔═╝║");
System.out.println(" ╚══╝");
System.out.println("Nepxion Discovery v3.2.2");
System.out.println("");
}
@Bean
public PluginContainerInitializedHandler pluginContainerInitializedHandler() {
return new PluginContainerInitializedHandler();
......@@ -83,6 +72,11 @@ public class PluginAutoConfiguration {
}
@Bean
public LoadBalanceListenerExecutor loadBalanceListenerExecutor() {
return new LoadBalanceListenerExecutor();
}
@Bean
public IpAddressFilterRegisterListener ipAddressFilterRegisterListener() {
return new IpAddressFilterRegisterListener();
}
......@@ -101,4 +95,14 @@ public class PluginAutoConfiguration {
public VersionFilterDiscoveryListener versionFilterDiscoveryListener() {
return new VersionFilterDiscoveryListener();
}
@Bean
public IpAddressFilterLoadBalanceListener ipAddressFilterLoadBalanceListener() {
return new IpAddressFilterLoadBalanceListener();
}
@Bean
public VersionFilterLoadBalanceListener versionFilterLoadBalanceListener() {
return new VersionFilterLoadBalanceListener();
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.listener.DiscoveryListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.discovery.DiscoveryListenerExecutor;
public class DiscoveryClientDecorator implements DiscoveryClient {
private DiscoveryClient discoveryClient;
......
package com.nepxion.discovery.plugin.framework.listener;
package com.nepxion.discovery.plugin.framework.listener.discovery;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -12,6 +12,8 @@ package com.nepxion.discovery.plugin.framework.listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import com.nepxion.discovery.plugin.framework.listener.BasicListener;
public abstract class AbstractDiscoveryListener extends BasicListener implements DiscoveryListener {
@Autowired
protected DiscoveryClient discoveryClient;
......
package com.nepxion.discovery.plugin.framework.listener;
package com.nepxion.discovery.plugin.framework.listener.discovery;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -13,6 +13,8 @@ import java.util.List;
import org.springframework.cloud.client.ServiceInstance;
import com.nepxion.discovery.plugin.framework.listener.Listener;
public interface DiscoveryListener extends Listener {
void onGetInstances(String serviceId, List<ServiceInstance> instances);
......
package com.nepxion.discovery.plugin.framework.listener;
package com.nepxion.discovery.plugin.framework.listener.discovery;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -15,9 +15,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import com.nepxion.discovery.plugin.framework.listener.impl.IpAddressFilterDiscoveryListener;
import com.nepxion.discovery.plugin.framework.listener.impl.VersionFilterDiscoveryListener;
// 因为内置监听触发的时候,需要优先过滤,所以顺序执行
public class DiscoveryListenerExecutor {
@Autowired
......
package com.nepxion.discovery.plugin.framework.listener.impl;
package com.nepxion.discovery.plugin.framework.listener.discovery;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -22,7 +22,6 @@ import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.listener.AbstractDiscoveryListener;
public class IpAddressFilterDiscoveryListener extends AbstractDiscoveryListener {
@Autowired
......
package com.nepxion.discovery.plugin.framework.listener.impl;
package com.nepxion.discovery.plugin.framework.listener.discovery;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -26,7 +26,6 @@ import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity;
import com.nepxion.discovery.plugin.framework.entity.DiscoveryServiceEntity;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.entity.VersionEntity;
import com.nepxion.discovery.plugin.framework.listener.AbstractDiscoveryListener;
public class VersionFilterDiscoveryListener extends AbstractDiscoveryListener {
@Autowired
......
package com.nepxion.discovery.plugin.framework.listener.impl;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import com.nepxion.discovery.plugin.framework.listener.BasicListener;
public abstract class AbstractLoadBalanceListener extends BasicListener implements LoadBalanceListener {
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.listener.impl;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.netflix.loadbalancer.Server;
public class IpAddressFilterLoadBalanceListener extends AbstractLoadBalanceListener {
@Autowired
private RuleEntity ruleEntity;
@Override
public void onGetServers(String serviceId, List<? extends Server> servers) {
applyIpAddressFilter(serviceId, servers);
}
private void applyIpAddressFilter(String providerServiceId, List<? extends Server> servers) {
DiscoveryEntity discoveryEntity = ruleEntity.getDiscoveryEntity();
if (discoveryEntity == null) {
return;
}
FilterEntity filterEntity = discoveryEntity.getFilterEntity();
if (filterEntity == null) {
return;
}
FilterType filterType = filterEntity.getFilterType();
List<String> globalFilterValueList = filterEntity.getFilterValueList();
Map<String, List<String>> filterMap = filterEntity.getFilterMap();
List<String> filterValueList = filterMap.get(providerServiceId);
List<String> allFilterValueList = new ArrayList<String>();
if (CollectionUtils.isNotEmpty(globalFilterValueList)) {
allFilterValueList.addAll(globalFilterValueList);
}
if (CollectionUtils.isNotEmpty(filterValueList)) {
allFilterValueList.addAll(filterValueList);
}
Iterator<? extends Server> iterator = servers.iterator();
while (iterator.hasNext()) {
Server server = iterator.next();
String host = server.getHost();
switch (filterType) {
case BLACKLIST:
if (validateBlacklist(allFilterValueList, host)) {
iterator.remove();
}
break;
case WHITELIST:
if (validateWhitelist(allFilterValueList, host)) {
iterator.remove();
}
break;
}
}
}
private boolean validateBlacklist(List<String> allFilterValueList, String ipAddress) {
for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) {
return true;
}
}
return false;
}
private boolean validateWhitelist(List<String> allFilterValueList, String ipAddress) {
boolean matched = true;
for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) {
matched = false;
break;
}
}
return matched;
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.listener.impl;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.util.List;
import com.nepxion.discovery.plugin.framework.listener.Listener;
import com.netflix.loadbalancer.Server;
public interface LoadBalanceListener extends Listener {
void onGetServers(String serviceId, List<? extends Server> servers);
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.listener.impl;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.framework.listener.discovery.DiscoveryListener;
import com.netflix.loadbalancer.Server;
// 因为内置监听触发的时候,需要优先过滤,所以顺序执行
public class LoadBalanceListenerExecutor {
@Autowired
private IpAddressFilterLoadBalanceListener ipAddressFilterLoadBalanceListener;
@Autowired
private VersionFilterLoadBalanceListener versionFilterLoadBalanceListener;
@Autowired
private List<DiscoveryListener> discoveryListenerList;
@Autowired
private ReentrantReadWriteLock reentrantReadWriteLock;
public void onGetServers(String serviceId, List<? extends Server> servers) {
try {
reentrantReadWriteLock.readLock().lock();
ipAddressFilterLoadBalanceListener.onGetServers(serviceId, servers);
versionFilterLoadBalanceListener.onGetServers(serviceId, servers);
} finally {
reentrantReadWriteLock.readLock().unlock();
}
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.listener.impl;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity;
import com.nepxion.discovery.plugin.framework.entity.DiscoveryServiceEntity;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.entity.VersionEntity;
import com.netflix.loadbalancer.Server;
public class VersionFilterLoadBalanceListener extends AbstractLoadBalanceListener {
@Autowired
private RuleEntity ruleEntity;
@Autowired
private PluginAdapter pluginAdapter;
@Override
public void onGetServers(String serviceId, List<? extends Server> servers) {
String consumerServiceId = environment.getProperty(PluginConstant.SPRING_APPLICATION_NAME);
String consumerServiceVersion = pluginAdapter.getVersion();
applyVersionFilter(consumerServiceId, consumerServiceVersion, serviceId, servers);
}
private void applyVersionFilter(String consumerServiceId, String consumerServiceVersion, String providerServiceId, List<? extends Server> servers) {
// 如果消费端未配置版本号,那么它可以调用提供端所有服务,需要符合规范,极力避免该情况发生
if (StringUtils.isEmpty(consumerServiceVersion)) {
return;
}
DiscoveryEntity discoveryEntity = ruleEntity.getDiscoveryEntity();
if (discoveryEntity == null) {
return;
}
VersionEntity versionEntity = discoveryEntity.getVersionEntity();
if (versionEntity == null) {
return;
}
Map<String, List<DiscoveryServiceEntity>> serviceEntityMap = versionEntity.getServiceEntityMap();
if (MapUtils.isEmpty(serviceEntityMap)) {
return;
}
List<DiscoveryServiceEntity> serviceEntityList = serviceEntityMap.get(consumerServiceId);
if (CollectionUtils.isEmpty(serviceEntityList)) {
return;
}
// 当前版本的消费端所能调用提供端的版本号列表
List<String> allFilterValueList = new ArrayList<String>();
for (DiscoveryServiceEntity serviceEntity : serviceEntityList) {
String providerServiceName = serviceEntity.getProviderServiceName();
if (StringUtils.equals(providerServiceName, providerServiceId)) {
List<String> consumerVersionValueList = serviceEntity.getConsumerVersionValueList();
List<String> providerVersionValueList = serviceEntity.getProviderVersionValueList();
// 判断consumer-version-value值是否包含当前消费端的版本号
if (CollectionUtils.isNotEmpty(consumerVersionValueList)) {
if (consumerVersionValueList.contains(consumerServiceVersion)) {
if (CollectionUtils.isNotEmpty(providerVersionValueList)) {
allFilterValueList.addAll(providerVersionValueList);
}
}
} else {
if (CollectionUtils.isNotEmpty(providerVersionValueList)) {
allFilterValueList.addAll(providerVersionValueList);
}
}
}
}
// 未找到相应的版本定义或者未定义
if (CollectionUtils.isEmpty(allFilterValueList)) {
return;
}
Iterator<? extends Server> iterator = servers.iterator();
while (iterator.hasNext()) {
Server server = iterator.next();
String metaDataVersion = pluginAdapter.getServerVersion(server);
if (!allFilterValueList.contains(metaDataVersion)) {
iterator.remove();
}
}
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.listener;
package com.nepxion.discovery.plugin.framework.listener.register;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -12,6 +12,8 @@ package com.nepxion.discovery.plugin.framework.listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import com.nepxion.discovery.plugin.framework.listener.BasicListener;
public abstract class AbstractRegisterListener extends BasicListener implements RegisterListener {
@Autowired
protected ServiceRegistry<?> serviceRegistry;
......
package com.nepxion.discovery.plugin.framework.listener.impl;
package com.nepxion.discovery.plugin.framework.listener.register;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -23,7 +23,6 @@ import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.event.RegisterFailureEvent;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener;
public class CountFilterRegisterListener extends AbstractRegisterListener {
@Autowired
......
package com.nepxion.discovery.plugin.framework.listener.impl;
package com.nepxion.discovery.plugin.framework.listener.register;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -26,7 +26,6 @@ import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.event.RegisterFailureEvent;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener;
public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
@Autowired
......
package com.nepxion.discovery.plugin.framework.listener;
package com.nepxion.discovery.plugin.framework.listener.register;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -11,6 +11,8 @@ package com.nepxion.discovery.plugin.framework.listener;
import org.springframework.cloud.client.serviceregistry.Registration;
import com.nepxion.discovery.plugin.framework.listener.Listener;
public interface RegisterListener extends Listener {
void onRegister(Registration registration);
......
package com.nepxion.discovery.plugin.framework.listener;
package com.nepxion.discovery.plugin.framework.listener.register;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -15,9 +15,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import com.nepxion.discovery.plugin.framework.listener.impl.CountFilterRegisterListener;
import com.nepxion.discovery.plugin.framework.listener.impl.IpAddressFilterRegisterListener;
// 因为内置监听触发的时候,会抛异常处理,所以逆序执行
public class RegisterListenerExecutor {
@Autowired
......
......@@ -13,7 +13,7 @@ import java.util.List;
import org.springframework.cloud.client.ServiceInstance;
import com.nepxion.discovery.plugin.framework.listener.AbstractDiscoveryListener;
import com.nepxion.discovery.plugin.framework.listener.discovery.AbstractDiscoveryListener;
public class MyDiscoveryListener extends AbstractDiscoveryListener {
@Override
......
......@@ -11,7 +11,7 @@ package com.nepxion.discovery.plugin.example.extension;
import org.springframework.cloud.client.serviceregistry.Registration;
import com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener;
import com.nepxion.discovery.plugin.framework.listener.register.AbstractRegisterListener;
public class MyRegisterListener extends AbstractRegisterListener {
@Override
......
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