Commit 346711d6 authored by zhangc's avatar zhangc

commit

parent 773b0433
......@@ -8,7 +8,9 @@ import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
......@@ -19,9 +21,9 @@ import java.util.List;
* @NAME: SentinelConfig
* @DATE: 2021-1-7/15:57
* @DAY_NAME_SHORT: 周四
* @Description: 限流熔断初始化配置
* @Description: 限流熔断初始化配置(只初始化一次,重启系统后配置从新进行初始化)
**/
//@Configuration
@Configuration
public class SentinelConfig {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
......@@ -39,7 +41,7 @@ public class SentinelConfig {
@Value("${sentinel.resource}")
private String limitSource ;
//@PostConstruct
@PostConstruct
private void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
......
package com.jz.dm.service.request;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.common.utils.RedisUtils;
......@@ -79,7 +80,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@Override
@ApiLogAspect(description = "API请求日志")
//@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) {
boolean bResult = false;
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
......@@ -420,6 +421,21 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
*/
public void fallbackGateway(OpenApiRequest request, OpenApiResponse response) {
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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment