提交 b2ab3f4f 编写于 作者: zlt2000's avatar zlt2000

fix sentinel

上级 f32501f4
...@@ -2,11 +2,10 @@ package com.central.sentinel.config; ...@@ -2,11 +2,10 @@ package com.central.sentinel.config;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler; 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.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
import com.central.common.model.Result; import com.central.common.model.Result;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -28,14 +27,11 @@ public class SentinelAutoConfigure { ...@@ -28,14 +27,11 @@ public class SentinelAutoConfigure {
/** /**
* 限流、熔断统一处理类 * 限流、熔断统一处理类
*/ */
/*@Configuration @Configuration
@ConditionalOnClass(HttpServletRequest.class)*/ @ConditionalOnClass(HttpServletRequest.class)
public static class WebmvcHandler { public static class WebmvcHandler {
public WebmvcHandler(SentinelWebMvcConfig config) { @Bean
config.setBlockExceptionHandler(webmvcBlockExceptionHandler()); public BlockExceptionHandler blockExceptionHandler() {
}
public BlockExceptionHandler webmvcBlockExceptionHandler() {
return (request, response, e) -> { return (request, response, e) -> {
response.setStatus(429); response.setStatus(429);
Result result = Result.failed(e.getMessage()); Result result = Result.failed(e.getMessage());
...@@ -48,14 +44,11 @@ public class SentinelAutoConfigure { ...@@ -48,14 +44,11 @@ public class SentinelAutoConfigure {
/** /**
* 限流、熔断统一处理类 * 限流、熔断统一处理类
*/ */
/*@Configuration @Configuration
@ConditionalOnClass(ServerResponse.class)*/ @ConditionalOnClass(ServerResponse.class)
public static class WebfluxHandler { public static class WebfluxHandler {
public WebfluxHandler() { @Bean
WebFluxCallbackManager.setBlockHandler(webfluxBlockExceptionHandler()); public BlockRequestHandler blockRequestHandler() {
}
public BlockRequestHandler webfluxBlockExceptionHandler() {
return (exchange, t) -> return (exchange, t) ->
ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS) ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册