Author: Lijbjava
Email: lijb1121@163.comweb
導入依賴redis
<dependency> <groupId>org.jyaml</groupId> <artifactId>jyaml</artifactId> <version>1.3</version> </dependency>
生成yml文件格式數據庫
--- !com.neunn.monitor.web.utils.generates.rulesbean.Generates groups: !com.neunn.monitor.web.utils.generates.rulesbean.Groups name: redis rules: - !com.neunn.monitor.web.utils.generates.rulesbean.Rules alert: redis Down annotations: !com.neunn.monitor.web.utils.generates.rulesbean.Annotations description: "{{ $labels.instance }} ---- {{ $labels.job }} Down 時間超過5s." summary: "Instance{{ $labels.instance }} down" expr: redis_up == 0 fors: 5s labels: !com.neunn.monitor.web.utils.generates.rulesbean.Labels metricType: 1 severity: red
Beanapp
public class Generates { private Groups groups; } public class Groups { private String name; private List<Rules> rules; } public class Rules { private String alert; private String expr; private String fors; private Labels labels; private Annotations annotations; } public class Labels { private String severity; private int metricType; } public class Annotations { private String summary; private String description; } //省略get/set方法
代碼(demo)測試
數據時寫死的假數據,yml中的數據是來自數據庫的ui
本次業務場景:最終返回給調用者的是String字符串.net
考慮一個請求完成生成臨時文件+讀取文件格式並返回字符串,設計成AOP環繞通知設計
package com.neunn.monitor.web.utils.generates; import com.neunn.monitor.web.utils.generates.rulesbean.*; import org.ho.yaml.Yaml; import org.junit.Test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * 動態生成yml文件內容測試 * [@author](https://my.oschina.net/arthor) lijb * [@company](https://my.oschina.net/u/3478402) 東網 * [@date](https://my.oschina.net/u/2504391) 2019-01-08 */ public class GenerateYml { //寫入yaml配置文件 [@Test](https://my.oschina.net/azibug) public void writeYml() { //初始化數據,將來數據是從數據庫查出來的 //labels Labels labels = new Labels(); labels.setSeverity("red"); labels.setMetricType(1); //annotations Annotations annotations = new Annotations(); annotations.setSummary("Instance{{ $labels.instance }} down"); annotations.setDescription("{{ $labels.instance }} ---- {{ $labels.job }} Down 時間超過5s."); //rules Rules rules = new Rules(); rules.setAlert("redis Down"); rules.setExpr("redis_up == 0"); rules.setFors("5s"); rules.setLabels(labels); rules.setAnnotations(annotations); List<Rules> rulesList=new ArrayList<>(); rulesList.add(rules); //groups Groups groups = new Groups(); groups.setRules(rulesList); groups.setName("redis"); //Generates Generates generates= new Generates(); generates.setGroups(groups); File dumpFile = new File(System.getProperty("user.dir")+"/src/com/neunn/monitor/web/utils/generates/ymlfiles/testYaml.yml"); try { Yaml.dump(generates, dumpFile); } catch (FileNotFoundException e) { e.printStackTrace(); } } //讀yml文件 @Test public void readYml(){ File file = new File(System.getProperty("user.dir"), "/src/com/neunn/monitor/web/utils/generates/ymlfiles/testYaml.yml"); try { // 讀取文件內容 (輸入流) FileInputStream out = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(out); int ch = 0; StringBuilder stringBuilder = new StringBuilder(); while ((ch = isr.read()) != -1) { stringBuilder.append((char) ch); } System.out.println(stringBuilder); } catch (Exception e) { // TODO: handle exception } } }
讀到的格式code
--- !com.neunn.monitor.web.utils.generates.rulesbean.Generates groups: !com.neunn.monitor.web.utils.generates.rulesbean.Groups name: redis rules: - !com.neunn.monitor.web.utils.generates.rulesbean.Rules alert: redis Down annotations: !com.neunn.monitor.web.utils.generates.rulesbean.Annotations description: "{{ $labels.instance }} ---- {{ $labels.job }} Down 時間超過5s." summary: "Instance{{ $labels.instance }} down" expr: redis_up == 0 fors: 5s labels: !com.neunn.monitor.web.utils.generates.rulesbean.Labels metricType: 1 severity: red