赞
踩
1:直接上代码,controller层
- 一:controller层
- @GetMapping("/insertAlarmYaml")
- public boolean insertAlarmYaml(@ApiIgnore AlarmRuleDTO alarmRuleDTO) throws ServiceException {
- try {
- Properties properties = new Properties();
- properties.load(new InputStreamReader(Object.class.getResourceAsStream("/AlarmSettingPath.properties"), "GBK"));
- String ymlPath = properties.getProperty("alarmRulePath");
- return alarmQueryService.insertYaml(alarmRuleDTO, ymlPath);
- } catch (Exception e) {
- log.error(e.getMessage());
- return false;
- }
- }
-
-
- 二:service层
-
- /**
- * 新增告警规则配置
- */
- boolean insertYaml(AlarmRuleDTO alarmRuleDTO, String ymlPath) throws ServiceException;
-
-
- 三:service实现层
-
- public boolean insertYaml(AlarmRuleDTO alarmRuleDTO, String ymlPath) throws ServiceException {
- Reader applicationReader = getReader();
- YamlUpdateUtil alarmInsert = new YamlUpdateUtil(applicationReader);
- return alarmInsert.insertAlarmConfig(alarmRuleDTO, ymlPath);
- }
-
- public Reader getReader() {
- Reader applicationReader;
- try {
- applicationReader = ResourceUtils.read("alarm-settings.yml");
- } catch (FileNotFoundException e) {
- throw new ServiceException("can't load alarm-settings.yml");
- }
- return applicationReader;
- }
-
- 四:增删改的操作都是在我自己写的工具类完成的,这里把整个工具类代码附上
- public class YamlUpdateUtil {
-
- private Map yamlData;
- private final static DumperOptions OPTIONS = new DumperOptions();
-
- //以块读取yml文件
- static {
- OPTIONS.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
- OPTIONS.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
- OPTIONS.setPrettyFlow(false);
- }
-
- public YamlUpdateUtil(Reader io) {
- Yaml yaml = new Yaml(new SafeConstructor());
- yamlData = (Map) yaml.load(io);
- }
-
- /**
- * 新增告警规则配置
- */
- public boolean insertAlarmConfig(AlarmRuleDTO alarmRuleDTO, String ymlPath) {
- if (Objects.nonNull(yamlData)) {
- return insertYaml(alarmRuleDTO, ymlPath);
- }
- return false;
- }
-
- /**
- * 更新告警规则配置
- */
- public boolean updateAlarmConfig(AlarmRuleDTO alarmRuleDTO, String ymlPath) {
- if (Objects.nonNull(yamlData)) {
- return updateAlarmYaml(alarmRuleDTO, ymlPath);
- }
- return false;
- }
-
- /**
- * 删除告警规则配置
- */
- public boolean removeAlarmConfig(String ruleName, String ymlPath) {
- if (Objects.nonNull(yamlData)) {
- return removeAlarmYaml(ruleName, ymlPath);
- }
- return false;
- }
-
- /**
- * 更新告警规则配置文件,根据告警规则名更新
- */
- private boolean updateAlarmYaml(AlarmRuleDTO alarmRuleDTO, String ymlPath) {
- Map ruleMap = (Map) yamlData.get("rules");
- if (null == ruleMap) {
- return false;
- }
- ruleMap.forEach((key, value) -> {
- if (((String) key).endsWith("_rule")) {
- //根据告警规则名判断需要更新的规则属性
- if (((String) key).equalsIgnoreCase(alarmRuleDTO.getAlarmRuleName())) {
- Map ymlValue = (Map) value;
- if (alarmRuleDTO.getMetricsName() != null) {
- ymlValue.put("metrics-name", alarmRuleDTO.getMetricsName());
- }
- if (alarmRuleDTO.getOp() != null) {
- ymlValue.put("op", alarmRuleDTO.getOp());
- }
- if (alarmRuleDTO.getThreshold() != null) {
- ymlValue.put("threshold", alarmRuleDTO.getThreshold());
- }
- if (alarmRuleDTO.getPeriod() != null) {
- ymlValue.put("period", alarmRuleDTO.getPeriod());
- }
- if (alarmRuleDTO.getCount() != null) {
- ymlValue.put("count", alarmRuleDTO.getCount());
- }
- if (alarmRuleDTO.getSilencePeriod() != null) {
- ymlValue.put("silence-period", alarmRuleDTO.getSilencePeriod());
- }
- if (alarmRuleDTO.getMessage() != null) {
- ymlValue.put("message", alarmRuleDTO.getMessage());
- }
- yamlData.put("rules", ruleMap);
- Yaml yaml = new Yaml(OPTIONS);
- try {
- FileWriter writer = new FileWriter(ymlPath);
- yaml.dump(yamlData, writer);
- writer.flush();
- } catch (IOException e) {
- log.error(e.getMessage());
- }
- }
- }
- });
- return true;
- }
-
- /**
- * 新增规则告警,告警名称为必输字段且必须以_rule结尾
- */
- private boolean insertYaml(AlarmRuleDTO alarmRuleDTO, String ymlPath) {
- Map ruleMap = (Map) yamlData.get("rules");
- //如果规则一开始是空的,这里应该先创建一个规则
- if (null == ruleMap) {
- ruleMap = new HashMap();
- yamlData.put("rules", ruleMap);
- }
- String ruleName = alarmRuleDTO.getAlarmRuleName();
- //如果传入的规则名称存在,直接返回失败
- if (ruleMap.containsKey(ruleName)) {
- log.error("INSERT:传入的规则名称存在,请核对");
- return false;
- }
- //放入新增的规则属性
- Map<String, Object> ymlValue = new HashMap<>();
- ymlValue.put("metrics-name", alarmRuleDTO.getMetricsName());
- ymlValue.put("op", alarmRuleDTO.getOp());
- ymlValue.put("threshold", alarmRuleDTO.getThreshold());
- ymlValue.put("period", alarmRuleDTO.getPeriod());
- ymlValue.put("count", alarmRuleDTO.getCount());
- ymlValue.put("silence-period", alarmRuleDTO.getSilencePeriod());
- ymlValue.put("message", alarmRuleDTO.getMessage());
- ruleMap.put(ruleName, ymlValue);
- Yaml yaml = new Yaml(OPTIONS);
- try {
- FileWriter writer = new FileWriter(ymlPath);
- yaml.dump(yamlData, writer);
- writer.flush();
- return true;
- } catch (IOException e) {
- log.error(e.getMessage());
- }
- return false;
- }
-
- /**
- * 根据告警规则名删除规则配置
- */
- private boolean removeAlarmYaml(String ruleName, String ymlPath) {
- Map ruleMap = (Map) yamlData.get("rules");
- //如果规则为空,则返回失败
- if (null == ruleMap) {
- return false;
- }
- //如果传入的规则名称不存在,直接返回失败
- if (!ruleMap.containsKey(ruleName)) {
- log.error("REMOVE:传入的规则名称不存在,请核对!");
- return false;
- }
- //根据规则名称删除相应配置
- ruleMap.remove(ruleName);
- Yaml yaml = new Yaml(OPTIONS);
- try {
- FileWriter writer = new FileWriter(ymlPath);
- yaml.dump(yamlData, writer);
- writer.flush();
- return true;
- } catch (IOException e) {
- log.error(e.getMessage());
- }
- return false;
- }
- }
*目前存在的问题的,对yml文件实现的增删改操作之后,要求实现动态加载,目前尚未完成,望各位对这块熟悉的大神能指点一二
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。