skywalking中新增,修改,删除alarm-setting.yml文件

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文件实现的增删改操作之后,要求实现动态加载,目前尚未完成,望各位对这块熟悉的大神能指点一二

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值