效果展示

先展示下钉钉告警预警图:

钉钉告警预警图

在展示下企业微信告警预警图:

企业微信告警预警图

钉钉告警准备

建立告警群 ,找到告警的钉钉群,点击右上角的群设置:

点击智能群助手:

点击添加群机器人:

点击添加机器人:

选择“自定义”然后点击添加:

输入信息点击完成,这里选择 自定义 关键词,就选择公司简称。当告警内容包含这个关键词时,才会真正发出告警消息。这里要注意下!!!!

会得到一个Webhook地址,这个使我们要告警用的:

准备完成,后面企业微信我就不讲准备了,直接讲代码。上面注意2点:

  • 关键词设置。
  • 拿到Webhook地址,配置到代码就可以告警了。

钉钉告警代码

代码流程分析

钉钉是通过提供 Webhook 地址,我们通过post请求这个地址,发送固定格式数据来实现告警的。第二个就是告警模块不能阻塞,影响应用服务,于是要采用线程池方式。

告警数据格式:

1
2
3
4
5
6
7
8
9
10
11
// 文本消息 
{
"sender":"user123",
"cid":"9f540d572b71cf97a556d95929f335",
"msg":{
"msgtype":"text",
"text":{
"content":"9月运动会通知"
}
}
}

更多格式文档:

发送普通消息

企业微信告警准备

企业微信告警准备流程

代码流程分析

消息推送配置说明

具体代码

maven引入http通信工具:

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>

整体代码如下:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Copyright (c) 2026 www.tchstudy.com Co.Ltd. All rights reserved.
*/
package com.gjcloud;

import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @ClassName Alert
* @Description 告警通知工具类
* @Author 2547714089@qq.com
* @Date 2026/1/30
**/
public class Alert {
private static final int cpuNum = Runtime.getRuntime().availableProcessors();
private static final AtomicInteger increment = new AtomicInteger();
private static volatile ThreadPoolExecutor executor = null;

/**
* 功能描述: 获取线程池
*
* @param
* @return: java.util.concurrent.ThreadPoolExecutor
* @author: 2547714089@qq.com
* @date: 2026/1/30
*/
private static ThreadPoolExecutor getThreadPool() {
if (executor == null) {
synchronized (Alert.class) {
if (executor == null) {
int coreNum = cpuNum <= 0 ? 8 : cpuNum;
// 线程池线程数为cpu的核心数,队列大小5000 。
executor = new ThreadPoolExecutor(coreNum, coreNum, 2000L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(5000), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(Thread.currentThread().getThreadGroup(), r,
"Alert-ThreadPool-" + increment.getAndIncrement());
}
}, new ThreadPoolExecutor.CallerRunsPolicy());
}
}
}
return executor;
}

/**
* 功能描述: Post到 钉钉 或者 企业微信
*
* @param webworkUrl
* @param content
* @return: void
* @author: 2547714089@qq.com
* @date: 2026/1/30
*/
private static void sendPost(String webworkUrl, String content) {
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)// 设置连接超时时间
.readTimeout(20, TimeUnit.SECONDS)// 设置读取超时时间
.build();
MediaType contentType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(contentType, content);
Request request = new Request.Builder().url(webworkUrl).post(body).addHeader("cache-control", "no-cache").build();
try {
Response r = client.newCall(request).execute();
System.err.println(r.toString());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 功能描述: 钉钉告警
*
* @param webworkUrl
* @param msg
* @param throwable
* @return: void
* @author: 2547714089@qq.com
* @date: 2026/1/30
*/
public static void ding(String webworkUrl, final String msg, Throwable throwable) {
// 先组装成钉钉支持的固定格式json,注意title设置为关键词,不然会发不出消息。
// 这个title就是建群机器人设置的关键词
String format = "{\n" +
"\t\"msgtype\":\"markdown\",\n" +
"\t\"markdown\":{\n" +
"\t\t\"title\":\"%s\",\n" +
"\t\t\"text\":\"%s\"\n" +
"\t}\n" +
"}";
// 组装数据格式
final StringBuilder content = new StringBuilder(String.format(format, "uc", msg));
if (throwable != null) {
content.append(", e = ").append(throwable.getLocalizedMessage());
}

getThreadPool().execute(() -> sendPost(webworkUrl, content.toString()));
}

/**
* 功能描述: 企业微信告警
*
* @param webworkUrl
* @param msg
* @param throwable
* @return: void
* @author: 2547714089@qq.com
* @date: 2026/1/30
*/
public static void wechat(String webworkUrl, final String msg, Throwable throwable) {
// 企业微信 没有 title
String format = "{\r\n" +
" \"msgtype\":\"markdown\",\r\n" +
" \"markdown\":{\r\n" +
" \"content\":\"%s\r\n\"" +
" }\r\n" +
"}}";
// 组装 数据格式
final StringBuilder content = new StringBuilder(String.format(format, msg));
if (throwable != null) {
content.append(", e = ").append(throwable.getLocalizedMessage());
}
getThreadPool().execute(() -> sendPost(webworkUrl, content.toString()));
}

public static void main(String[] args) {
wechat("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7378d8b1-24e9-44ab-a2a6-9e19f3187faf",
"公费师范", null);
}
}

企业微信和钉钉不同的就是企业微信没有title,不需要设置为关键词。