diff --git a/pom.xml b/pom.xml
index e727893dee1ada4c9e390a5fb6f8c47261a745d1..e715a83feb0697b1aad7c5a4892cab733b92f43a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,7 +13,7 @@
+ * Blog: https://blog.csdn.net/zlt2000 + * Github: https://github.com/zlt2000 */ public class SentinelAutoConfigure { - public SentinelAutoConfigure() { - WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler()); + /** + * 限流、熔断统一处理类 + */ + @Configuration + @ConditionalOnClass(HttpServletRequest.class) + public static class WebmvcHandler { + public WebmvcHandler(SentinelWebMvcConfig config) { + config.setBlockExceptionHandler(webmvcBlockExceptionHandler()); + } + + public BlockExceptionHandler webmvcBlockExceptionHandler() { + return (request, response, e) -> { + response.setStatus(429); + Result result = Result.failed(e.getMessage()); + response.getWriter().print(JSONUtil.toJsonStr(result)); + }; + } } + /** * 限流、熔断统一处理类 */ - public class CustomUrlBlockHandler implements UrlBlockHandler { - @Override - public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException { - Result result = Result.failed("flow-limiting"); - httpServletResponse.getWriter().print(JSONUtil.toJsonStr(result)); + @Configuration + @ConditionalOnClass(ServerResponse.class) + public static class WebfluxHandler { + public WebfluxHandler() { + WebFluxCallbackManager.setBlockHandler(webfluxBlockExceptionHandler()); + } + + public BlockRequestHandler webfluxBlockExceptionHandler() { + return (exchange, t) -> + ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS) + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromObject(Result.failed(t.getMessage()))); } } } diff --git a/zlt-config/src/main/resources/bootstrap.properties b/zlt-config/src/main/resources/bootstrap.properties index c0baec4bb7830b5f2496fcd41edf9630f2b4ed4d..e00f2725327642bbc3ac6ded88b102e02746fe2c 100644 --- a/zlt-config/src/main/resources/bootstrap.properties +++ b/zlt-config/src/main/resources/bootstrap.properties @@ -4,6 +4,8 @@ spring.profiles.active=dev ##### nacos(注册中心和配置中心)地址 spring.cloud.nacos.server-addr=192.168.28.130:8848 +spring.cloud.nacos.username=nacos +spring.cloud.nacos.password=nacos spring.cloud.nacos.config.file-extension=yml spring.cloud.nacos.config.shared-dataids=common.yml spring.cloud.nacos.config.refreshable-dataids=common.yml diff --git a/zlt-gateway/sc-gateway/src/main/resources/application.yml b/zlt-gateway/sc-gateway/src/main/resources/application.yml index 28080a1d6728e7c62e285844777329d93af827d4..32362edace8d044b2e848f00a30838e0f7c4230b 100644 --- a/zlt-gateway/sc-gateway/src/main/resources/application.yml +++ b/zlt-gateway/sc-gateway/src/main/resources/application.yml @@ -75,7 +75,7 @@ spring: - StripPrefix=1 # sentinel: # datasource.ds1.nacos: -# server-addr: ${zlt.nacos.server-addr} +# server-addr: ${spring.cloud.nacos.server-addr} # data-id: ${spring.application.name}-sentinel-gw-flow # group-id: DEFAULT_GROUP # ruleType: gw-flow diff --git a/zlt-gateway/zuul-gateway/src/main/resources/application.yml b/zlt-gateway/zuul-gateway/src/main/resources/application.yml index e2a06e3d9ffeee2f2cc7b1984fcacbf5935145ba..9f0c4be4030353e9eed9aae57690394475b021aa 100644 --- a/zlt-gateway/zuul-gateway/src/main/resources/application.yml +++ b/zlt-gateway/zuul-gateway/src/main/resources/application.yml @@ -14,14 +14,14 @@ # 限流 # ds1: # nacos: -# server-addr: ${zlt.nacos.server-addr} +# server-addr: ${spring.cloud.nacos.server-addr} # dataId: ${spring.application.name}-sentinel-gw-flow # groupId: DEFAULT_GROUP # rule-type: gw-flow # api分组 # ds2: # nacos: -# server-addr: ${zlt.nacos.server-addr} +# server-addr: ${spring.cloud.nacos.server-addr} # dataId: ${spring.application.name}-sentinel-gw-api-group # groupId: DEFAULT_GROUP # rule-type: gw-api-group