Commit 09f5e762 by Nepxion

支持Consule服务注册发现

parent 95ffb6a9
<?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-consul</artifactId>
<name>Nepxion Discovery Plugin Framework Consul</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-consul-all</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.consul.serviceregistry.ConsulRegistration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.framework.constant.ConsulConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
public class ConsulAdapter implements PluginAdapter {
@Autowired
protected ConfigurableEnvironment environment;
@Override
public String getIpAddress(Registration registration) {
if (registration instanceof ConsulRegistration) {
ConsulRegistration consulRegistration = (ConsulRegistration) registration;
return consulRegistration.getService().getAddress();
}
throw new PluginException("Registration instance isn't the type of Consul");
}
@Override
public String getVersion() {
return environment.getProperty(ConsulConstant.CONSUL_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.ConsulAdapter;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
@Configuration
public class ConsulAutoConfiguration {
@Bean
public PluginAdapter pluginAdapter() {
return new ConsulAdapter();
}
}
\ 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 ConsulConstant {
public static final String CONSUL_METADATA_VERSION = "spring.cloud.consul.discovery.tags.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.consul.serviceregistry.ConsulServiceRegistry;
import org.springframework.context.ConfigurableApplicationContext;
import com.nepxion.discovery.plugin.framework.decorator.ConsulServiceRegistryDecorator;
public class ConsulApplicationContextInitializer extends PluginApplicationContextInitializer {
@Override
protected Object afterInitialization(ConfigurableApplicationContext applicationContext, Object bean, String beanName) throws BeansException {
if (bean instanceof ConsulServiceRegistry) {
ConsulServiceRegistry consulServiceRegistry = (ConsulServiceRegistry) bean;
return new ConsulServiceRegistryDecorator(consulServiceRegistry, 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.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
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 ConsulServiceRegistryDecorator extends ConsulServiceRegistry {
private ConsulServiceRegistry serviceRegistry;
private ConfigurableApplicationContext applicationContext;
private ConfigurableEnvironment environment;
public ConsulServiceRegistryDecorator(ConsulServiceRegistry serviceRegistry, ConfigurableApplicationContext applicationContext) {
super(null, null, null, null);
this.serviceRegistry = serviceRegistry;
this.applicationContext = applicationContext;
this.environment = applicationContext.getEnvironment();
}
@Override
public void register(ConsulRegistration registration) {
Boolean registerControlEnabled = PluginContextAware.isRegisterControlEnabled(environment);
if (registerControlEnabled) {
RegisterListenerExecutor registerListenerExecutor = applicationContext.getBean(RegisterListenerExecutor.class);
registerListenerExecutor.onRegister(registration);
}
serviceRegistry.register(registration);
}
@Override
public void deregister(ConsulRegistration registration) {
Boolean registerControlEnabled = PluginContextAware.isRegisterControlEnabled(environment);
if (registerControlEnabled) {
RegisterListenerExecutor registerListenerExecutor = applicationContext.getBean(RegisterListenerExecutor.class);
registerListenerExecutor.onDeregister(registration);
}
serviceRegistry.deregister(registration);
}
@Override
public void setStatus(ConsulRegistration 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(ConsulRegistration 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();
}
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-consul</artifactId>
<name>Nepxion Discovery Plugin Starter Consul</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-consul</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
org.springframework.context.ApplicationContextInitializer=\
com.nepxion.discovery.plugin.framework.context.ConsulApplicationContextInitializer
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.plugin.framework.configuration.PluginAutoConfiguration,\
com.nepxion.discovery.plugin.framework.configuration.ConsulAutoConfiguration,\
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
......@@ -40,7 +40,8 @@
<dependency>
<groupId>com.nepxion</groupId>
<!-- <artifactId>discovery-plugin-starter-eureka</artifactId> -->
<artifactId>discovery-plugin-starter-zookeeper</artifactId>
<artifactId>discovery-plugin-starter-consul</artifactId>
<!-- <artifactId>discovery-plugin-starter-zookeeper</artifactId> -->
<version>${discovery.plugin.version}</version>
</dependency>
</dependencies>
......
......@@ -7,6 +7,11 @@ eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/
eureka.instance.preferIpAddress=true
eureka.instance.metadataMap.version=1.0
# Consul config
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.tags.version=1.0
# Zookeeper config
spring.cloud.zookeeper.connectString=localhost:2181
spring.cloud.zookeeper.discovery.root=/spring-cloud-service
......
......@@ -40,7 +40,8 @@
<dependency>
<groupId>com.nepxion</groupId>
<!-- <artifactId>discovery-plugin-starter-eureka</artifactId> -->
<artifactId>discovery-plugin-starter-zookeeper</artifactId>
<artifactId>discovery-plugin-starter-consul</artifactId>
<!-- <artifactId>discovery-plugin-starter-zookeeper</artifactId> -->
<version>${discovery.plugin.version}</version>
</dependency>
</dependencies>
......
......@@ -4,6 +4,9 @@ server.port=1200
# Eureka config
eureka.instance.metadataMap.version=1.0
# Consul config
spring.cloud.consul.discovery.tags.version=1.0
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.0
......
......@@ -4,6 +4,9 @@ server.port=1201
# Eureka config
eureka.instance.metadataMap.version=1.1
# Consul config
spring.cloud.consul.discovery.tags.version=1.1
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.1
......
......@@ -5,8 +5,13 @@ spring.application.name=discovery-springcloud-example-b
eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/
eureka.instance.preferIpAddress=true
# Consul config
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
# Zookeeper config
spring.cloud.zookeeper.connectString=localhost:2181
spring.cloud.zookeeper.discovery.root=/spring-cloud-service
# Admin config
management.security.enabled=false
\ No newline at end of file
......@@ -45,7 +45,8 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<!-- <artifactId>spring-cloud-starter-eureka</artifactId> -->
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
<!-- <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> -->
</dependency>
</dependencies>
......
......@@ -4,5 +4,8 @@ server.port=1300
# Eureka config
eureka.instance.metadataMap.version=1.0
# Consul config
spring.cloud.consul.discovery.tags.version=1.0
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.0
\ No newline at end of file
......@@ -4,5 +4,8 @@ server.port=1301
# Eureka config
eureka.instance.metadataMap.version=1.1
# Consul config
spring.cloud.consul.discovery.tags.version=1.1
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.1
\ No newline at end of file
......@@ -4,5 +4,8 @@ server.port=1302
# Eureka config
eureka.instance.metadataMap.version=1.2
# Consul config
spring.cloud.consul.discovery.tags.version=1.2
# Zookeeper config
spring.cloud.zookeeper.discovery.metadata.version=1.2
\ No newline at end of file
......@@ -5,6 +5,10 @@ spring.application.name=discovery-springcloud-example-c
eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/
eureka.instance.preferIpAddress=true
# Consul config
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
# Zookeeper config
spring.cloud.zookeeper.connectString=localhost:2181
spring.cloud.zookeeper.discovery.root=/spring-cloud-service
\ No newline at end of file
......@@ -13,11 +13,13 @@
<modules>
<module>discovery-plugin-framework</module>
<module>discovery-plugin-framework-eureka</module>
<module>discovery-plugin-framework-consul</module>
<module>discovery-plugin-framework-zookeeper</module>
<module>discovery-plugin-config-center</module>
<module>discovery-plugin-admin-center</module>
<module>discovery-plugin-router-center</module>
<module>discovery-plugin-starter-eureka</module>
<module>discovery-plugin-starter-consul</module>
<module>discovery-plugin-starter-zookeeper</module>
<module>discovery-springcloud-example-eureka</module>
<module>discovery-springcloud-example-a</module>
......@@ -32,6 +34,7 @@
<commons.io.version>2.5</commons.io.version>
<dom4j.version>1.6.1</dom4j.version>
<spring.cloud.version>Dalston.SR5</spring.cloud.version>
<spring.cloud.consul.version>2.0.0.RELEASE</spring.cloud.consul.version>
<spring.cloud.zookeeper.version>2.0.0.RELEASE</spring.cloud.zookeeper.version>
<!-- <spring.cloud.version>Edgware.SR3</spring.cloud.version> -->
<spring.boot.version>1.5.8.RELEASE</spring.boot.version>
......@@ -57,6 +60,12 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework-consul</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework-zookeeper</artifactId>
<version>${project.version}</version>
</dependency>
......@@ -87,6 +96,12 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-starter-consul</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-starter-zookeeper</artifactId>
<version>${project.version}</version>
</dependency>
......@@ -131,6 +146,14 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-consul-dependencies</artifactId>
<version>${spring.cloud.consul.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-dependencies</artifactId>
<version>${spring.cloud.zookeeper.version}</version>
<type>pom</type>
......
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