Commit 22b71b9e by Nepxion

读取配置文件接口由返回InputStream改成String

parent c0aa58bd
...@@ -13,12 +13,9 @@ import io.swagger.annotations.Api; ...@@ -13,12 +13,9 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -30,7 +27,6 @@ import org.springframework.web.bind.annotation.RequestMethod; ...@@ -30,7 +27,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter; 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.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.PluginEventWapper; import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
...@@ -115,19 +111,14 @@ public class ConfigEndpoint { ...@@ -115,19 +111,14 @@ public class ConfigEndpoint {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Config rest control is disabled"); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Config rest control is disabled");
} }
try { pluginEventWapper.fireRuleUpdated(new RuleUpdatedEvent(config), async);
InputStream inputStream = IOUtils.toInputStream(config, PluginConstant.ENCODING_UTF_8);
pluginEventWapper.fireRuleUpdated(new RuleUpdatedEvent(inputStream), async);
} catch (IOException e) {
return toExceptionResponseEntity(e, true);
}
// return ResponseEntity.ok().build(); // return ResponseEntity.ok().build();
return ResponseEntity.ok().body("OK"); return ResponseEntity.ok().body("OK");
} }
private ResponseEntity<String> toExceptionResponseEntity(Exception e, boolean showDetail) { protected ResponseEntity<String> toExceptionResponseEntity(Exception e, boolean showDetail) {
String message = null; String message = null;
if (showDetail) { if (showDetail) {
message = ExceptionUtils.getStackTrace(e); message = ExceptionUtils.getStackTrace(e);
......
...@@ -9,13 +9,10 @@ package com.nepxion.discovery.plugin.configcenter.extension.nacos.adapter; ...@@ -9,13 +9,10 @@ package com.nepxion.discovery.plugin.configcenter.extension.nacos.adapter;
* @version 1.0 * @version 1.0
*/ */
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -26,7 +23,6 @@ import com.alibaba.nacos.api.config.listener.Listener; ...@@ -26,7 +23,6 @@ import com.alibaba.nacos.api.config.listener.Listener;
import com.nepxion.discovery.plugin.configcenter.ConfigAdapter; import com.nepxion.discovery.plugin.configcenter.ConfigAdapter;
import com.nepxion.discovery.plugin.configcenter.extension.nacos.constant.NacosConstant; import com.nepxion.discovery.plugin.configcenter.extension.nacos.constant.NacosConstant;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter; 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.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.RuleClearedEvent; import com.nepxion.discovery.plugin.framework.event.RuleClearedEvent;
...@@ -45,7 +41,7 @@ public class NacosConfigAdapter extends ConfigAdapter { ...@@ -45,7 +41,7 @@ public class NacosConfigAdapter extends ConfigAdapter {
private PluginAdapter pluginAdapter; private PluginAdapter pluginAdapter;
@Override @Override
public InputStream getInputStream() throws Exception { public String getConfig() throws Exception {
String groupKey = pluginContextAware.getGroupKey(); String groupKey = pluginContextAware.getGroupKey();
String group = pluginAdapter.getGroup(); String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId(); String serviceId = pluginAdapter.getServiceId();
...@@ -54,12 +50,7 @@ public class NacosConfigAdapter extends ConfigAdapter { ...@@ -54,12 +50,7 @@ public class NacosConfigAdapter extends ConfigAdapter {
LOG.info("Get remote config from Nacos server, {}={}, serviceId={}, timeout={}", groupKey, group, serviceId, timeout); LOG.info("Get remote config from Nacos server, {}={}, serviceId={}, timeout={}", groupKey, group, serviceId, timeout);
String config = configService.getConfig(serviceId, group, timeout); return configService.getConfig(serviceId, group, timeout);
if (StringUtils.isEmpty(config)) {
return null;
}
return toInputStream(config);
} }
@PostConstruct @PostConstruct
...@@ -73,25 +64,20 @@ public class NacosConfigAdapter extends ConfigAdapter { ...@@ -73,25 +64,20 @@ public class NacosConfigAdapter extends ConfigAdapter {
configService.addListener(serviceId, group, new Listener() { configService.addListener(serviceId, group, new Listener() {
@Override @Override
public void receiveConfigInfo(String config) { public void receiveConfigInfo(String config) {
try { if (StringUtils.isNotEmpty(config)) {
if (StringUtils.isNotEmpty(config)) { LOG.info("Get config updated event from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
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); RuleEntity ruleEntity = pluginAdapter.getRule();
String rule = ruleEntity.getContent();
if (!StringUtils.equals(rule, config)) {
fireRuleUpdated(new RuleUpdatedEvent(config), true);
} else {
LOG.info("Retrieved config is same as current config, ignore to update, {}={}, serviceId={}", groupKey, group, serviceId);
} }
} catch (IOException e) { } else {
e.printStackTrace(); LOG.info("Get config cleared event from Nacos server, {}={}, serviceId={}", groupKey, group, serviceId);
fireRuleCleared(new RuleClearedEvent(), true);
} }
} }
...@@ -101,8 +87,4 @@ public class NacosConfigAdapter extends ConfigAdapter { ...@@ -101,8 +87,4 @@ public class NacosConfigAdapter extends ConfigAdapter {
} }
}); });
} }
private InputStream toInputStream(String config) throws IOException {
return IOUtils.toInputStream(config, PluginConstant.ENCODING_UTF_8);
}
} }
\ No newline at end of file
...@@ -9,10 +9,9 @@ package com.nepxion.discovery.plugin.configcenter; ...@@ -9,10 +9,9 @@ package com.nepxion.discovery.plugin.configcenter;
* @version 1.0 * @version 1.0
*/ */
import java.io.InputStream;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -54,51 +53,55 @@ public class ConfigInitializer { ...@@ -54,51 +53,55 @@ public class ConfigInitializer {
LOG.info("Rule starts to initialize..."); LOG.info("Rule starts to initialize...");
InputStream inputStream = getInputStream(); String config = getConfig();
if (StringUtils.isEmpty(config)) {
return;
}
try { try {
RuleEntity ruleEntity = configParser.parse(inputStream); RuleEntity ruleEntity = configParser.parse(config);
pluginAdapter.setLocalRule(ruleEntity); pluginAdapter.setLocalRule(ruleEntity);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Parse rule xml failed", e); LOG.error("Parse rule xml failed", e);
} }
} }
public InputStream getInputStream() { public String getConfig() {
InputStream inputStream = null; String config = null;
if (remoteConfigLoader != null) { if (remoteConfigLoader != null) {
try { try {
inputStream = remoteConfigLoader.getInputStream(); config = remoteConfigLoader.getConfig();
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Get remote input stream failed", e); LOG.warn("Get remote config failed", e);
} }
if (inputStream != null) { if (StringUtils.isNotEmpty(config)) {
LOG.info("Remote input stream is retrieved"); LOG.info("Remote config is retrieved");
return inputStream; return config;
} else { } else {
LOG.info("Remote input stream isn't retrieved, use local config loader"); LOG.info("Remote config 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 {
inputStream = localConfigLoader.getInputStream(); config = localConfigLoader.getConfig();
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Get local input stream failed", e); LOG.warn("Get local config failed", e);
} }
if (inputStream != null) { if (StringUtils.isNotEmpty(config)) {
LOG.info("Local input stream is retrieved"); LOG.info("Local config is retrieved");
return inputStream; return config;
} else { } else {
LOG.info("Local input stream isn't retrieved"); LOG.info("Local config isn't retrieved");
} }
LOG.info("No input stream is retrieved, use no config settings"); LOG.info("No config is retrieved");
return null; return null;
} }
......
...@@ -9,14 +9,12 @@ package com.nepxion.discovery.plugin.configcenter; ...@@ -9,14 +9,12 @@ package com.nepxion.discovery.plugin.configcenter;
* @version 1.0 * @version 1.0
*/ */
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute; import org.dom4j.Attribute;
import org.dom4j.Document; import org.dom4j.Document;
...@@ -43,28 +41,24 @@ public class ConfigParser implements PluginConfigParser { ...@@ -43,28 +41,24 @@ public class ConfigParser implements PluginConfigParser {
private static final Logger LOG = LoggerFactory.getLogger(ConfigParser.class); private static final Logger LOG = LoggerFactory.getLogger(ConfigParser.class);
@Override @Override
public RuleEntity parse(InputStream inputStream) { public RuleEntity parse(String config) {
try { if (StringUtils.isEmpty(config)) {
String text = IOUtils.toString(inputStream, PluginConstant.ENCODING_UTF_8); throw new PluginException("Config is null or empty");
}
Document document = Dom4JReader.getDocument(text); try {
Document document = Dom4JReader.getDocument(config);
Element rootElement = document.getRootElement(); Element rootElement = document.getRootElement();
return parseRoot(text, rootElement); return parseRoot(config, rootElement);
} catch (NullPointerException e) {
throw new PluginException("Input stream is null");
} catch (Exception e) { } catch (Exception e) {
throw new PluginException(e.getMessage(), e); throw new PluginException(e.getMessage(), e);
} finally {
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
} }
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private RuleEntity parseRoot(String text, Element element) { private RuleEntity parseRoot(String config, Element element) {
LOG.info("Start to parse rule xml..."); LOG.info("Start to parse rule xml...");
int registerElementCount = element.elements(ConfigConstant.REGISTER_ELEMENT_NAME).size(); int registerElementCount = element.elements(ConfigConstant.REGISTER_ELEMENT_NAME).size();
...@@ -97,7 +91,7 @@ public class ConfigParser implements PluginConfigParser { ...@@ -97,7 +91,7 @@ public class ConfigParser implements PluginConfigParser {
RuleEntity ruleEntity = new RuleEntity(); RuleEntity ruleEntity = new RuleEntity();
ruleEntity.setRegisterEntity(registerEntity); ruleEntity.setRegisterEntity(registerEntity);
ruleEntity.setDiscoveryEntity(discoveryEntity); ruleEntity.setDiscoveryEntity(discoveryEntity);
ruleEntity.setContent(text); ruleEntity.setContent(config);
LOG.info("Rule entity=\n{}", ruleEntity); LOG.info("Rule entity=\n{}", ruleEntity);
......
...@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.configcenter.loader; ...@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.configcenter.loader;
* @version 1.0 * @version 1.0
*/ */
import java.io.InputStream;
public interface ConfigLoader { public interface ConfigLoader {
InputStream getInputStream() throws Exception; String getConfig() throws Exception;
} }
\ No newline at end of file
...@@ -11,24 +11,37 @@ package com.nepxion.discovery.plugin.configcenter.loader; ...@@ -11,24 +11,37 @@ package com.nepxion.discovery.plugin.configcenter.loader;
import java.io.InputStream; import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
public abstract class LocalConfigLoader implements ConfigLoader { public abstract class LocalConfigLoader implements ConfigLoader {
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@Override @Override
public InputStream getInputStream() throws Exception { public String getConfig() throws Exception {
String path = getPath(); String path = getPath();
if (StringUtils.isEmpty(path)) { if (StringUtils.isEmpty(path)) {
return null; throw new IllegalArgumentException("Local path isn't set");
} }
String filePath = applicationContext.getEnvironment().resolvePlaceholders(path); InputStream inputStream = null;
try {
return applicationContext.getResource(filePath).getInputStream(); String filePath = applicationContext.getEnvironment().resolvePlaceholders(path);
inputStream = applicationContext.getResource(filePath).getInputStream();
return IOUtils.toString(inputStream, PluginConstant.ENCODING_UTF_8);
} catch (Exception e) {
throw e;
} finally {
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
}
} }
protected abstract String getPath(); protected abstract String getPath();
......
...@@ -9,10 +9,8 @@ package com.nepxion.discovery.plugin.framework.config; ...@@ -9,10 +9,8 @@ package com.nepxion.discovery.plugin.framework.config;
* @version 1.0 * @version 1.0
*/ */
import java.io.InputStream;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
public interface PluginConfigParser { public interface PluginConfigParser {
RuleEntity parse(InputStream inputStream); RuleEntity parse(String config);
} }
\ No newline at end of file
...@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.framework.event; ...@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.framework.event;
* @version 1.0 * @version 1.0
*/ */
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -57,9 +55,9 @@ public class PluginSubscriber { ...@@ -57,9 +55,9 @@ public class PluginSubscriber {
throw new PluginException("RuleUpdatedEvent can't be null"); throw new PluginException("RuleUpdatedEvent can't be null");
} }
InputStream inputStream = ruleUpdatedEvent.getInputStream(); String rule = ruleUpdatedEvent.getRule();
try { try {
RuleEntity ruleEntity = pluninConfigParser.parse(inputStream); RuleEntity ruleEntity = pluninConfigParser.parse(rule);
pluginAdapter.setDynamicRule(ruleEntity); pluginAdapter.setDynamicRule(ruleEntity);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Parse rule xml failed", e); LOG.error("Parse rule xml failed", e);
......
...@@ -9,19 +9,18 @@ package com.nepxion.discovery.plugin.framework.event; ...@@ -9,19 +9,18 @@ package com.nepxion.discovery.plugin.framework.event;
* @version 1.0 * @version 1.0
*/ */
import java.io.InputStream;
import java.io.Serializable; import java.io.Serializable;
public class RuleUpdatedEvent implements Serializable { public class RuleUpdatedEvent implements Serializable {
private static final long serialVersionUID = 2315578803987663866L; private static final long serialVersionUID = 2315578803987663866L;
private InputStream inputStream; private String rule;
public RuleUpdatedEvent(InputStream inputStream) { public RuleUpdatedEvent(String rule) {
this.inputStream = inputStream; this.rule = rule;
} }
public InputStream getInputStream() { public String getRule() {
return inputStream; return rule;
} }
} }
\ No newline at end of file
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