Commit 03b61bc3 by Nepxion

和Eureka解耦

parent 2c150489
......@@ -24,5 +24,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>discovery-plugin-framework-eureka</artifactId>
<name>Nepxion Discovery Plugin Framework</name>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<description>Nepxion Discovery is an enhancement for Spring Cloud Discovery</description>
<url>http://www.nepxion.com</url>
<parent>
<groupId>com.nepxion</groupId>
<artifactId>discovery</artifactId>
<version>2.0.11</version>
</parent>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.adapter;
/**
* <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.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.constant.EurekaConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
public class EurekaAdapter implements PluginAdapter {
@Autowired
protected ConfigurableEnvironment environment;
@Override
public String getIpAddress(Registration registration) {
if (registration instanceof EurekaRegistration) {
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
return eurekaRegistration.getInstanceConfig().getIpAddress();
}
throw new PluginException("Registration instance isn't the type of Eureka");
}
@Override
public String getVersion() {
return environment.getProperty(EurekaConstant.EUREKA_METADATA_VERSION);
}
}
\ No newline at end of file
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.plugin.framework.adapter.EurekaAdapter;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
@Configuration
public class EurekaAutoConfiguration {
@Bean
public PluginAdapter pluginAdapter() {
return new EurekaAdapter();
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.constant;
/**
* <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
*/
public class EurekaConstant {
public static final String EUREKA_METADATA_VERSION = "eureka.instance.metadataMap.version";
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.context;
/**
* <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.BeansException;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
import org.springframework.context.ConfigurableApplicationContext;
import com.nepxion.discovery.plugin.framework.decorator.DiscoveryClientDecorator;
import com.nepxion.discovery.plugin.framework.decorator.EurekaServiceRegistryDecorator;
public class EurekaApplicationContextInitializer extends PluginApplicationContextInitializer {
@Override
protected Object afterInitialization(ConfigurableApplicationContext applicationContext, Object bean, String beanName) throws BeansException {
if (bean instanceof DiscoveryClient) {
DiscoveryClient discoveryClient = (DiscoveryClient) bean;
return new DiscoveryClientDecorator(discoveryClient, applicationContext);
} else if (bean instanceof EurekaServiceRegistry) {
EurekaServiceRegistry eurekaServiceRegistry = (EurekaServiceRegistry) bean;
return new EurekaServiceRegistryDecorator(eurekaServiceRegistry, applicationContext);
} else if (bean instanceof EurekaInstanceConfigBean) {
EurekaInstanceConfigBean instanceConfig = (EurekaInstanceConfigBean) bean;
instanceConfig.setPreferIpAddress(true);
return bean;
} else {
return bean;
}
}
}
\ No newline at end of file
......@@ -32,7 +32,7 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
......
package com.nepxion.discovery.plugin.framework.adapter;
/**
* <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.cloud.client.serviceregistry.Registration;
public interface PluginAdapter {
String getIpAddress(Registration registration);
String getVersion();
}
\ No newline at end of file
......@@ -15,7 +15,6 @@ public class PluginConstant {
public static final String SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED = "spring.application.discovery.remote.config.enabled";
public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final String EUREKA_METADATA_VERSION = "eureka.instance.metadataMap.version";
public static final String SERVICE_ID = "serviceId";
public static final String VERSION = "version";
public static final String METADATA = "metadata";
......
......@@ -11,35 +11,19 @@ package com.nepxion.discovery.plugin.framework.context;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import com.nepxion.discovery.plugin.framework.decorator.DiscoveryClientDecorator;
import com.nepxion.discovery.plugin.framework.decorator.EurekaServiceRegistryDecorator;
public class PluginApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public abstract class PluginApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DiscoveryClient) {
DiscoveryClient discoveryClient = (DiscoveryClient) bean;
return new DiscoveryClientDecorator(discoveryClient, applicationContext);
} else if (bean instanceof EurekaServiceRegistry) {
EurekaServiceRegistry eurekaServiceRegistry = (EurekaServiceRegistry) bean;
return new EurekaServiceRegistryDecorator(eurekaServiceRegistry, applicationContext);
} else if (bean instanceof EurekaInstanceConfigBean) {
EurekaInstanceConfigBean instanceConfig = (EurekaInstanceConfigBean) bean;
instanceConfig.setPreferIpAddress(true);
return bean;
} else {
return bean;
}
return afterInitialization(applicationContext, bean, beanName);
}
});
}
protected abstract Object afterInitialization(ConfigurableApplicationContext applicationContext, Object bean, String beanName) throws BeansException;
}
\ No newline at end of file
......@@ -46,8 +46,4 @@ public class PluginContextAware implements ApplicationContextAware {
public static Boolean isRemoteConfigEnabled(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED, Boolean.class, Boolean.TRUE);
}
public Environment getEnvironment() {
return environment;
}
}
\ No newline at end of file
......@@ -18,8 +18,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.entity.FilterEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.RegisterEntity;
......@@ -33,15 +33,13 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
@Autowired
private RuleEntity ruleEntity;
@Autowired
private PluginAdapter pluginAdapter;
@Override
public void onRegister(Registration registration) {
String serviceId = registration.getServiceId();
String ipAddress = null;
if (registration instanceof EurekaRegistration) {
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
ipAddress = eurekaRegistration.getInstanceConfig().getIpAddress();
}
String ipAddress = pluginAdapter.getIpAddress(registration);
applyIpAddressFilter(serviceId, ipAddress);
}
......@@ -87,7 +85,7 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) {
throw new PluginException(ipAddress + " isn't allowed to register to Eureka server, because it is in blacklist");
throw new PluginException(ipAddress + " isn't allowed to register to Discovery server, because it is in blacklist");
}
}
}
......@@ -104,7 +102,7 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
}
if (matched) {
throw new PluginException(ipAddress + " isn't allowed to register to Eureka server, because it isn't in whitelist");
throw new PluginException(ipAddress + " isn't allowed to register to Discovery server, because it isn't in whitelist");
}
}
......
......@@ -20,6 +20,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
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;
......@@ -31,10 +32,13 @@ public class VersionFilterDiscoveryListener extends AbstractDiscoveryListener {
@Autowired
private RuleEntity ruleEntity;
@Autowired
private PluginAdapter pluginAdapter;
@Override
public void onGetInstances(String serviceId, List<ServiceInstance> instances) {
String consumerServiceId = environment.getProperty(PluginConstant.SPRING_APPLICATION_NAME);
String consumerServiceVersion = environment.getProperty(PluginConstant.EUREKA_METADATA_VERSION);
String consumerServiceVersion = pluginAdapter.getVersion();
applyVersionFilter(consumerServiceId, consumerServiceVersion, serviceId, instances);
}
......
......@@ -19,5 +19,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -19,6 +19,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -27,9 +28,9 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.context.PluginContainerInitializedHandler;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RouterEntity;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.nepxion.eventbus.util.HostUtil;
......@@ -40,7 +41,10 @@ public class RouterController {
private PluginContainerInitializedHandler pluginContainerInitializedHandler;
@Autowired
private PluginContextAware pluginContextAware;
private PluginAdapter pluginAdapter;
@Autowired
protected ConfigurableEnvironment environment;
@Autowired
private RestTemplate routerRestTemplate;
......@@ -83,8 +87,8 @@ public class RouterController {
}
public RouterEntity getRouterEntity() {
String serviceId = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_NAME);
String version = pluginContextAware.getEnvironment().getProperty(PluginConstant.EUREKA_METADATA_VERSION);
String serviceId = environment.getProperty(PluginConstant.SPRING_APPLICATION_NAME);
String version = pluginAdapter.getVersion();
String host = HostUtil.getLocalhost();
int port = pluginContainerInitializedHandler.getPort();
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>discovery-plugin-starter</artifactId>
<name>Nepxion Discovery Plugin Starter</name>
<artifactId>discovery-plugin-starter-eureka</artifactId>
<name>Nepxion Discovery Plugin Starter Eureka</name>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<description>Nepxion Discovery is an enhancement for Spring Cloud Discovery</description>
......@@ -29,5 +29,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-router-center</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework-eureka</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
org.springframework.context.ApplicationContextInitializer=\
com.nepxion.discovery.plugin.framework.context.PluginApplicationContextInitializer
com.nepxion.discovery.plugin.framework.context.EurekaApplicationContextInitializer
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.plugin.framework.configuration.PluginAutoConfiguration,\
com.nepxion.discovery.plugin.framework.configuration.EurekaAutoConfiguration,\
com.nepxion.discovery.plugin.configcenter.configuration.ConfigAutoConfiguration,\
com.nepxion.discovery.plugin.admincenter.configuration.AdminAutoConfiguration,\
com.nepxion.discovery.plugin.routercenter.configuration.RouterAutoConfiguration
\ No newline at end of file
......@@ -39,7 +39,7 @@
<dependencies>
<dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-starter</artifactId>
<artifactId>discovery-plugin-starter-eureka</artifactId>
<version>${discovery.plugin.version}</version>
</dependency>
</dependencies>
......
......@@ -39,7 +39,7 @@
<dependencies>
<dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-starter</artifactId>
<artifactId>discovery-plugin-starter-eureka</artifactId>
<version>${discovery.plugin.version}</version>
</dependency>
</dependencies>
......
......@@ -12,10 +12,11 @@
<modules>
<module>discovery-plugin-framework</module>
<module>discovery-plugin-framework-eureka</module>
<module>discovery-plugin-config-center</module>
<module>discovery-plugin-admin-center</module>
<module>discovery-plugin-router-center</module>
<module>discovery-plugin-starter</module>
<module>discovery-plugin-starter-eureka</module>
<module>discovery-springcloud-example-eureka</module>
<module>discovery-springcloud-example-a</module>
<module>discovery-springcloud-example-b</module>
......@@ -47,6 +48,12 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework-eureka</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-config-center</artifactId>
<version>${project.version}</version>
</dependency>
......@@ -65,7 +72,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-starter</artifactId>
<artifactId>discovery-plugin-starter-eureka</artifactId>
<version>${project.version}</version>
</dependency>
......
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