Commit 22b71b9e by Nepxion

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

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