Commit 3384f43b by Nepxion

整合Nacos远程配置中心

parent 616eb23b
<?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-console-extension-nacos</artifactId>
<name>Nepxion Discovery Console Extension 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.0.9</version>
</parent>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-console</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.console.extension.nacos.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 com.alibaba.nacos.api.config.ConfigService;
import com.nepxion.discovery.console.remote.ConfigAdapter;
public class NacosConfigAdapter implements ConfigAdapter {
@Autowired
private ConfigService configService;
@Override
public boolean configUpdate(String group, String serviceId, String config) throws Exception {
return configService.publishConfig(serviceId, group, config);
}
@Override
public boolean configClear(String group, String serviceId) throws Exception {
return configService.removeConfig(serviceId, group);
}
}
\ No newline at end of file
package com.nepxion.discovery.console.extension.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.console.extension.nacos.adapter.NacosConfigAdapter;
import com.nepxion.discovery.console.extension.nacos.constant.NacosConstant;
import com.nepxion.discovery.console.remote.ConfigAdapter;
@Configuration
public class NacosConfigConfiguration {
@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 {
return new NacosConfigAdapter();
}
}
\ No newline at end of file
package com.nepxion.discovery.console.extension.nacos.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 NacosConstant {
public static final String URL_KEY = "serverAddr";
public static final String URL = "nacos.url";
public static final String TIMEOUT = "nacos.timout";
}
\ No newline at end of file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.console.extension.nacos.configuration.NacosConfigConfiguration
\ No newline at end of file
...@@ -18,9 +18,12 @@ import java.util.LinkedHashMap; ...@@ -18,9 +18,12 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
...@@ -30,6 +33,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -30,6 +33,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.console.entity.InstanceEntity; import com.nepxion.discovery.console.entity.InstanceEntity;
import com.nepxion.discovery.console.remote.ConfigAdapter;
import com.nepxion.discovery.console.rest.ConfigClearRestInvoker; import com.nepxion.discovery.console.rest.ConfigClearRestInvoker;
import com.nepxion.discovery.console.rest.ConfigUpdateRestInvoker; import com.nepxion.discovery.console.rest.ConfigUpdateRestInvoker;
import com.nepxion.discovery.console.rest.VersionClearRestInvoker; import com.nepxion.discovery.console.rest.VersionClearRestInvoker;
...@@ -38,9 +42,14 @@ import com.nepxion.discovery.console.rest.VersionUpdateRestInvoker; ...@@ -38,9 +42,14 @@ import com.nepxion.discovery.console.rest.VersionUpdateRestInvoker;
@RestController @RestController
@Api(tags = { "控制台接口" }) @Api(tags = { "控制台接口" })
public class ConsoleEndpoint { public class ConsoleEndpoint {
private static final Logger LOG = LoggerFactory.getLogger(ConsoleEndpoint.class);
@Autowired @Autowired
private DiscoveryClient discoveryClient; private DiscoveryClient discoveryClient;
@Autowired(required = false)
private ConfigAdapter configAdapter;
@Autowired @Autowired
private RestTemplate consoleRestTemplate; private RestTemplate consoleRestTemplate;
...@@ -68,6 +77,18 @@ public class ConsoleEndpoint { ...@@ -68,6 +77,18 @@ public class ConsoleEndpoint {
return getInstanceMap(); return getInstanceMap();
} }
@RequestMapping(path = "/console/remote-config/update/{group}/{serviceId}", method = RequestMethod.POST)
@ApiOperation(value = "推送更新规则配置信息到远程配置中心", notes = "", response = ResponseEntity.class, httpMethod = "POST")
public ResponseEntity<?> remoteConfigUpdate(@PathVariable(value = "group") @ApiParam(value = "组名", required = true) String group, @PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId, @RequestBody @ApiParam(value = "规则配置内容,XML格式", required = true) String config) {
return executeRemoteConfigUpdate(group, serviceId, config);
}
@RequestMapping(path = "/console/remote-config/clear/{group}/{serviceId}", method = RequestMethod.POST)
@ApiOperation(value = "清除规则配置信息到远程配置中心", notes = "", response = ResponseEntity.class, httpMethod = "POST")
public ResponseEntity<?> remoteConfigClear(@PathVariable(value = "group") @ApiParam(value = "组名", required = true) String group, @PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId) {
return executeRemoteClearUpdate(group, serviceId);
}
@RequestMapping(path = "/console/config/update-async/{serviceId}", method = RequestMethod.POST) @RequestMapping(path = "/console/config/update-async/{serviceId}", method = RequestMethod.POST)
@ApiOperation(value = "批量异步推送更新规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "POST") @ApiOperation(value = "批量异步推送更新规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "POST")
public ResponseEntity<?> configUpdateAsync(@PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId, @RequestBody @ApiParam(value = "规则配置内容,XML格式", required = true) String config) { public ResponseEntity<?> configUpdateAsync(@PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId, @RequestBody @ApiParam(value = "规则配置内容,XML格式", required = true) String config) {
...@@ -140,6 +161,38 @@ public class ConsoleEndpoint { ...@@ -140,6 +161,38 @@ public class ConsoleEndpoint {
return instanceMap; return instanceMap;
} }
private ResponseEntity<?> executeRemoteConfigUpdate(String group, String serviceId, String config) {
if (configAdapter == null) {
LOG.error("Remote config adapter isn't provided");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Remote config adapter isn't provided");
}
try {
boolean result = configAdapter.configUpdate(group, serviceId, config);
return ResponseEntity.ok().body(result ? "OK" : "NO");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
private ResponseEntity<?> executeRemoteClearUpdate(String group, String serviceId) {
if (configAdapter == null) {
LOG.error("Remote config adapter isn't provided");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Remote config adapter isn't provided");
}
try {
boolean result = configAdapter.configClear(group, serviceId);
return ResponseEntity.ok().body(result ? "OK" : "NO");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
private ResponseEntity<?> executeConfigUpdate(String serviceId, String config, boolean async) { private ResponseEntity<?> executeConfigUpdate(String serviceId, String config, boolean async) {
List<ServiceInstance> serviceInstances = getInstances(serviceId); List<ServiceInstance> serviceInstances = getInstances(serviceId);
......
package com.nepxion.discovery.console.remote;
/**
* <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 interface ConfigAdapter {
boolean configUpdate(String group, String serviceId, String config) throws Exception;
boolean configClear(String group, String serviceId) throws Exception;
}
\ 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-config-center-extension-nacos</artifactId>
<name>Nepxion Discovery Plugin Config Center Extension 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.0.9</version>
</parent>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-config-center</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.plugin.configcenter.extension.nacos.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 java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor;
import javax.annotation.PostConstruct;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
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.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.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.RuleClearedEvent;
import com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent;
public class NacosConfigAdapter extends ConfigAdapter {
private static final Logger LOG = LoggerFactory.getLogger(NacosConfigAdapter.class);
@Autowired
private ConfigService configService;
@Autowired
protected PluginContextAware pluginContextAware;
@Autowired
private PluginAdapter pluginAdapter;
@Override
public InputStream getInputStream() throws Exception {
String groupKey = pluginContextAware.getGroupKey();
String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId();
long timeout = pluginContextAware.getEnvironment().getProperty(NacosConstant.TIMEOUT, Long.class);
LOG.info("Get remote config from Nacos server, {}={}, serviceId={}, timeout={}", groupKey, group, serviceId, timeout);
String config = configService.getConfig(serviceId, group, timeout);
if (StringUtils.isEmpty(config)) {
return null;
}
return toInputStream(config);
}
@PostConstruct
public void subscribe() throws Exception {
String groupKey = pluginContextAware.getGroupKey();
String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId();
LOG.info("Subscribe remote config from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
configService.addListener(serviceId, group, new Listener() {
@Override
public void receiveConfigInfo(String config) {
try {
if (StringUtils.isNotEmpty(config)) {
LOG.info("Get config updated event from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
RuleEntity ruleEntity = pluginAdapter.getRule();
String rule = ruleEntity.getContent();
if (!StringUtils.equals(rule, config)) {
InputStream inputStream = toInputStream(config);
fireRuleUpdated(new RuleUpdatedEvent(inputStream), true);
} else {
LOG.info("Retrieved config is same as current config, ignore to update, {}={}, serviceId={}", groupKey, group, serviceId);
}
} else {
LOG.info("Get config cleared event from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
fireRuleCleared(new RuleClearedEvent(), true);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Executor getExecutor() {
return null;
}
});
}
private InputStream toInputStream(String config) throws IOException {
return IOUtils.toInputStream(config, PluginConstant.ENCODING_UTF_8);
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.configcenter.extension.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 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 NacosConfigConfiguration {
@Autowired
protected 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 {
return new NacosConfigAdapter();
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.configcenter.extension.nacos.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 NacosConstant {
public static final String URL_KEY = "serverAddr";
public static final String URL = "nacos.url";
public static final String TIMEOUT = "nacos.timout";
}
\ No newline at end of file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.plugin.configcenter.extension.nacos.configuration.NacosConfigConfiguration
\ No newline at end of file
...@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.configcenter.loader.RemoteConfigLoader; import com.nepxion.discovery.plugin.configcenter.loader.RemoteConfigLoader;
import com.nepxion.discovery.plugin.framework.event.PluginEventWapper; import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.RuleClearedEvent;
import com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent; import com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent;
public abstract class ConfigAdapter extends RemoteConfigLoader { public abstract class ConfigAdapter extends RemoteConfigLoader {
...@@ -22,4 +23,8 @@ public abstract class ConfigAdapter extends RemoteConfigLoader { ...@@ -22,4 +23,8 @@ public abstract class ConfigAdapter extends RemoteConfigLoader {
public void fireRuleUpdated(RuleUpdatedEvent ruleUpdatedEvent, boolean async) { public void fireRuleUpdated(RuleUpdatedEvent ruleUpdatedEvent, boolean async) {
pluginEventWapper.fireRuleUpdated(ruleUpdatedEvent, async); pluginEventWapper.fireRuleUpdated(ruleUpdatedEvent, async);
} }
public void fireRuleCleared(RuleClearedEvent ruleClearedEvent, boolean async) {
pluginEventWapper.fireRuleCleared(ruleClearedEvent, async);
}
} }
\ No newline at end of file
...@@ -47,12 +47,12 @@ public class ConfigInitializer { ...@@ -47,12 +47,12 @@ public class ConfigInitializer {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!registerControlEnabled && !discoveryControlEnabled) { if (!registerControlEnabled && !discoveryControlEnabled) {
LOG.info("********** Register and Discovery controls are all disabled, ignore to initialize **********"); LOG.info("Register and Discovery controls are all disabled, ignore to initialize");
return; return;
} }
LOG.info("********** Rule starts to initialize **********"); LOG.info("Rule starts to initialize...");
InputStream inputStream = getInputStream(); InputStream inputStream = getInputStream();
try { try {
...@@ -74,14 +74,14 @@ public class ConfigInitializer { ...@@ -74,14 +74,14 @@ public class ConfigInitializer {
} }
if (inputStream != null) { if (inputStream != null) {
LOG.info("********** Remote input stream is retrieved **********"); LOG.info("Remote input stream is retrieved");
return inputStream; return inputStream;
} else { } else {
LOG.info("********** Remote input stream isn't retrieved, use local config loader **********"); LOG.info("Remote input stream isn't retrieved, use local config loader");
} }
} else { } else {
LOG.info("********** Remote config loader isn't provided, use local config loader **********"); LOG.info("Remote config loader isn't provided, use local config loader");
} }
try { try {
...@@ -91,14 +91,14 @@ public class ConfigInitializer { ...@@ -91,14 +91,14 @@ public class ConfigInitializer {
} }
if (inputStream != null) { if (inputStream != null) {
LOG.info("********** Local input stream is retrieved **********"); LOG.info("Local input stream is retrieved");
return inputStream; return inputStream;
} else { } else {
LOG.info("********** Local input stream isn't retrieved **********"); LOG.info("Local input stream isn't retrieved");
} }
LOG.info("********** No input stream is retrieved, use no config settings **********"); LOG.info("No input stream is retrieved, use no config settings");
return null; return null;
} }
......
...@@ -39,6 +39,7 @@ public class ConsulApplicationContextInitializer extends PluginApplicationContex ...@@ -39,6 +39,7 @@ public class ConsulApplicationContextInitializer extends PluginApplicationContex
tags.add(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED + "=" + PluginContextAware.isRegisterControlEnabled(environment)); tags.add(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED + "=" + PluginContextAware.isRegisterControlEnabled(environment));
tags.add(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED + "=" + PluginContextAware.isDiscoveryControlEnabled(environment)); tags.add(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED + "=" + PluginContextAware.isDiscoveryControlEnabled(environment));
tags.add(PluginConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED + "=" + PluginContextAware.isConfigRestControlEnabled(environment)); tags.add(PluginConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED + "=" + PluginContextAware.isConfigRestControlEnabled(environment));
tags.add(PluginConstant.SPRING_APPLICATION_GROUP_KEY + "=" + PluginContextAware.getGroupKey(environment));
return bean; return bean;
} else { } else {
......
...@@ -39,6 +39,7 @@ public class EurekaApplicationContextInitializer extends PluginApplicationContex ...@@ -39,6 +39,7 @@ public class EurekaApplicationContextInitializer extends PluginApplicationContex
metadataMap.put(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, PluginContextAware.isRegisterControlEnabled(environment).toString()); metadataMap.put(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, PluginContextAware.isRegisterControlEnabled(environment).toString());
metadataMap.put(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, PluginContextAware.isDiscoveryControlEnabled(environment).toString()); metadataMap.put(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, PluginContextAware.isDiscoveryControlEnabled(environment).toString());
metadataMap.put(PluginConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED, PluginContextAware.isConfigRestControlEnabled(environment).toString()); metadataMap.put(PluginConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED, PluginContextAware.isConfigRestControlEnabled(environment).toString());
metadataMap.put(PluginConstant.SPRING_APPLICATION_GROUP_KEY, PluginContextAware.getGroupKey(environment));
return bean; return bean;
} else { } else {
......
...@@ -39,6 +39,7 @@ public class ZookeeperApplicationContextInitializer extends PluginApplicationCon ...@@ -39,6 +39,7 @@ public class ZookeeperApplicationContextInitializer extends PluginApplicationCon
metadata.put(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, PluginContextAware.isRegisterControlEnabled(environment).toString()); metadata.put(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, PluginContextAware.isRegisterControlEnabled(environment).toString());
metadata.put(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, PluginContextAware.isDiscoveryControlEnabled(environment).toString()); metadata.put(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, PluginContextAware.isDiscoveryControlEnabled(environment).toString());
metadata.put(PluginConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED, PluginContextAware.isConfigRestControlEnabled(environment).toString()); metadata.put(PluginConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED, PluginContextAware.isConfigRestControlEnabled(environment).toString());
metadata.put(PluginConstant.SPRING_APPLICATION_GROUP_KEY, PluginContextAware.getGroupKey(environment));
return bean; return bean;
} else { } else {
......
...@@ -20,6 +20,7 @@ import com.nepxion.discovery.plugin.framework.cache.RuleCache; ...@@ -20,6 +20,7 @@ import com.nepxion.discovery.plugin.framework.cache.RuleCache;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
public abstract class AbstractPluginAdapter implements PluginAdapter { public abstract class AbstractPluginAdapter implements PluginAdapter {
...@@ -36,6 +37,18 @@ public abstract class AbstractPluginAdapter implements PluginAdapter { ...@@ -36,6 +37,18 @@ public abstract class AbstractPluginAdapter implements PluginAdapter {
protected RuleCache ruleCache; protected RuleCache ruleCache;
@Override @Override
public String getGroup() {
String groupKey = pluginContextAware.getGroupKey();
String group = getMetaData().get(groupKey);
if (StringUtils.isEmpty(group)) {
throw new PluginException("The value is empty for metadata key=" + groupKey + ", please check your configuration");
}
return group;
}
@Override
public String getServiceId() { public String getServiceId() {
return registration.getServiceId(); return registration.getServiceId();
} }
......
...@@ -15,6 +15,8 @@ import com.nepxion.discovery.plugin.framework.entity.RuleEntity; ...@@ -15,6 +15,8 @@ import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
public interface PluginAdapter { public interface PluginAdapter {
String getGroup();
String getServiceId(); String getServiceId();
String getHost(); String getHost();
......
...@@ -15,8 +15,10 @@ public class PluginConstant { ...@@ -15,8 +15,10 @@ public class PluginConstant {
public static final String SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED = "spring.application.discovery.control.enabled"; public static final String SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED = "spring.application.discovery.control.enabled";
public static final String SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED = "spring.application.config.rest.control.enabled"; public static final String SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED = "spring.application.config.rest.control.enabled";
public static final String SPRING_APPLICATION_CONFIG_PATH = "spring.application.config.path"; public static final String SPRING_APPLICATION_CONFIG_PATH = "spring.application.config.path";
public static final String SPRING_APPLICATION_GROUP_KEY = "spring.application.group.key";
public static final String SPRING_APPLICATION_NAME = "spring.application.name"; public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final String GROUP = "group";
public static final String SERVICE_ID = "serviceId"; public static final String SERVICE_ID = "serviceId";
public static final String HOST = "host"; public static final String HOST = "host";
public static final String PORT = "port"; public static final String PORT = "port";
......
...@@ -100,6 +100,10 @@ public class PluginContextAware implements ApplicationContextAware { ...@@ -100,6 +100,10 @@ public class PluginContextAware implements ApplicationContextAware {
return getConfigPath(environment); return getConfigPath(environment);
} }
public String getGroupKey() {
return getGroupKey(environment);
}
public static Boolean isRegisterControlEnabled(Environment environment) { public static Boolean isRegisterControlEnabled(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, Boolean.class, Boolean.TRUE); return environment.getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, Boolean.class, Boolean.TRUE);
} }
...@@ -115,4 +119,8 @@ public class PluginContextAware implements ApplicationContextAware { ...@@ -115,4 +119,8 @@ public class PluginContextAware implements ApplicationContextAware {
public static String getConfigPath(Environment environment) { public static String getConfigPath(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_CONFIG_PATH); return environment.getProperty(PluginConstant.SPRING_APPLICATION_CONFIG_PATH);
} }
public static String getGroupKey(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_GROUP_KEY, String.class, PluginConstant.GROUP);
}
} }
\ No newline at end of file
...@@ -46,12 +46,12 @@ public class PluginSubscriber { ...@@ -46,12 +46,12 @@ public class PluginSubscriber {
public void onRuleUpdated(RuleUpdatedEvent ruleUpdatedEvent) { public void onRuleUpdated(RuleUpdatedEvent ruleUpdatedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********"); LOG.info("Discovery control is disabled, ignore to subscribe");
return; return;
} }
LOG.info("********** Rule updating has been triggered **********"); LOG.info("Rule updating has been triggered");
if (ruleUpdatedEvent == null) { if (ruleUpdatedEvent == null) {
throw new PluginException("RuleUpdatedEvent can't be null"); throw new PluginException("RuleUpdatedEvent can't be null");
...@@ -74,12 +74,12 @@ public class PluginSubscriber { ...@@ -74,12 +74,12 @@ public class PluginSubscriber {
public void onRuleCleared(RuleClearedEvent ruleClearedEvent) { public void onRuleCleared(RuleClearedEvent ruleClearedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********"); LOG.info("Discovery control is disabled, ignore to subscribe");
return; return;
} }
LOG.info("********** Rule clearing has been triggered **********"); LOG.info("Rule clearing has been triggered");
if (ruleClearedEvent == null) { if (ruleClearedEvent == null) {
throw new PluginException("RuleClearedEvent can't be null"); throw new PluginException("RuleClearedEvent can't be null");
...@@ -94,12 +94,12 @@ public class PluginSubscriber { ...@@ -94,12 +94,12 @@ public class PluginSubscriber {
public void onVersionUpdated(VersionUpdatedEvent versionUpdatedEvent) { public void onVersionUpdated(VersionUpdatedEvent versionUpdatedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********"); LOG.info("Discovery control is disabled, ignore to subscribe");
return; return;
} }
LOG.info("********** Version updating has been triggered **********"); LOG.info("Version updating has been triggered");
if (versionUpdatedEvent == null) { if (versionUpdatedEvent == null) {
throw new PluginException("VersionUpdatedEvent can't be null"); throw new PluginException("VersionUpdatedEvent can't be null");
...@@ -113,14 +113,14 @@ public class PluginSubscriber { ...@@ -113,14 +113,14 @@ public class PluginSubscriber {
refreshLoadBalancer(); refreshLoadBalancer();
LOG.info("********** Version has been updated, new version is {} **********", dynamicVersion); LOG.info("Version has been updated, new version is {}", dynamicVersion);
} else { } else {
if (StringUtils.equals(pluginAdapter.getLocalVersion(), localVersion)) { if (StringUtils.equals(pluginAdapter.getLocalVersion(), localVersion)) {
pluginAdapter.setDynamicVersion(dynamicVersion); pluginAdapter.setDynamicVersion(dynamicVersion);
refreshLoadBalancer(); refreshLoadBalancer();
LOG.info("********** Version has been updated, new version is {} **********", dynamicVersion); LOG.info("Version has been updated, new version is {}", dynamicVersion);
} else { } else {
throw new PluginException("Version updating will be ignored, because input localVersion=" + localVersion + ", current localVersion=" + pluginAdapter.getLocalVersion()); throw new PluginException("Version updating will be ignored, because input localVersion=" + localVersion + ", current localVersion=" + pluginAdapter.getLocalVersion());
} }
...@@ -131,12 +131,12 @@ public class PluginSubscriber { ...@@ -131,12 +131,12 @@ public class PluginSubscriber {
public void onVersionCleared(VersionClearedEvent versionClearedEvent) { public void onVersionCleared(VersionClearedEvent versionClearedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********"); LOG.info("Discovery control is disabled, ignore to subscribe");
return; return;
} }
LOG.info("********** Version clearing has been triggered **********"); LOG.info("Version clearing has been triggered");
if (versionClearedEvent == null) { if (versionClearedEvent == null) {
throw new PluginException("VersionClearedEvent can't be null"); throw new PluginException("VersionClearedEvent can't be null");
...@@ -149,14 +149,14 @@ public class PluginSubscriber { ...@@ -149,14 +149,14 @@ public class PluginSubscriber {
refreshLoadBalancer(); refreshLoadBalancer();
LOG.info("********** Version has been cleared **********"); LOG.info("Version has been cleared");
} else { } else {
if (StringUtils.equals(pluginAdapter.getLocalVersion(), localVersion)) { if (StringUtils.equals(pluginAdapter.getLocalVersion(), localVersion)) {
pluginAdapter.clearDynamicVersion(); pluginAdapter.clearDynamicVersion();
refreshLoadBalancer(); refreshLoadBalancer();
LOG.info("********** Version has been cleared **********"); LOG.info("Version has been cleared");
} else { } else {
throw new PluginException("Version clearing will be ignored, because input localVersion=" + localVersion + ", current localVersion=" + pluginAdapter.getLocalVersion()); throw new PluginException("Version clearing will be ignored, because input localVersion=" + localVersion + ", current localVersion=" + pluginAdapter.getLocalVersion());
} }
......
...@@ -43,6 +43,12 @@ ...@@ -43,6 +43,12 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-console-extension-nacos</artifactId>
<version>${discovery.plugin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> </dependency>
......
...@@ -25,6 +25,10 @@ spring.cloud.zookeeper.discovery.metadata.group=example-console-group ...@@ -25,6 +25,10 @@ spring.cloud.zookeeper.discovery.metadata.group=example-console-group
ribbon.ReadTimeout=60000 ribbon.ReadTimeout=60000
ribbon.ConnectTimeout=60000 ribbon.ConnectTimeout=60000
# Nacos config
nacos.url=localhost:8080
nacos.timout=30000
# Admin config # Admin config
management.port=3333 management.port=3333
management.security.enabled=false management.security.enabled=false
......
...@@ -45,6 +45,12 @@ ...@@ -45,6 +45,12 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-config-center-extension-nacos</artifactId>
<version>${discovery.plugin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> </dependency>
......
...@@ -30,7 +30,7 @@ public class MyConfigAdapter extends ConfigAdapter { ...@@ -30,7 +30,7 @@ public class MyConfigAdapter extends ConfigAdapter {
// 订阅远程配置中心的规则更新(推送策略自己决定,可以所有服务都只对应一个规则信息,也可以根据服务名获取对应的规则信息) // 订阅远程配置中心的规则更新(推送策略自己决定,可以所有服务都只对应一个规则信息,也可以根据服务名获取对应的规则信息)
@PostConstruct @PostConstruct
public void update() throws Exception { public void subscribe() throws Exception {
InputStream inputStream = FileUtils.openInputStream(new File("src/main/resources/rule.xml")); InputStream inputStream = FileUtils.openInputStream(new File("src/main/resources/rule.xml"));
fireRuleUpdated(new RuleUpdatedEvent(inputStream), true); fireRuleUpdated(new RuleUpdatedEvent(inputStream), true);
} }
......
...@@ -18,6 +18,10 @@ spring.cloud.zookeeper.discovery.preferIpAddress=true ...@@ -18,6 +18,10 @@ spring.cloud.zookeeper.discovery.preferIpAddress=true
ribbon.ReadTimeout=60000 ribbon.ReadTimeout=60000
ribbon.ConnectTimeout=60000 ribbon.ConnectTimeout=60000
# Nacos config
nacos.url=localhost:8080
nacos.timout=30000
# Admin config(management.health.consul.enabled必须配置在bootstrap.properties里,配置在application.properties无效) # Admin config(management.health.consul.enabled必须配置在bootstrap.properties里,配置在application.properties无效)
management.security.enabled=false management.security.enabled=false
# management.health.consul.enabled=true # management.health.consul.enabled=true
...@@ -29,11 +33,11 @@ spring.application.register.control.enabled=true ...@@ -29,11 +33,11 @@ spring.application.register.control.enabled=true
spring.application.discovery.control.enabled=true spring.application.discovery.control.enabled=true
# 开启和关闭通过Rest方式对规则配置的控制和推送。一旦关闭,只能通过远程配置中心来控制和推送。缺失则默认为true # 开启和关闭通过Rest方式对规则配置的控制和推送。一旦关闭,只能通过远程配置中心来控制和推送。缺失则默认为true
spring.application.config.rest.control.enabled=true spring.application.config.rest.control.enabled=true
# 本地规则文件的路径。缺失则默认为不装载本地规则 # 本地规则文件的路径,支持两种方式:classpath:rule.xml - 规则文件放在resources目录下,便于打包进jar;file:rule.xml - 规则文件放在工程根目录下,放置在外部便于修改。缺失则默认为不装载本地规则
# 两种方式放置方式:
# classpath:rule.xml,规则文件放在resources目录下,便于把规则文件打包进jar
# file:rule.xml,规则文件放在工程根目录下,便于把规则文件放置在外部,方便修改
spring.application.config.path=classpath:rule.xml spring.application.config.path=classpath:rule.xml
# 为微服务归类的Key,一般通过group字段来归类,例如eureka.instance.metadataMap.group=xxx-group或者eureka.instance.metadataMap.application=xxx-application。缺失则默认为group
# spring.application.group.key=group
# spring.application.group.key=application
# Swagger config # Swagger config
swagger.service.base.package=com.nepxion.discovery.plugin.admincenter.endpoint swagger.service.base.package=com.nepxion.discovery.plugin.admincenter.endpoint
......
...@@ -16,13 +16,15 @@ ...@@ -16,13 +16,15 @@
<module>discovery-plugin-framework-consul</module> <module>discovery-plugin-framework-consul</module>
<module>discovery-plugin-framework-zookeeper</module> <module>discovery-plugin-framework-zookeeper</module>
<module>discovery-plugin-config-center</module> <module>discovery-plugin-config-center</module>
<module>discovery-plugin-config-center-extension-nacos</module>
<module>discovery-plugin-admin-center</module> <module>discovery-plugin-admin-center</module>
<module>discovery-console</module>
<module>discovery-console-desktop</module>
<module>discovery-plugin-starter-eureka</module> <module>discovery-plugin-starter-eureka</module>
<module>discovery-plugin-starter-consul</module> <module>discovery-plugin-starter-consul</module>
<module>discovery-plugin-starter-zookeeper</module> <module>discovery-plugin-starter-zookeeper</module>
<module>discovery-console</module>
<module>discovery-console-extension-nacos</module>
<module>discovery-console-starter</module> <module>discovery-console-starter</module>
<module>discovery-console-desktop</module>
<module>discovery-springcloud-example-console</module> <module>discovery-springcloud-example-console</module>
<module>discovery-springcloud-example-eureka</module> <module>discovery-springcloud-example-eureka</module>
<module>discovery-springcloud-example</module> <module>discovery-springcloud-example</module>
...@@ -34,6 +36,7 @@ ...@@ -34,6 +36,7 @@
<commons.collections4.version>4.1</commons.collections4.version> <commons.collections4.version>4.1</commons.collections4.version>
<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>
<nacos.version>0.1.0</nacos.version>
<caffeine.version>2.6.2</caffeine.version> <caffeine.version>2.6.2</caffeine.version>
<swagger.version>2.7.0</swagger.version> <swagger.version>2.7.0</swagger.version>
<spring.cloud.version>Finchley.RELEASE</spring.cloud.version> <spring.cloud.version>Finchley.RELEASE</spring.cloud.version>
...@@ -77,13 +80,13 @@ ...@@ -77,13 +80,13 @@
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-admin-center</artifactId> <artifactId>discovery-plugin-config-center-extension-nacos</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>discovery-console</artifactId> <artifactId>discovery-plugin-admin-center</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
...@@ -107,6 +110,18 @@ ...@@ -107,6 +110,18 @@
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>discovery-console</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-console-extension-nacos</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-console-starter</artifactId> <artifactId>discovery-console-starter</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
...@@ -148,6 +163,12 @@ ...@@ -148,6 +163,12 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId> <groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId> <artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version> <version>${swagger.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