Commit 57b7b9a1 by Nepxion

增强异常处理

parent b72fdd07
...@@ -18,8 +18,7 @@ import java.io.InputStream; ...@@ -18,8 +18,7 @@ import java.io.InputStream;
import java.util.Collections; import java.util.Collections;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
...@@ -45,8 +44,6 @@ import com.nepxion.discovery.plugin.framework.event.RuleChangedEvent; ...@@ -45,8 +44,6 @@ import com.nepxion.discovery.plugin.framework.event.RuleChangedEvent;
// 用法参照ServiceRegistryEndpoint和ServiceRegistryAutoConfiguration // 用法参照ServiceRegistryEndpoint和ServiceRegistryAutoConfiguration
@ManagedResource(description = "Config Endpoint") @ManagedResource(description = "Config Endpoint")
public class ConfigEndpoint implements MvcEndpoint { public class ConfigEndpoint implements MvcEndpoint {
private static final Logger LOG = LoggerFactory.getLogger(ConfigEndpoint.class);
@Autowired @Autowired
private PluginContextAware pluginContextAware; private PluginContextAware pluginContextAware;
...@@ -56,11 +53,11 @@ public class ConfigEndpoint implements MvcEndpoint { ...@@ -56,11 +53,11 @@ public class ConfigEndpoint implements MvcEndpoint {
@Autowired @Autowired
private RuleCache ruleCache; private RuleCache ruleCache;
@RequestMapping(path = "/config/send", method = RequestMethod.POST) @RequestMapping(path = "/config/send-async", method = RequestMethod.POST)
@ApiOperation(value = "推送规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "POST") @ApiOperation(value = "异步推送规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "POST")
@ResponseBody @ResponseBody
@ManagedOperation @ManagedOperation
public ResponseEntity<?> send(@RequestBody @ApiParam(value = "规则配置内容,XML格式", required = true) String config) { public ResponseEntity<?> sendAsync(@RequestBody @ApiParam(value = "规则配置内容,XML格式", required = true) String config) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND); return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND);
...@@ -70,9 +67,7 @@ public class ConfigEndpoint implements MvcEndpoint { ...@@ -70,9 +67,7 @@ public class ConfigEndpoint implements MvcEndpoint {
InputStream inputStream = IOUtils.toInputStream(config, PluginConstant.ENCODING_UTF_8); InputStream inputStream = IOUtils.toInputStream(config, PluginConstant.ENCODING_UTF_8);
pluginEventWapper.fireRuleChanged(new RuleChangedEvent(inputStream), true); pluginEventWapper.fireRuleChanged(new RuleChangedEvent(inputStream), true);
} catch (IOException e) { } catch (IOException e) {
LOG.error("Publish config failed", e); return toExceptionResponseEntity(e, true);
return new ResponseEntity<>(Collections.singletonMap("Message", "Send config failed"), HttpStatus.OK);
} }
// return ResponseEntity.ok().build(); // return ResponseEntity.ok().build();
...@@ -80,6 +75,26 @@ public class ConfigEndpoint implements MvcEndpoint { ...@@ -80,6 +75,26 @@ public class ConfigEndpoint implements MvcEndpoint {
return ResponseEntity.ok().body("OK"); return ResponseEntity.ok().body("OK");
} }
@RequestMapping(path = "/config/send-sync", method = RequestMethod.POST)
@ApiOperation(value = "同步推送规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "POST")
@ResponseBody
@ManagedOperation
public ResponseEntity<?> sendSync(@RequestBody @ApiParam(value = "规则配置内容,XML格式", required = true) String config) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) {
return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND);
}
try {
InputStream inputStream = IOUtils.toInputStream(config, PluginConstant.ENCODING_UTF_8);
pluginEventWapper.fireRuleChanged(new RuleChangedEvent(inputStream), false);
} catch (IOException e) {
return toExceptionResponseEntity(e, true);
}
return ResponseEntity.ok().body("OK");
}
@RequestMapping(path = "/config/view", method = RequestMethod.GET) @RequestMapping(path = "/config/view", method = RequestMethod.GET)
@ApiOperation(value = "查看当前生效的规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "GET") @ApiOperation(value = "查看当前生效的规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "GET")
@ResponseBody @ResponseBody
...@@ -95,6 +110,19 @@ public class ConfigEndpoint implements MvcEndpoint { ...@@ -95,6 +110,19 @@ public class ConfigEndpoint implements MvcEndpoint {
return ResponseEntity.ok().body(content); return ResponseEntity.ok().body(content);
} }
private ResponseEntity<String> toExceptionResponseEntity(Exception e, boolean showDetail) {
String message = null;
if (showDetail) {
message = ExceptionUtils.getStackTrace(e);
} else {
message = e.getMessage();
}
message = "An internal error occurred while processing your request\n" + message;
return new ResponseEntity<String>(message, HttpStatus.INTERNAL_SERVER_ERROR);
}
@Override @Override
public String getPath() { public String getPath() {
return "/"; return "/";
......
...@@ -55,7 +55,7 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser { ...@@ -55,7 +55,7 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser {
} catch (NullPointerException e) { } catch (NullPointerException e) {
throw new PluginException("Input stream is null"); throw new PluginException("Input stream is null");
} catch (Exception e) { } catch (Exception e) {
throw new PluginException("Parse rule xml failed", e); throw new PluginException(e.getMessage(), e);
} finally { } finally {
if (inputStream != null) { if (inputStream != null) {
IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(inputStream);
......
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