From 2f367e4fe186b96e50f511562b029770b63d5437 Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Mon, 4 May 2020 10:50:15 +0800 Subject: [PATCH] fix sentinel --- .../config/SentinelAutoConfigure.java | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) 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 967b398c..bb56cc9e 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 @@ -2,11 +2,10 @@ package com.central.sentinel.config; import cn.hutool.json.JSONUtil; 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.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -28,14 +27,11 @@ public class SentinelAutoConfigure { /** * 限流、熔断统一处理类 */ - /*@Configuration - @ConditionalOnClass(HttpServletRequest.class)*/ + @Configuration + @ConditionalOnClass(HttpServletRequest.class) public static class WebmvcHandler { - public WebmvcHandler(SentinelWebMvcConfig config) { - config.setBlockExceptionHandler(webmvcBlockExceptionHandler()); - } - - public BlockExceptionHandler webmvcBlockExceptionHandler() { + @Bean + public BlockExceptionHandler blockExceptionHandler() { return (request, response, e) -> { response.setStatus(429); Result result = Result.failed(e.getMessage()); @@ -48,14 +44,11 @@ public class SentinelAutoConfigure { /** * 限流、熔断统一处理类 */ - /*@Configuration - @ConditionalOnClass(ServerResponse.class)*/ + @Configuration + @ConditionalOnClass(ServerResponse.class) public static class WebfluxHandler { - public WebfluxHandler() { - WebFluxCallbackManager.setBlockHandler(webfluxBlockExceptionHandler()); - } - - public BlockRequestHandler webfluxBlockExceptionHandler() { + @Bean + public BlockRequestHandler blockRequestHandler() { return (exchange, t) -> ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS) .contentType(MediaType.APPLICATION_JSON)