From 2d67a0aed7346048d5f1adb3165853d98dde1d99 Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Thu, 16 Apr 2020 11:57:27 +0800 Subject: [PATCH] update spring-cloud-alibaba to v2.1.2.RELEASE --- pom.xml | 2 +- .../zlt-sentinel-spring-boot-starter/pom.xml | 6 ++ .../config/SentinelAutoConfigure.java | 55 +++++++++++++++---- .../src/main/resources/bootstrap.properties | 2 + .../src/main/resources/application.yml | 2 +- .../src/main/resources/application.yml | 4 +- 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index e727893..e715a83 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ 1.8 UTF-8 8 - 2.1.1.RELEASE + 2.1.2.RELEASE 2.1.12.RELEASE Greenwich.SR5 4.4 diff --git a/zlt-commons/zlt-sentinel-spring-boot-starter/pom.xml b/zlt-commons/zlt-sentinel-spring-boot-starter/pom.xml index 34b849d..43e41b9 100644 --- a/zlt-commons/zlt-sentinel-spring-boot-starter/pom.xml +++ b/zlt-commons/zlt-sentinel-spring-boot-starter/pom.xml @@ -40,5 +40,11 @@ spring-boot-configuration-processor true + + + org.springframework + spring-webflux + true + diff --git a/zlt-commons/zlt-sentinel-spring-boot-starter/src/main/java/com/central/sentinel/config/SentinelAutoConfigure.java b/zlt-commons/zlt-sentinel-spring-boot-starter/src/main/java/com/central/sentinel/config/SentinelAutoConfigure.java index 1ff5bc5..cc97960 100644 --- a/zlt-commons/zlt-sentinel-spring-boot-starter/src/main/java/com/central/sentinel/config/SentinelAutoConfigure.java +++ b/zlt-commons/zlt-sentinel-spring-boot-starter/src/main/java/com/central/sentinel/config/SentinelAutoConfigure.java @@ -1,34 +1,65 @@ package com.central.sentinel.config; import cn.hutool.json.JSONUtil; -import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; -import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager; -import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler; +import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager; +import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; +import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig; import com.central.common.model.Result; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.server.ServerResponse; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; /** * Sentinel配置类 * * @author zlt * @date 2019/1/22 + *

+ * 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 c0baec4..e00f272 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 28080a1..32362ed 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 e2a06e3..9f0c4be 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 -- GitLab