Commit fcb3daf9 by Nepxion

增加对Zookeeper服务注册发现的支持

parent b608dda5
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> 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> <artifactId>discovery-plugin-framework-eureka</artifactId>
<name>Nepxion Discovery Plugin Framework</name> <name>Nepxion Discovery Plugin Framework Eureka</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<description>Nepxion Discovery is an enhancement for Spring Cloud Discovery</description> <description>Nepxion Discovery is an enhancement for Spring Cloud Discovery</description>
......
...@@ -9,7 +9,6 @@ package com.nepxion.discovery.plugin.framework.decorator; ...@@ -9,7 +9,6 @@ package com.nepxion.discovery.plugin.framework.decorator;
* @version 1.0 * @version 1.0
*/ */
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
...@@ -19,11 +18,11 @@ import com.nepxion.discovery.plugin.framework.context.PluginContextAware; ...@@ -19,11 +18,11 @@ import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.listener.RegisterListenerExecutor; import com.nepxion.discovery.plugin.framework.listener.RegisterListenerExecutor;
public class EurekaServiceRegistryDecorator extends EurekaServiceRegistry { public class EurekaServiceRegistryDecorator extends EurekaServiceRegistry {
private ServiceRegistry<EurekaRegistration> serviceRegistry; private EurekaServiceRegistry serviceRegistry;
private ConfigurableApplicationContext applicationContext; private ConfigurableApplicationContext applicationContext;
private ConfigurableEnvironment environment; private ConfigurableEnvironment environment;
public EurekaServiceRegistryDecorator(ServiceRegistry<EurekaRegistration> serviceRegistry, ConfigurableApplicationContext applicationContext) { public EurekaServiceRegistryDecorator(EurekaServiceRegistry serviceRegistry, ConfigurableApplicationContext applicationContext) {
this.serviceRegistry = serviceRegistry; this.serviceRegistry = serviceRegistry;
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
this.environment = applicationContext.getEnvironment(); this.environment = applicationContext.getEnvironment();
......
<?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-zookeeper</artifactId>
<name>Nepxion Discovery Plugin Framework Zookeeper</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-zookeeper-discovery</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.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.constant.ZookeeperConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
public class ZookeeperAdapter implements PluginAdapter {
@Autowired
protected ConfigurableEnvironment environment;
@Override
public String getIpAddress(Registration registration) {
if (registration instanceof ZookeeperRegistration) {
ZookeeperRegistration zookeeperRegistration = (ZookeeperRegistration) registration;
return zookeeperRegistration.getServiceInstance().getAddress();
}
throw new PluginException("Registration instance isn't the type of Zookeeper");
}
@Override
public String getVersion() {
return environment.getProperty(ZookeeperConstant.ZOOKEEPER_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.ZookeeperAdapter;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
@Configuration
public class ZookeeperAutoConfiguration {
@Bean
public PluginAdapter pluginAdapter() {
return new ZookeeperAdapter();
}
}
\ 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 ZookeeperConstant {
public static final String ZOOKEEPER_METADATA_VERSION = "spring.cloud.zookeeper.discovery.metadata.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.zookeeper.serviceregistry.ZookeeperServiceRegistry;
import org.springframework.context.ConfigurableApplicationContext;
import com.nepxion.discovery.plugin.framework.decorator.DiscoveryClientDecorator;
import com.nepxion.discovery.plugin.framework.decorator.ZookeeperServiceRegistryDecorator;
public class ZookeeperApplicationContextInitializer 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 ZookeeperServiceRegistry) {
ZookeeperServiceRegistry zookeeperServiceRegistry = (ZookeeperServiceRegistry) bean;
return new ZookeeperServiceRegistryDecorator(zookeeperServiceRegistry, applicationContext);
} else {
return bean;
}
}
}
\ 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 org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperServiceRegistry;
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;
public class ZookeeperServiceRegistryDecorator extends ZookeeperServiceRegistry {
private ZookeeperServiceRegistry serviceRegistry;
private ConfigurableApplicationContext applicationContext;
private ConfigurableEnvironment environment;
public ZookeeperServiceRegistryDecorator(ZookeeperServiceRegistry serviceRegistry, ConfigurableApplicationContext applicationContext) {
super(null);
this.serviceRegistry = serviceRegistry;
this.applicationContext = applicationContext;
this.environment = applicationContext.getEnvironment();
}
@Override
public void register(ZookeeperRegistration registration) {
Boolean registerControlEnabled = PluginContextAware.isRegisterControlEnabled(environment);
if (registerControlEnabled) {
RegisterListenerExecutor registerListenerExecutor = applicationContext.getBean(RegisterListenerExecutor.class);
registerListenerExecutor.onRegister(registration);
}
serviceRegistry.register(registration);
}
@Override
public void deregister(ZookeeperRegistration registration) {
Boolean registerControlEnabled = PluginContextAware.isRegisterControlEnabled(environment);
if (registerControlEnabled) {
RegisterListenerExecutor registerListenerExecutor = applicationContext.getBean(RegisterListenerExecutor.class);
registerListenerExecutor.onDeregister(registration);
}
serviceRegistry.deregister(registration);
}
@Override
public void setStatus(ZookeeperRegistration registration, String status) {
Boolean registerControlEnabled = PluginContextAware.isRegisterControlEnabled(environment);
if (registerControlEnabled) {
RegisterListenerExecutor registerListenerExecutor = applicationContext.getBean(RegisterListenerExecutor.class);
registerListenerExecutor.onSetStatus(registration, status);
}
serviceRegistry.setStatus(registration, status);
}
@Override
public Object getStatus(ZookeeperRegistration registration) {
return serviceRegistry.getStatus(registration);
}
@Override
public void close() {
Boolean registerControlEnabled = PluginContextAware.isRegisterControlEnabled(environment);
if (registerControlEnabled) {
RegisterListenerExecutor registerListenerExecutor = applicationContext.getBean(RegisterListenerExecutor.class);
registerListenerExecutor.onClose();
}
serviceRegistry.close();
}
@Override
public void afterSingletonsInstantiated() {
serviceRegistry.afterSingletonsInstantiated();
}
public ConfigurableEnvironment getEnvironment() {
return environment;
}
}
\ 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-starter-zookeeper</artifactId>
<name>Nepxion Discovery Plugin Starter Zookeeper</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-config-center</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-admin-center</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-router-center</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework-zookeeper</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
org.springframework.context.ApplicationContextInitializer=\
com.nepxion.discovery.plugin.framework.context.ZookeeperApplicationContextInitializer
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.plugin.framework.configuration.PluginAutoConfiguration,\
com.nepxion.discovery.plugin.framework.configuration.ZookeeperAutoConfiguration,\
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,8 @@ ...@@ -39,7 +39,8 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.nepxion</groupId> <groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-starter-eureka</artifactId> <!-- <artifactId>discovery-plugin-starter-eureka</artifactId> -->
<artifactId>discovery-plugin-starter-zookeeper</artifactId>
<version>${discovery.plugin.version}</version> <version>${discovery.plugin.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
......
...@@ -7,6 +7,11 @@ eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/ ...@@ -7,6 +7,11 @@ eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/
eureka.instance.preferIpAddress=true eureka.instance.preferIpAddress=true
eureka.instance.metadataMap.version=1.0 eureka.instance.metadataMap.version=1.0
# Zookeeper config
spring.cloud.zookeeper.connectString=localhost:2181
spring.cloud.zookeeper.discovery.root=/spring-cloud-service
spring.cloud.zookeeper.discovery.metadata.version=1.0
# Plugin config # Plugin config
# 开启和关闭服务注册层面的控制。一旦关闭,服务注册的黑/白名单过滤功能将失效。缺失则默认为true # 开启和关闭服务注册层面的控制。一旦关闭,服务注册的黑/白名单过滤功能将失效。缺失则默认为true
spring.application.register.control.enabled=true spring.application.register.control.enabled=true
......
...@@ -39,7 +39,8 @@ ...@@ -39,7 +39,8 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.nepxion</groupId> <groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-starter-eureka</artifactId> <!-- <artifactId>discovery-plugin-starter-eureka</artifactId> -->
<artifactId>discovery-plugin-starter-zookeeper</artifactId>
<version>${discovery.plugin.version}</version> <version>${discovery.plugin.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
......
# Spring cloud config # Spring cloud config
server.port=1200 server.port=1200
# Eureka config
eureka.instance.metadataMap.version=1.0 eureka.instance.metadataMap.version=1.0
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.0
# Admin config
management.port=8200 management.port=8200
\ No newline at end of file
# Spring cloud config # Spring cloud config
server.port=1201 server.port=1201
# Eureka config
eureka.instance.metadataMap.version=1.1 eureka.instance.metadataMap.version=1.1
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.1
# Admin config
management.port=8201 management.port=8201
\ No newline at end of file
...@@ -5,4 +5,8 @@ spring.application.name=discovery-springcloud-example-b ...@@ -5,4 +5,8 @@ spring.application.name=discovery-springcloud-example-b
eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/ eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/
eureka.instance.preferIpAddress=true eureka.instance.preferIpAddress=true
# Zookeeper config
spring.cloud.zookeeper.connectString=localhost:2181
spring.cloud.zookeeper.discovery.root=/spring-cloud-service
management.security.enabled=false management.security.enabled=false
\ No newline at end of file
...@@ -36,9 +36,16 @@ ...@@ -36,9 +36,16 @@
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<!-- Zookeeper discovery needs spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId> <!-- <artifactId>spring-cloud-starter-eureka</artifactId> -->
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
......
# Spring cloud config # Spring cloud config
server.port=1300 server.port=1300
eureka.instance.metadataMap.version=1.0
\ No newline at end of file # Eureka config
eureka.instance.metadataMap.version=1.0
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.0
\ No newline at end of file
# Spring cloud config # Spring cloud config
server.port=1301 server.port=1301
eureka.instance.metadataMap.version=1.1
\ No newline at end of file # Eureka config
eureka.instance.metadataMap.version=1.1
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.1
\ No newline at end of file
# Spring cloud config # Spring cloud config
server.port=1302 server.port=1302
eureka.instance.metadataMap.version=1.2
\ No newline at end of file # Eureka config
eureka.instance.metadataMap.version=1.2
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.2
\ No newline at end of file
...@@ -3,4 +3,8 @@ spring.application.name=discovery-springcloud-example-c ...@@ -3,4 +3,8 @@ spring.application.name=discovery-springcloud-example-c
# Eureka config # Eureka config
eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/ eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/
eureka.instance.preferIpAddress=true eureka.instance.preferIpAddress=true
\ No newline at end of file
# Zookeeper config
spring.cloud.zookeeper.connectString=localhost:2181
spring.cloud.zookeeper.discovery.root=/spring-cloud-service
\ No newline at end of file
...@@ -13,10 +13,12 @@ ...@@ -13,10 +13,12 @@
<modules> <modules>
<module>discovery-plugin-framework</module> <module>discovery-plugin-framework</module>
<module>discovery-plugin-framework-eureka</module> <module>discovery-plugin-framework-eureka</module>
<module>discovery-plugin-framework-zookeeper</module>
<module>discovery-plugin-config-center</module> <module>discovery-plugin-config-center</module>
<module>discovery-plugin-admin-center</module> <module>discovery-plugin-admin-center</module>
<module>discovery-plugin-router-center</module> <module>discovery-plugin-router-center</module>
<module>discovery-plugin-starter-eureka</module> <module>discovery-plugin-starter-eureka</module>
<module>discovery-plugin-starter-zookeeper</module>
<module>discovery-springcloud-example-eureka</module> <module>discovery-springcloud-example-eureka</module>
<module>discovery-springcloud-example-a</module> <module>discovery-springcloud-example-a</module>
<module>discovery-springcloud-example-b</module> <module>discovery-springcloud-example-b</module>
...@@ -30,6 +32,7 @@ ...@@ -30,6 +32,7 @@
<commons.io.version>2.5</commons.io.version> <commons.io.version>2.5</commons.io.version>
<dom4j.version>1.6.1</dom4j.version> <dom4j.version>1.6.1</dom4j.version>
<spring.cloud.version>Dalston.SR5</spring.cloud.version> <spring.cloud.version>Dalston.SR5</spring.cloud.version>
<spring.cloud.zookeeper.version>2.0.0.RELEASE</spring.cloud.zookeeper.version>
<!-- <spring.cloud.version>Edgware.SR3</spring.cloud.version> --> <!-- <spring.cloud.version>Edgware.SR3</spring.cloud.version> -->
<spring.boot.version>1.5.8.RELEASE</spring.boot.version> <spring.boot.version>1.5.8.RELEASE</spring.boot.version>
<disruptor.version>3.3.7</disruptor.version> <disruptor.version>3.3.7</disruptor.version>
...@@ -54,6 +57,12 @@ ...@@ -54,6 +57,12 @@
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework-zookeeper</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-config-center</artifactId> <artifactId>discovery-plugin-config-center</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
...@@ -78,6 +87,12 @@ ...@@ -78,6 +87,12 @@
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-starter-zookeeper</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>eventbus-aop-starter</artifactId> <artifactId>eventbus-aop-starter</artifactId>
<version>${eventbus.version}</version> <version>${eventbus.version}</version>
</dependency> </dependency>
...@@ -115,6 +130,14 @@ ...@@ -115,6 +130,14 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-dependencies</artifactId>
<version>${spring.cloud.zookeeper.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId> <artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version> <version>${spring.boot.version}</version>
......
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