网上很多介绍
先创建实体类信息(比较多 因为返回层数多)
package com.cetcnav.domain.prometheus;
import lombok.Data;
/**
* @Description:
* @Author: dell
* @Date: 2024/4/12 10:28
*/
@Data
public class PromResponceInfo {
/**
* 状态
* 成功-- success
*/
private String status;
/**
* prometheus指标属性和值
*/
private PromDataInfo data;
}
package com.cetcnav.domain.prometheus;
import lombok.Data;
import java.util.List;
/**
* @Description:
* @Author: dell
* @Date: 2024/4/12 10:28
*/
@Data
public class PromDataInfo {
/**
* prometheus监控服务指表参数
*/
private List droppedTargets;
private List<PromResultInfo> activeTargets;
/**
* prometheus结果类型
* vector--瞬时向量
* matrix--区间向量
* scalar--标量
* string--字符串
*/
private String resultType;
private List<PromMetric> result;
}
package com.cetcnav.domain.prometheus;
import lombok.Data;
/**
* prometheus服务指标参数
*
* @author gf
* @date 2022/10/19
*/
@Data
public class PromResultInfo {
/**
* prometheus指标属性
*/
private PromMetricInfo labels;
/**
* prometheus指标值
*/
private String globalUrl;
private String health;
private String lastScrape;
private String lastScrapeDuration;
private String scrapeInterval;
private String scrapePool;
private String scrapeTimeout;
private String scrapeUrl;
}
package com.cetcnav.domain.prometheus;
import lombok.Data;
/**
* @Description:
* @Author: dell
* @Date: 2024/4/12 10:29
*/
@Data
public class PromMetricInfo {
/**
* prometheus指标名称
*/
private String __name__;
/**
* prometheus实例名称
*/
private String instance;
/**
* prometheus任务名称
*/
private String job;
private String cluster;
private String application;
private String exception;
private String method;
private String outcome;
private String status;
private String url;
private String code;
private String handler;
private String queue;
private String vhost;
private String durable;
}
package com.cetcnav.domain.prometheus;
import lombok.Data;
import java.util.List;
/**
* @Description:
* @Author: dell
* @Date: 2024/4/12 10:29
*/
@Data
public class PromMetric {
/**
* metric name和描述当前样本特征的labelsets
*/
private PromMetricInfo metric;
/**
* 一个float64的浮点型数据表示当前样本的值。
*/
private List<Object> values;
private List<Object> value;
}
package com.cetcnav.scheduleTask.redis;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.cetcnav.cache.redis.RedisMemoryUsedBytesCache;
import com.cetcnav.cache.redis.RedisUpCache;
import com.cetcnav.cache.redis.RedisUpTimeCache;
import com.cetcnav.constant.PromConstants;
import com.cetcnav.constant.RedisConstants;
import com.cetcnav.domain.prometheus.PromResponceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
/**
* @Description: Redis定时任务 获取Prometheus数据信息
* @Author: dell
* @Date: 2024/4/26 10:07
*/
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class RedisScheduleTask {
private static Logger log = LoggerFactory.getLogger(RedisScheduleTask.class);
@Value("${prometheus.url}")
private String PROMETHEUS_URL;
/**
* 每小时获取一次
* redis是否存活
*/
// @Scheduled(cron = "0 0 0/1 * * ?")
private void RedisUpTasks() {
log.info("执行获取Redis是否存活,任务时间: " + LocalDateTime.now());
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("query", RedisConstants.REDIS_UP);
paramMap.put("time", LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond());
// 获取到返回的JSON串
String jsonStr = HttpUtil.get(PROMETHEUS_URL, paramMap);
try {
PromResponceInfo info = JSON.parseObject(jsonStr, PromResponceInfo.class);
if (PromConstants.SUCCESS.equals(info.getStatus()) && info.getData().getResult() != null) {
RedisUpCache.addRedisUpCahce(info);
}
} catch (Exception e) {
log.error(ExceptionUtil.getMessage(e), e);
}
}
/**
* 每小时获取一次
* redis运行时长 单位s
*/
// @Scheduled(cron = "0 0 0/1 * * ?")
// @Scheduled(cron = "0/5 * * * * ?")
private void RedisUpTimeTasks() {
log.info("执行获取Redis运行时长(单位s),任务时间: " + LocalDateTime.now());
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("query", RedisConstants.REDIS_UPTIME_IN_SECONDS);
paramMap.put("time", LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond());
// 获取到返回的JSON串
String jsonStr = HttpUtil.get(PROMETHEUS_URL, paramMap);
try {
PromResponceInfo info = JSON.parseObject(jsonStr, PromResponceInfo.class);
if (PromConstants.SUCCESS.equals(info.getStatus()) && info.getData().getResult() != null) {
RedisUpTimeCache.addRedisUpTimeCahce(info);
}
} catch (Exception e) {
log.error(ExceptionUtil.getMessage(e), e);
}
}