mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2026-01-13 07:05:11 +08:00
remove 重大改动 删除sentinel所有相关功能(为什么删除 查看文档详细说明 https://plus-doc.dromara.org/#/questions/sentinel_404)
This commit is contained in:
@@ -37,12 +37,6 @@
|
||||
<artifactId>ruoyi-common-nacos</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel Gateway -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -61,17 +55,6 @@
|
||||
<version>${satoken.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-sentinel</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-apache-dubbo3-adapter</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-satoken</artifactId>
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.adapter.gateway.sc.callback;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||
|
||||
// https://github.com/alibaba/Sentinel/issues/3298
|
||||
// 临时解决 sentinel 限流插件 jdk17 报错问题
|
||||
/**
|
||||
* The default implementation of {@link BlockRequestHandler}.
|
||||
* Compatible with Spring WebFlux and Spring Cloud Gateway.
|
||||
*
|
||||
* @author Eric Zhao
|
||||
*/
|
||||
public class DefaultBlockRequestHandler implements BlockRequestHandler {
|
||||
|
||||
private static final String DEFAULT_BLOCK_MSG_PREFIX = "Blocked by Sentinel: ";
|
||||
|
||||
@Override
|
||||
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable ex) {
|
||||
if (acceptsHtml(exchange)) {
|
||||
return htmlErrorResponse(ex);
|
||||
}
|
||||
// JSON result by default.
|
||||
return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.body(fromObject(buildErrorResult(ex)));
|
||||
}
|
||||
|
||||
private Mono<ServerResponse> htmlErrorResponse(Throwable ex) {
|
||||
return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.syncBody(DEFAULT_BLOCK_MSG_PREFIX + ex.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
private ErrorResult buildErrorResult(Throwable ex) {
|
||||
return new ErrorResult(HttpStatus.TOO_MANY_REQUESTS.value(),
|
||||
DEFAULT_BLOCK_MSG_PREFIX + ex.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reference from {@code DefaultErrorWebExceptionHandler} of Spring Boot.
|
||||
*/
|
||||
private boolean acceptsHtml(ServerWebExchange exchange) {
|
||||
try {
|
||||
List<MediaType> acceptedMediaTypes = exchange.getRequest().getHeaders().getAccept();
|
||||
acceptedMediaTypes.remove(MediaType.ALL);
|
||||
MimeTypeUtils. sortBySpecificity(acceptedMediaTypes);
|
||||
return acceptedMediaTypes.stream()
|
||||
.anyMatch(MediaType.TEXT_HTML::isCompatibleWith);
|
||||
} catch (InvalidMediaTypeException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ErrorResult {
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
ErrorResult(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,6 @@ import org.springframework.boot.context.metrics.buffering.BufferingApplicationSt
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
|
||||
public class RuoYiGatewayApplication {
|
||||
public static void main(String[] args) {
|
||||
// 标记 sentinel 类型为 网关
|
||||
System.setProperty("csp.sentinel.app.type", "1");
|
||||
SpringApplication application = new SpringApplication(RuoYiGatewayApplication.class);
|
||||
application.setApplicationStartup(new BufferingApplicationStartup(2048));
|
||||
application.run(args);
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package org.dromara.gateway.config;
|
||||
|
||||
import org.dromara.gateway.handler.SentinelFallbackHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* 网关限流配置
|
||||
@@ -13,9 +9,5 @@ import org.springframework.core.annotation.Order;
|
||||
*/
|
||||
@Configuration
|
||||
public class GatewayConfig {
|
||||
@Bean
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public SentinelFallbackHandler sentinelGatewayExceptionHandler() {
|
||||
return new SentinelFallbackHandler();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package org.dromara.gateway.handler;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import org.dromara.gateway.utils.WebFluxUtils;
|
||||
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) {
|
||||
return WebFluxUtils.webFluxResponseWriter(exchange.getResponse(), "请求超过最大数,请稍候再试");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
|
||||
ex.printStackTrace();
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user