Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
dm_project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
姚本章
dm_project
Commits
346711d6
Commit
346711d6
authored
Feb 03, 2021
by
zhangc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit
parent
773b0433
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
5 deletions
+23
-5
SentinelConfig.java
...ateway/src/main/java/com/jz/dm/config/SentinelConfig.java
+5
-3
ApiQueryService.java
.../main/java/com/jz/dm/service/request/ApiQueryService.java
+18
-2
No files found.
jz-dm-apigateway/src/main/java/com/jz/dm/config/SentinelConfig.java
View file @
346711d6
...
@@ -8,7 +8,9 @@ import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
...
@@ -8,7 +8,9 @@ import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
javax.annotation.PostConstruct
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
...
@@ -19,9 +21,9 @@ import java.util.List;
...
@@ -19,9 +21,9 @@ import java.util.List;
* @NAME: SentinelConfig
* @NAME: SentinelConfig
* @DATE: 2021-1-7/15:57
* @DATE: 2021-1-7/15:57
* @DAY_NAME_SHORT: 周四
* @DAY_NAME_SHORT: 周四
* @Description: 限流熔断初始化配置
* @Description: 限流熔断初始化配置
(只初始化一次,重启系统后配置从新进行初始化)
**/
**/
//
@Configuration
@Configuration
public
class
SentinelConfig
{
public
class
SentinelConfig
{
@Bean
@Bean
public
SentinelResourceAspect
sentinelResourceAspect
()
{
public
SentinelResourceAspect
sentinelResourceAspect
()
{
...
@@ -39,7 +41,7 @@ public class SentinelConfig {
...
@@ -39,7 +41,7 @@ public class SentinelConfig {
@Value
(
"${sentinel.resource}"
)
@Value
(
"${sentinel.resource}"
)
private
String
limitSource
;
private
String
limitSource
;
//
@PostConstruct
@PostConstruct
private
void
initFlowRules
()
{
private
void
initFlowRules
()
{
List
<
FlowRule
>
rules
=
new
ArrayList
<>();
List
<
FlowRule
>
rules
=
new
ArrayList
<>();
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/ApiQueryService.java
View file @
346711d6
package
com
.
jz
.
dm
.
service
.
request
;
package
com
.
jz
.
dm
.
service
.
request
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.jz.common.utils.HttpsUtils
;
import
com.jz.common.utils.HttpsUtils
;
import
com.jz.common.utils.RedisUtils
;
import
com.jz.common.utils.RedisUtils
;
...
@@ -79,7 +80,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
...
@@ -79,7 +80,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@Override
@Override
@ApiLogAspect
(
description
=
"API请求日志"
)
@ApiLogAspect
(
description
=
"API请求日志"
)
//@AccessLimit(limit = 10000, sec = 1)
//@AccessLimit(limit = 10000, sec = 1)
@SentinelResource
(
value
=
"api.gateway"
,
fallback
=
"fallbackGateway"
)
@SentinelResource
(
value
=
"api.gateway"
,
fallback
=
"fallbackGateway"
,
blockHandler
=
"exceptionHandler"
)
public
void
doService
(
OpenApiRequest
request
,
OpenApiResponse
response
)
{
public
void
doService
(
OpenApiRequest
request
,
OpenApiResponse
response
)
{
boolean
bResult
=
false
;
boolean
bResult
=
false
;
JSONObject
parameter
=
JSONObject
.
parseObject
(
request
.
getOpenApiParams
());
JSONObject
parameter
=
JSONObject
.
parseObject
(
request
.
getOpenApiParams
());
...
@@ -420,6 +421,21 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
...
@@ -420,6 +421,21 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
*/
*/
public
void
fallbackGateway
(
OpenApiRequest
request
,
OpenApiResponse
response
)
{
public
void
fallbackGateway
(
OpenApiRequest
request
,
OpenApiResponse
response
)
{
log
.
info
(
"用户请求过于频繁触发限流接口:ApiKey为:{}"
+
request
.
getApiKey
());
log
.
info
(
"用户请求过于频繁触发限流接口:ApiKey为:{}"
+
request
.
getApiKey
());
throw
new
GatewayException
(
GatewayResultCode
.
RATE_LIMIT_EXCEEDED
);
//throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED);
response
.
setCode
(
GatewayResultCode
.
RATE_LIMIT_EXCEEDED
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
RATE_LIMIT_EXCEEDED
.
getMsg
());
}
/**
* 熔断方法
* @param request
* @param response
*/
public
void
exceptionHandler
(
OpenApiRequest
request
,
OpenApiResponse
response
,
BlockException
ex
)
{
log
.
info
(
"用户请求过于频繁触发限流接口:ApiKey为:{}"
+
request
.
getApiKey
());
//log.error("异常信息:{}",ex);
//throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED);
response
.
setCode
(
GatewayResultCode
.
RATE_LIMIT_EXCEEDED
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
RATE_LIMIT_EXCEEDED
.
getMsg
());
}
}
}
}
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