提交 cc06d883 作者: 方治民

feat: 新增 Uptime 工具类实现,封装 notice Push 类型通知上报方法

上级 e6833563
......@@ -5,4 +5,6 @@ dependencies {
// hutool
implementation "cn.hutool:hutool-core:${hutoolVersion}"
implementation "cn.hutool:hutool-http:${hutoolVersion}"
implementation "cn.hutool:hutool-extra:${hutoolVersion}"
}
/* (C) 2024 YiRing, Inc. */
package com.yiring.common.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpUtil;
import java.util.Objects;
import lombok.Getter;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
/**
* @author Jim
*/
@SuppressWarnings("unused")
@Slf4j
@UtilityClass
public class Uptime {
static String UPTIME_DOMAIN;
static {
UPTIME_DOMAIN = SpringUtil.getProperty("uptime.domain");
}
/**
* 内网请求 监测请求
* eg: <a href="http://uptime.health.yiring.com">uptime.health.yiring.com</a>
*
* @param key key
* @param status 状态
* @param msg 信息
*/
public void noticeInternal(String key, Status status, String msg) {
// 服务器不具备公网访问权限
// http://192.168.173.150
String domain = "http" + "://uptime.health.yiring.com";
notice(domain, key, status, msg);
}
/**
* 公网请求 监测请求
* eg: <a href="https://uptime.yiring.com">uptime.yiring.com</a>
*
* @param key key
* @param status 状态
* @param msg 信息
*/
public void noticeExternal(String key, Status status, String msg) {
// 服务器具备公网访问权限
String domain = "https" + "://uptime.yiring.com";
notice(domain, key, status, msg);
}
/**
* 默认的通知方法
* eg: <a href="https://uptime.yiring.com">${uptime.domain}</a>
*
* @param key key
* @param status 状态
* @param msg 信息
*/
public void notice(String key, Status status, String msg) {
if (StrUtil.isBlank(UPTIME_DOMAIN)) {
throw new RuntimeException("please set ${uptime.domain} value in application config file.");
}
notice(UPTIME_DOMAIN, key, status, msg);
}
/**
* 通过 Push 方式通知 Uptime Kuma 健康指标检测平台
*
* @param domain Uptime Kuma 的 domain 地址
* @param key Push key
* @param status Push status
* @param msg Push message
*/
private void notice(String domain, String key, Status status, String msg) {
String url = "{}/api/push/{}?status={}&msg={}&ping=";
try {
// 读取配置文件中的 uptime.domain 覆盖默认配置
if (!Objects.equals(domain, UPTIME_DOMAIN) && StrUtil.isNotEmpty(UPTIME_DOMAIN)) {
domain = UPTIME_DOMAIN;
}
HttpUtil.get(StrUtil.format(url, domain, key, status.getValue(), msg));
} catch (Exception e) {
log.error("[Uptime Kuma] network connection failure: {}", e.getMessage(), e);
}
}
@Getter
public enum Status {
UP("up"),
DOWN("down");
final String value;
Status(String value) {
this.value = value;
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论