Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sl-gateway
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhouwei
sl-gateway
Commits
2691b976
Commit
2691b976
authored
Apr 14, 2025
by
zhouwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add alarm service
parent
18300239
Changes
5
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
277 additions
and
5 deletions
+277
-5
AppConfig.java
src/main/java/com/nanyan/securitylink/config/AppConfig.java
+19
-0
Response.java
src/main/java/com/nanyan/securitylink/entity/Response.java
+61
-0
WeChatMsg.java
src/main/java/com/nanyan/securitylink/entity/WeChatMsg.java
+13
-0
FailoverGatewayFilterFactory.java
...yan/securitylink/filter/FailoverGatewayFilterFactory.java
+132
-5
AlertService.java
...in/java/com/nanyan/securitylink/service/AlertService.java
+52
-0
No files found.
src/main/java/com/nanyan/securitylink/config/AppConfig.java
0 → 100644
View file @
2691b976
package
com
.
nanyan
.
securitylink
.
config
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.cloud.context.config.annotation.RefreshScope
;
import
org.springframework.context.annotation.Configuration
;
@Slf4j
@RefreshScope
@Configuration
public
class
AppConfig
{
@Value
(
"${alert.host}"
)
String
alertHost
;
public
String
getAlertHost
()
{
return
alertHost
;
}
}
src/main/java/com/nanyan/securitylink/entity/Response.java
0 → 100644
View file @
2691b976
package
com
.
nanyan
.
securitylink
.
entity
;
import
com.nanyan.securitylink.common.MsgCode
;
public
class
Response
<
T
>
{
String
msg
;
int
code
;
T
data
;
public
static
Response
getResponse
(
MsgCode
msgCode
){
Response
response
=
new
Response
();
response
.
setCode
(
msgCode
.
getCode
());
response
.
setMsg
(
msgCode
.
getMsg
());
return
response
;
}
public
static
Response
getResponse
(
int
code
,
String
msg
){
Response
response
=
new
Response
();
response
.
setCode
(
code
);
response
.
setMsg
(
msg
);
return
response
;
}
public
static
<
T
>
Response
<
T
>
SUCCESS
(
T
data
){
Response
response
=
new
Response
<
T
>();
response
.
setCode
(
MsgCode
.
SUCCESS
.
getCode
());
response
.
setMsg
(
MsgCode
.
SUCCESS
.
getMsg
());
response
.
setData
(
data
);
return
response
;
}
public
static
<
T
>
Response
<
T
>
FAILED
(
T
data
){
Response
response
=
new
Response
();
response
.
setCode
(
MsgCode
.
FAILED
.
getCode
());
response
.
setMsg
(
MsgCode
.
FAILED
.
getMsg
());
response
.
setData
(
data
);
return
response
;
}
public
String
getMsg
()
{
return
msg
;
}
public
void
setMsg
(
String
msg
)
{
this
.
msg
=
msg
;
}
public
int
getCode
()
{
return
code
;
}
public
void
setCode
(
int
code
)
{
this
.
code
=
code
;
}
public
T
getData
()
{
return
data
;
}
public
void
setData
(
T
data
)
{
this
.
data
=
data
;
}
}
src/main/java/com/nanyan/securitylink/entity/WeChatMsg.java
0 → 100644
View file @
2691b976
package
com
.
nanyan
.
securitylink
.
entity
;
import
lombok.Data
;
@Data
public
class
WeChatMsg
{
String
msgtype
;
MsgText
text
;
MsgText
markdown
;
public
static
class
MsgText
{
public
String
content
;
}
}
src/main/java/com/nanyan/securitylink/filter/FailoverGatewayFilterFactory.java
View file @
2691b976
This diff is collapsed.
Click to expand it.
src/main/java/com/nanyan/securitylink/service/AlertService.java
0 → 100644
View file @
2691b976
package
com
.
nanyan
.
securitylink
.
service
;
import
com.alibaba.fastjson.JSONObject
;
import
com.nanyan.securitylink.config.AppConfig
;
import
com.nanyan.securitylink.entity.WeChatMsg
;
import
com.nanyan.securitylink.vo.Response
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.client.RestTemplate
;
import
java.nio.charset.StandardCharsets
;
@Slf4j
@Service
public
class
AlertService
{
@Autowired
RestTemplate
restTemplate
;
@Autowired
AppConfig
appConfig
;
public
boolean
sendAlertMsg
(
String
msg
){
WeChatMsg
weChatMsg
=
buildAlertMsg
(
msg
);
log
.
info
(
"发送企业微信msg:{}"
,
JSONObject
.
toJSONString
(
weChatMsg
));
HttpEntity
<
String
>
entity
=
new
HttpEntity
<>(
JSONObject
.
toJSONString
(
weChatMsg
),
getHeader
().
getHeaders
());
ResponseEntity
<
Response
>
response
=
restTemplate
.
postForEntity
(
String
.
format
(
"%s/api/v1/msg"
,
appConfig
.
getAlertHost
()),
entity
,
Response
.
class
);
log
.
info
(
"发送企业微信返回:{}"
,
JSONObject
.
toJSONString
(
response
));
return
true
;
}
private
WeChatMsg
buildAlertMsg
(
String
content
)
{
if
(
content
.
length
()>
4000
){
content
=
content
.
substring
(
0
,
4000
);
}
WeChatMsg
weChatMsg
=
new
WeChatMsg
();
weChatMsg
.
setMsgtype
(
"markdown"
);
WeChatMsg
.
MsgText
msgText
=
new
WeChatMsg
.
MsgText
();
String
builder
=
"## dify异常\n"
+
">内容:<font color=\"comment\"> "
+
content
+
"</font>\n"
;
msgText
.
content
=
builder
;
weChatMsg
.
setMarkdown
(
msgText
);
return
weChatMsg
;
}
private
HttpEntity
getHeader
(){
HttpHeaders
httpHeaders
=
new
HttpHeaders
();
httpHeaders
.
set
(
"Content-Type"
,
"application/json"
);
return
new
HttpEntity
<>(
httpHeaders
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment