alibaba / alibaba/Sentinel

spring cloud gateway 集成 sentinel,自定义 BlockHandler 未生效

Open
#2,765 1 comment 0 reactions 0 assignees View on GitHub
area/gateway-flow-control kind/question
Dominant language
Java
Stars
23.1k
Forks
8.1k
PR merge metrics
No merged PRs in 30d

Description

依赖 spring-cloud-starter-alibaba-sentinel、sentinel-spring-cloud-gateway-adapter 版本 1.8.1

自定义限流处理类
```java
public class MySentinelBlockRequestHandler implements BlockRequestHandler {
@Override
public Mono handleRequest(ServerWebExchange exchange, Throwable t) {
// 判断是否是html访问,如果是则转发url
if (acceptsHtml(exchange)) {
return htmlErrorResponse();
}
// 如果是接口访问,则返回提示
return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
.contentType(MediaType.APPLICATION_JSON)
.body(fromValue(new ResponseData(HttpStatus.TOO_MANY_REQUESTS.value(),"请求太多了")));
}

private Mono htmlErrorResponse() {
String url="http://www.baidu.com";
URI uri = URI.create(url);
return ServerResponse.temporaryRedirect(uri).build();
}

private boolean acceptsHtml(ServerWebExchange exchange) {
try {
List acceptedMediaTypes = exchange.getRequest().getHeaders().getAccept();
acceptedMediaTypes.remove(MediaType.ALL);
MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
return acceptedMediaTypes.stream()
.anyMatch(MediaType.TEXT_HTML::isCompatibleWith);
} catch (InvalidMediaTypeException ex) {
return false;
}
}

/**
* 定义返回的实体类,字段根据需要添加
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
static class ResponseData {
private int code;
private String msg;
}
}
```

配置类:
```java
@Configuration
public class SentinelGatewayAutoConfiguration implements CommandLineRunner {
private final List viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;

public SentinelGatewayAutoConfiguration(ObjectProvider> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}

@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}

@Override
public void run(String... args) {
GatewayCallbackManager.setBlockHandler(new MySentinelBlockRequestHandler());
}
}
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the stated Spring Cloud Gateway and Sentinel 1.8.1 dependencies, then inspect SentinelGatewayAutoConfiguration, GatewayCallbackManager.setBlockHandler, SentinelGatewayBlockExceptionHandler, and SentinelGatewayFilter. Confirm whether the custom BlockRequestHandler is registered and invoked when rate limiting occurs, and document the observed response behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.