Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
B
byh-alarm-service
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
包
包
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
杨凯
byh-alarm-service
Commits
c17c3248
提交
c17c3248
authored
4月 20, 2026
作者:
Edwin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 修复告警服务
上级
bf546a32
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
104 行增加
和
43 行删除
+104
-43
AlarmServiceImpl.java
...ebaiyihui/alarm/server/service/Impl/AlarmServiceImpl.java
+104
-43
没有找到文件。
alarm-server/src/main/java/com/ebaiyihui/alarm/server/service/Impl/AlarmServiceImpl.java
浏览文件 @
c17c3248
...
@@ -8,6 +8,7 @@ import com.ebaiyihui.alarm.server.util.HttpKit;
...
@@ -8,6 +8,7 @@ import com.ebaiyihui.alarm.server.util.HttpKit;
import
com.ebaiyihui.framework.utils.StringUtil
;
import
com.ebaiyihui.framework.utils.StringUtil
;
import
com.google.gson.Gson
;
import
com.google.gson.Gson
;
import
com.google.gson.JsonObject
;
import
com.google.gson.JsonObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
...
@@ -16,6 +17,7 @@ import javax.crypto.Mac;
...
@@ -16,6 +17,7 @@ import javax.crypto.Mac;
import
javax.crypto.spec.SecretKeySpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.security.InvalidKeyException
;
import
java.security.InvalidKeyException
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.NoSuchAlgorithmException
;
import
java.nio.charset.StandardCharsets
;
import
java.util.*
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
...
@@ -24,6 +26,7 @@ import java.util.stream.Collectors;
...
@@ -24,6 +26,7 @@ import java.util.stream.Collectors;
* @author: yk
* @author: yk
* @create: 2023/08/09 09:52
* @create: 2023/08/09 09:52
**/
**/
@Slf4j
@Service
@Service
public
class
AlarmServiceImpl
implements
AlarmService
{
public
class
AlarmServiceImpl
implements
AlarmService
{
...
@@ -42,49 +45,107 @@ public class AlarmServiceImpl implements AlarmService {
...
@@ -42,49 +45,107 @@ public class AlarmServiceImpl implements AlarmService {
@Override
@Override
public
String
receive
(
List
<
AlarmMessage
>
alarmMessages
)
{
public
String
receive
(
List
<
AlarmMessage
>
alarmMessages
)
{
// for (AlarmMessage alarmMessage : alarmMessages) {
if
(
alarmMessages
==
null
||
alarmMessages
.
isEmpty
())
{
// String name = alarmMessage.getName();
// String message = alarmMessage.getAlarmMessage();
//
// String scope = alarmMessage.getScope();
// // 过滤一下不发送服务通知逻辑
// if (SERVICE.equals(scope)) {
// if (Objects.nonNull(projProperties.getNotNotifyService()) && projProperties.getNotNotifyService().contains(name)) {
// return "success";
// }
// }
// // 过滤一下不发送服务通知接口逻辑
// if (ENDPOINT_RELATION.equalsIgnoreCase(scope)) {
// // 获取路径地址
// String tempName = name.substring(name.indexOf("/"), name.length());
// String pathUrl = tempName.substring(0, tempName.indexOf(" "));
// if (Objects.nonNull(projProperties.getNotNotifyService()) && projProperties.getNotNotifyUrl().contains(pathUrl)) {
// return "success";
// }
// }
//
// // 获取服务名称
// String serviceNme = name.substring(name.indexOf("byh"), name.length());
// List<ServiceInfo> serviceInfos = projProperties.getServiceInfo();
// for (ServiceInfo serviceInfo : serviceInfos) {
// // 相应的服务通知对应人员
// if (serviceInfo.getServiceNames().contains(serviceNme)) {
// // message += "<at user_id=\\\"ou_6cb2f35f9bf8e7d7e977483801ab13de\\\">杨凯</at>";
// message += "<at user_id=\\\"" + serviceInfo.getServiceNotifyName() + "\\\"></at>";
// alarmMessage.setAlarmMessage(message);
// }
// String sendMessage = "{\"msg_type\":\"text\",\"content\":{\"text\":\"Apache SkyWalking Alarm: \\n %s.\"}}";
// String requestBody = getRequestBody(sendMessage, alarmMessage);
// try {
// HttpKit.jsonPost(webHookUrl, requestBody);
// } catch (Exception e) {
// return "fail";
// }
// }
//
// }
return
"success"
;
return
"success"
;
}
}
boolean
hasError
=
false
;
for
(
AlarmMessage
alarmMessage
:
alarmMessages
)
{
try
{
if
(
shouldSkip
(
alarmMessage
))
{
continue
;
}
String
notifyMessage
=
buildNotifyMessage
(
alarmMessage
);
alarmMessage
.
setAlarmMessage
(
notifyMessage
);
String
sendMessage
=
"{\"msg_type\":\"text\",\"content\":{\"text\":\"Apache SkyWalking Alarm:\\n%s\"}}"
;
String
requestBody
=
getRequestBody
(
sendMessage
,
alarmMessage
);
HttpKit
.
jsonPost
(
webHookUrl
,
requestBody
);
}
catch
(
Exception
e
)
{
hasError
=
true
;
log
.
error
(
"Send alarm message failed, alarm={}"
,
alarmMessage
,
e
);
}
}
if
(
hasError
)
{
return
"fail"
;
}
return
"success"
;
}
private
boolean
shouldSkip
(
AlarmMessage
alarmMessage
)
{
String
scope
=
alarmMessage
.
getScope
();
String
name
=
alarmMessage
.
getName
();
if
(
StringUtil
.
isBlank
(
name
))
{
return
false
;
}
if
(
SERVICE
.
equals
(
scope
))
{
return
splitToSet
(
projProperties
.
getNotNotifyService
()).
contains
(
name
);
}
if
(
ENDPOINT_RELATION
.
equalsIgnoreCase
(
scope
))
{
String
pathUrl
=
extractPathUrl
(
name
);
return
!
StringUtil
.
isBlank
(
pathUrl
)
&&
splitToSet
(
projProperties
.
getNotNotifyUrl
()).
contains
(
pathUrl
);
}
return
false
;
}
private
String
buildNotifyMessage
(
AlarmMessage
alarmMessage
)
{
String
message
=
alarmMessage
.
getAlarmMessage
();
if
(
StringUtil
.
isBlank
(
message
))
{
return
""
;
}
String
serviceName
=
extractServiceName
(
alarmMessage
.
getName
());
if
(
StringUtil
.
isBlank
(
serviceName
))
{
return
message
;
}
List
<
ServiceInfo
>
serviceInfos
=
projProperties
.
getServiceInfo
();
if
(
serviceInfos
==
null
||
serviceInfos
.
isEmpty
())
{
return
message
;
}
StringBuilder
notifyUsers
=
new
StringBuilder
();
for
(
ServiceInfo
serviceInfo
:
serviceInfos
)
{
Set
<
String
>
serviceNames
=
splitToSet
(
serviceInfo
.
getServiceNames
());
if
(
serviceNames
.
contains
(
serviceName
)
&&
!
StringUtil
.
isBlank
(
serviceInfo
.
getServiceNotifyName
()))
{
notifyUsers
.
append
(
"<at user_id=\""
).
append
(
serviceInfo
.
getServiceNotifyName
()).
append
(
"\"></at>"
);
}
}
if
(
notifyUsers
.
length
()
>
0
)
{
return
message
+
"\n"
+
notifyUsers
;
}
return
message
;
}
private
String
extractServiceName
(
String
name
)
{
if
(
StringUtil
.
isBlank
(
name
))
{
return
""
;
}
int
byhIndex
=
name
.
indexOf
(
"byh"
);
if
(
byhIndex
<
0
)
{
return
""
;
}
String
service
=
name
.
substring
(
byhIndex
).
trim
();
int
blankIndex
=
service
.
indexOf
(
' '
);
if
(
blankIndex
>
0
)
{
return
service
.
substring
(
0
,
blankIndex
);
}
return
service
;
}
private
String
extractPathUrl
(
String
name
)
{
int
slashIndex
=
name
.
indexOf
(
"/"
);
int
blankIndex
=
name
.
indexOf
(
" "
,
slashIndex
);
if
(
slashIndex
<
0
||
blankIndex
<
0
||
blankIndex
<=
slashIndex
)
{
return
""
;
}
return
name
.
substring
(
slashIndex
,
blankIndex
);
}
private
Set
<
String
>
splitToSet
(
String
value
)
{
if
(
StringUtil
.
isBlank
(
value
))
{
return
Collections
.
emptySet
();
}
return
Arrays
.
stream
(
value
.
split
(
","
))
.
map
(
String:
:
trim
)
.
filter
(
item
->
!
item
.
isEmpty
())
.
collect
(
Collectors
.
toSet
());
}
/**
/**
* deal requestBody,if it has sign set the sign
* deal requestBody,if it has sign set the sign
...
@@ -131,8 +192,8 @@ public class AlarmServiceImpl implements AlarmService {
...
@@ -131,8 +192,8 @@ public class AlarmServiceImpl implements AlarmService {
private
String
sign
(
final
Long
timestamp
,
String
secret
)
throws
NoSuchAlgorithmException
,
InvalidKeyException
{
private
String
sign
(
final
Long
timestamp
,
String
secret
)
throws
NoSuchAlgorithmException
,
InvalidKeyException
{
String
stringToSign
=
timestamp
+
"\n"
+
secret
;
String
stringToSign
=
timestamp
+
"\n"
+
secret
;
Mac
mac
=
Mac
.
getInstance
(
"HmacSHA256"
);
Mac
mac
=
Mac
.
getInstance
(
"HmacSHA256"
);
mac
.
init
(
new
SecretKeySpec
(
s
tringToSign
.
getBytes
(
),
"HmacSHA256"
));
mac
.
init
(
new
SecretKeySpec
(
s
ecret
.
getBytes
(
StandardCharsets
.
UTF_8
),
"HmacSHA256"
));
byte
[]
signData
=
mac
.
doFinal
();
byte
[]
signData
=
mac
.
doFinal
(
stringToSign
.
getBytes
(
StandardCharsets
.
UTF_8
)
);
return
Base64
.
getEncoder
().
encodeToString
(
signData
);
return
Base64
.
getEncoder
().
encodeToString
(
signData
);
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论