mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-01-26 11:51:55 +08:00
若依 2.0
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package com.ruoyi.gateway.config;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
|
||||
import com.ruoyi.gateway.handler.SentinelFallbackHandler;
|
||||
|
||||
/**
|
||||
* 网关限流配置
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
public class GatewayConfig
|
||||
{
|
||||
@Bean
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public SentinelFallbackHandler sentinelGatewayExceptionHandler()
|
||||
{
|
||||
return new SentinelFallbackHandler();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(-1)
|
||||
public GlobalFilter sentinelGatewayFilter()
|
||||
{
|
||||
return new SentinelGatewayFilter();
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import com.ruoyi.gateway.handler.HystrixFallbackHandler;
|
||||
import com.ruoyi.gateway.handler.ValidateCodeHandler;
|
||||
|
||||
/**
|
||||
@@ -19,20 +18,14 @@ import com.ruoyi.gateway.handler.ValidateCodeHandler;
|
||||
public class RouterFunctionConfiguration
|
||||
{
|
||||
@Autowired
|
||||
private HystrixFallbackHandler hystrixFallbackHandler;
|
||||
|
||||
@Autowired
|
||||
private ValidateCodeHandler imageCodeHandler;
|
||||
private ValidateCodeHandler validateCodeHandler;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Bean
|
||||
public RouterFunction routerFunction()
|
||||
{
|
||||
return RouterFunctions
|
||||
.route(RequestPredicates.path("/fallback").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),
|
||||
hystrixFallbackHandler)
|
||||
.andRoute(RequestPredicates.GET("/code").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),
|
||||
imageCodeHandler);
|
||||
return RouterFunctions.route(
|
||||
RequestPredicates.GET("/code").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),
|
||||
validateCodeHandler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.ruoyi.gateway.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import java.util.Optional;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR;
|
||||
|
||||
/**
|
||||
* 熔断降级处理
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Component
|
||||
public class HystrixFallbackHandler implements HandlerFunction<ServerResponse>
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(HystrixFallbackHandler.class);
|
||||
|
||||
@Override
|
||||
public Mono<ServerResponse> handle(ServerRequest serverRequest)
|
||||
{
|
||||
Optional<Object> originalUris = serverRequest.attribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
|
||||
originalUris.ifPresent(originalUri -> log.error("网关执行请求:{}失败,hystrix服务降级处理", originalUri));
|
||||
return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR.value()).contentType(MediaType.APPLICATION_JSON)
|
||||
.body(BodyInserters.fromValue(JSON.toJSONString(R.failed("服务已被降级熔断"))));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.ruoyi.gateway.handler;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebExceptionHandler;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 自定义限流异常处理
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class SentinelFallbackHandler implements WebExceptionHandler
|
||||
{
|
||||
private Mono<Void> writeResponse(ServerResponse response, ServerWebExchange exchange)
|
||||
{
|
||||
ServerHttpResponse serverHttpResponse = exchange.getResponse();
|
||||
serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
|
||||
byte[] datas = "{\"status\":429,\"message\":\"请求超过最大数,请稍后再试\"}".getBytes(StandardCharsets.UTF_8);
|
||||
DataBuffer buffer = serverHttpResponse.bufferFactory().wrap(datas);
|
||||
return serverHttpResponse.writeWith(Mono.just(buffer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex)
|
||||
{
|
||||
if (exchange.getResponse().isCommitted())
|
||||
{
|
||||
return Mono.error(ex);
|
||||
}
|
||||
if (!BlockException.isBlockException(ex))
|
||||
{
|
||||
return Mono.error(ex);
|
||||
}
|
||||
return handleBlockedRequest(exchange, ex).flatMap(response -> writeResponse(response, exchange));
|
||||
}
|
||||
|
||||
private Mono<ServerResponse> handleBlockedRequest(ServerWebExchange exchange, Throwable throwable)
|
||||
{
|
||||
return GatewayCallbackManager.getBlockHandler().handleRequest(exchange, throwable);
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,10 @@ package com.ruoyi.gateway.service.impl;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.FastByteArrayOutputStream;
|
||||
|
||||
import com.google.code.kaptcha.Producer;
|
||||
import com.ruoyi.common.core.constant.Constants;
|
||||
import com.ruoyi.common.core.exception.CaptchaException;
|
||||
|
||||
Reference in New Issue
Block a user