Ver código fonte

增加sentinel异常处理器,统一返回系统繁忙,请稍后重试

dengsixing 3 anos atrás
pai
commit
ce740ec85f

+ 21 - 0
src/main/java/com/fdkankan/gateway/config/SentinelBlockRequestHandlerInitConfig.java

@@ -0,0 +1,21 @@
+package com.fdkankan.gateway.config;
+
+import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
+import com.fdkankan.gateway.exception.SentinelBlockRequestHandler;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * sentinel限流异常响应处理器初始化
+ */
+@Slf4j
+@Configuration
+public class SentinelBlockRequestHandlerInitConfig implements CommandLineRunner {
+    @Override
+    public void run(String... args) throws Exception {
+        log.info("初始化限流handler");
+        GatewayCallbackManager.setBlockHandler(new SentinelBlockRequestHandler());
+    }
+}
+

+ 1 - 3
src/main/java/com/fdkankan/gateway/config/SentinelPersistenceConfig.java

@@ -17,9 +17,7 @@ import org.springframework.core.annotation.Order;
 import java.util.Set;
 
 /**
- * @Description: 限流规则持久化
- * @author: bright
- * @Date: 2020/9/25 13:46
+ * @Description: 读取nacos限流规则
  */
 @Configuration
 @Order(2)

+ 25 - 0
src/main/java/com/fdkankan/gateway/exception/SentinelBlockRequestHandler.java

@@ -0,0 +1,25 @@
+package com.fdkankan.gateway.exception;
+
+import static org.springframework.web.reactive.function.BodyInserters.fromValue;
+
+import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
+import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.common.response.ResultData;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.web.reactive.function.server.ServerResponse;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+/**
+ * @Description: Sentinel异常处理类
+ */
+public class SentinelBlockRequestHandler implements BlockRequestHandler {
+
+    @Override
+    public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {
+        return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
+                .contentType(MediaType.APPLICATION_JSON)
+                .body(fromValue(ResultData.error(ErrorCode.SYSTEM_BUSY)));
+    }
+}

+ 8 - 8
src/main/resources/bootstrap-test.yml

@@ -50,14 +50,14 @@ spring:
             namespace: 4dkankan-test
             rule-type: gw_api_group
       #配置限流之后的响应内容
-      scg:
-        fallback:
-          # 两种模式:一种是response返回文字提示信息,一种是redirect,重定向跳转,需要同时配置redirect(跳转的uri)
-          mode: response
-          # 响应的状态
-          response-status: 426
-          # 响应体
-          response-body: '{"code": 426,"message": "限流了,稍后重试!"}'
+#      scg:
+#        fallback:
+#          # 两种模式:一种是response返回文字提示信息,一种是redirect,重定向跳转,需要同时配置redirect(跳转的uri)
+#          mode: response
+#          # 响应的状态
+#          response-status: 426
+#          # 响应体
+#          response-body: '{"code": 426,"message": "限流了,稍后重试!"}'
 #management:
 #  endpoints:
 #    web: