Commit 71127034 by Nepxion

重构Nacos,独立出Common模块,便于以后接入Nacos的服务注册发现机制

parent 8fdc6fb9
<?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-common-nacos</artifactId>
<name>Nepxion Discovery Common Nacos</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>4.1.6</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.common.nacos.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 java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.nepxion.discovery.common.nacos.constant.NacosConstant;
import com.nepxion.discovery.common.nacos.operation.NacosOperation;
@Configuration
public class NacosAutoConfiguration {
@Autowired
private Environment environment;
@Bean
public ConfigService configService() throws NacosException {
String url = environment.getProperty(NacosConstant.URL);
Properties properties = new Properties();
properties.put(NacosConstant.URL_KEY, url);
return NacosFactory.createConfigService(properties);
}
@Bean
public NacosOperation nacosOperation() {
return new NacosOperation();
}
}
\ No newline at end of file
package com.nepxion.discovery.common.nacos.operation;
/**
* <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.concurrent.Executor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.nepxion.discovery.common.nacos.constant.NacosConstant;
public class NacosOperation {
@Autowired
private ConfigService configService;
@Autowired
private Environment environment;
public String getConfig(String group, String serviceId) throws NacosException {
long timeout = environment.getProperty(NacosConstant.TIMEOUT, Long.class, NacosConstant.DEFAULT_TIMEOUT);
return configService.getConfig(serviceId, group, timeout);
}
public boolean removeConfig(String group, String serviceId) throws NacosException {
return configService.removeConfig(serviceId, group);
}
public boolean publishConfig(String group, String serviceId, String config) throws NacosException {
return configService.publishConfig(serviceId, group, config);
}
public void subscribeConfig(String group, String serviceId, NacosSubscribeCallback subscribeCallback) throws NacosException {
configService.addListener(serviceId, group, new Listener() {
@Override
public void receiveConfigInfo(String config) {
subscribeCallback.callback(config);
}
@Override
public Executor getExecutor() {
return null;
}
});
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.configcenter.extension.nacos.constant;
package com.nepxion.discovery.common.nacos.operation;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -9,10 +9,6 @@ package com.nepxion.discovery.plugin.configcenter.extension.nacos.constant;
* @version 1.0
*/
public class NacosConstant {
public static final String URL_KEY = "serverAddr";
public static final String URL = "nacos.url";
public static final String TIMEOUT = "nacos.timout";
public static final long DEFAULT_TIMEOUT = 30000;
public interface NacosSubscribeCallback {
void callback(String config);
}
\ No newline at end of file
......@@ -21,8 +21,8 @@
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-common-nacos</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -10,33 +10,26 @@ package com.nepxion.discovery.console.extension.nacos.adapter;
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import com.alibaba.nacos.api.config.ConfigService;
import com.nepxion.discovery.console.extension.nacos.constant.NacosConstant;
import com.nepxion.discovery.common.nacos.operation.NacosOperation;
import com.nepxion.discovery.console.remote.ConfigAdapter;
public class NacosConfigAdapter implements ConfigAdapter {
@Autowired
private ConfigService configService;
@Autowired
private Environment environment;
private NacosOperation nacosOperation;
@Override
public boolean updateConfig(String group, String serviceId, String config) throws Exception {
return configService.publishConfig(serviceId, group, config);
return nacosOperation.publishConfig(group, serviceId, config);
}
@Override
public boolean clearConfig(String group, String serviceId) throws Exception {
return configService.removeConfig(serviceId, group);
return nacosOperation.removeConfig(group, serviceId);
}
@Override
public String getConfig(String group, String serviceId) throws Exception {
long timeout = environment.getProperty(NacosConstant.TIMEOUT, Long.class, NacosConstant.DEFAULT_TIMEOUT);
return configService.getConfig(serviceId, group, timeout);
return nacosOperation.getConfig(group, serviceId);
}
}
\ No newline at end of file
......@@ -9,37 +9,16 @@ package com.nepxion.discovery.console.extension.nacos.configuration;
* @version 1.0
*/
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.nepxion.discovery.console.extension.nacos.adapter.NacosConfigAdapter;
import com.nepxion.discovery.console.extension.nacos.constant.NacosConstant;
import com.nepxion.discovery.console.remote.ConfigAdapter;
@Configuration
public class NacosConfigAutoConfiguration {
@Autowired
private Environment environment;
@Bean
public ConfigService configService() throws NacosException {
String url = environment.getProperty(NacosConstant.URL);
Properties properties = new Properties();
properties.put(NacosConstant.URL_KEY, url);
return NacosFactory.createConfigService(properties);
}
@Bean
public ConfigAdapter configAdapter() throws NacosException {
public ConfigAdapter configAdapter() {
return new NacosConfigAdapter();
}
}
\ No newline at end of file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.common.nacos.configuration.NacosAutoConfiguration,\
com.nepxion.discovery.console.extension.nacos.configuration.NacosConfigAutoConfiguration
\ No newline at end of file
......@@ -21,8 +21,8 @@
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-common-nacos</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.configcenter.extension.nacos.adapter;
* @version 1.0
*/
import java.util.concurrent.Executor;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
......@@ -18,11 +16,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.nepxion.discovery.common.nacos.operation.NacosOperation;
import com.nepxion.discovery.common.nacos.operation.NacosSubscribeCallback;
import com.nepxion.discovery.plugin.configcenter.ConfigAdapter;
import com.nepxion.discovery.plugin.configcenter.extension.nacos.constant.NacosConstant;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
......@@ -33,7 +29,7 @@ public class NacosConfigAdapter extends ConfigAdapter {
private static final Logger LOG = LoggerFactory.getLogger(NacosConfigAdapter.class);
@Autowired
private ConfigService configService;
private NacosOperation nacosOperation;
@Autowired
protected PluginContextAware pluginContextAware;
......@@ -47,11 +43,9 @@ public class NacosConfigAdapter extends ConfigAdapter {
String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId();
long timeout = pluginContextAware.getEnvironment().getProperty(NacosConstant.TIMEOUT, Long.class, NacosConstant.DEFAULT_TIMEOUT);
LOG.info("Get remote config from Nacos server, {}={}, serviceId={}, timeout={}", groupKey, group, serviceId, timeout);
LOG.info("Get config from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
return configService.getConfig(serviceId, group, timeout);
return nacosOperation.getConfig(group, serviceId);
}
@PostConstruct
......@@ -60,12 +54,12 @@ public class NacosConfigAdapter extends ConfigAdapter {
String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId();
LOG.info("Subscribe remote config from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
LOG.info("Subscribe config from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
try {
configService.addListener(serviceId, group, new Listener() {
nacosOperation.subscribeConfig(group, serviceId, new NacosSubscribeCallback() {
@Override
public void receiveConfigInfo(String config) {
public void callback(String config) {
if (StringUtils.isNotEmpty(config)) {
LOG.info("Get config updated event from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
......@@ -82,13 +76,8 @@ public class NacosConfigAdapter extends ConfigAdapter {
fireRuleCleared(new RuleClearedEvent(), true);
}
}
@Override
public Executor getExecutor() {
return null;
}
});
} catch (NacosException e) {
} catch (Exception e) {
LOG.error("Subscribe config failed", e);
}
}
......
......@@ -9,37 +9,16 @@ package com.nepxion.discovery.plugin.configcenter.extension.nacos.configuration;
* @version 1.0
*/
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.nepxion.discovery.plugin.configcenter.ConfigAdapter;
import com.nepxion.discovery.plugin.configcenter.extension.nacos.adapter.NacosConfigAdapter;
import com.nepxion.discovery.plugin.configcenter.extension.nacos.constant.NacosConstant;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
@Configuration
public class NacosConfigAutoConfiguration {
@Autowired
private PluginContextAware pluginContextAware;
@Bean
public ConfigService configService() throws NacosException {
String url = pluginContextAware.getEnvironment().getProperty(NacosConstant.URL);
Properties properties = new Properties();
properties.put(NacosConstant.URL_KEY, url);
return NacosFactory.createConfigService(properties);
}
@Bean
public ConfigAdapter configAdapter() throws NacosException {
public ConfigAdapter configAdapter() {
return new NacosConfigAdapter();
}
}
\ No newline at end of file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.common.nacos.configuration.NacosAutoConfiguration,\
com.nepxion.discovery.plugin.configcenter.extension.nacos.configuration.NacosConfigAutoConfiguration
\ No newline at end of file
......@@ -11,6 +11,7 @@
<url>http://www.nepxion.com</url>
<modules>
<module>discovery-common-nacos</module>
<module>discovery-plugin-framework</module>
<module>discovery-plugin-framework-eureka</module>
<module>discovery-plugin-framework-consul</module>
......@@ -51,6 +52,12 @@
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-common-nacos</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework</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