JAVA语言
提示
由于java生态比较丰富,而且几乎所有人都有自己实现POST请求的工具类,这么就不提供SDK了。
建议使用Guava自带限流工具类, 例如10秒告警一次,防止大量请求消耗你自己的服务器,如果使用redis分布式限流更好,这里还用到了Hutool工具包下的http请求类。
Hutool官网说明文档: https://hutool.cn/docs/#/ (opens new window) 最新maven如下
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
下面的告警工具类还用到了 Guava 的限流工具,需额外引入(只跑最小示例则不需要):
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
1
2
3
4
5
2
3
4
5
java告警工具类demo:
# java告警工具类demo
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.google.common.util.concurrent.RateLimiter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Java系统告警工具类
*/
public class SystemAlarmUtil {
private final static RateLimiter rateLimiter = RateLimiter.create(1d);
private SystemAlarmUtil() {
super();
}
public static String sendAlarmMessage(String title, String message) {
return sendAlarmMessage(title, message, null);
}
public static String sendAlarmMessage(String title, String message, String url) {
try {
// count 每次消耗的令牌10个, 那么每10秒才会通过一次 timeout 超时等待的时间, 等待超过1秒就拒绝发送
if (!rateLimiter.tryAcquire(10, 1, TimeUnit.SECONDS)) {
return null;
}
// 创建json对象作为requestBody,注意正文的字段名必须是 body
Map<String, Object> body = new HashMap<>();
body.put("head", title);
body.put("body", message);
if (url != null && !url.isEmpty()) {
body.put("url", url);
}
// 添加请求头信息
Map<String, String> heads = new HashMap<>();
// 使用json发送请求,下面的是必须的
heads.put("Content-Type", "application/json;charset=UTF-8");
HttpResponse response = HttpRequest.post("https://www.phprm.com/services/push/send/4d05f4abdb0a0c2a0269900809946903")
.headerMap(heads, false).body(JSONUtil.toJsonStr(body)).timeout(5 * 1000).execute();
System.out.println("告警推送结果: " + response.body());
return response.body();
} catch (Exception exception) {
exception.printStackTrace();
}
return null;
}
public static void main(String[] args) {
sendAlarmMessage("系统故障告警", "故障模块: 订单模块\n订单ID=xxxx\n订单金额: 100元\n故障原因: xxxx");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
不需要 Guava 限流、只想跑通最小示例的新手,可以用下面的精简版本(只依赖 Hutool):
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import java.util.HashMap;
import java.util.Map;
public class PushDemo {
public static void main(String[] args) {
Map<String, Object> body = new HashMap<>();
body.put("head", "系统故障告警"); // 必填:标题
body.put("body", "故障模块: 订单模块\n订单ID=xxxx"); // 选填:正文,支持换行
body.put("url", ""); // 选填:点击跳转链接
Map<String, String> heads = new HashMap<>();
heads.put("Content-Type", "application/json;charset=UTF-8");
HttpResponse response = HttpRequest.post(
"https://www.phprm.com/services/push/send/4d05f4abdb0a0c2a0269900809946903")
.headerMap(heads, false)
.body(JSONUtil.toJsonStr(body))
.timeout(5000)
.execute();
System.out.println(response.body());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Last Updated: 2026/09/11, 21:21:10