Sentinel spring cloud gateway 整合 sentinel 限流不生效

c9x0cxw0  于 2022-10-19  发布在  Spring
关注(0)|答案(9)|浏览(417)

Issue Description

version

spring cloud gateway 2.2.5.RELEASE
cloud Hoxton.SR8
spring boot 2.3.12

dependencies

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <scope>provided</scope>
        </dependency>

<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.0</version>
        </dependency>

<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-extension</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-parameter-flow-control</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-common</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-spring-webflux-adapter</artifactId>
            <version>${sentinel.version}</version>
        </dependency>

Describe what happened (or what feature you want)

1、我在dashboard上保存流控规则后持久化后至nacos

2、gateway 监听nacos规则发生变化后
GatewayRuleManager.loadRules 去加载了规则,但是访问资源时规制并没有生效

case GATEWAY_API:
                GatewayApiDefinitionManager.loadApiDefinitions(sentinelGatewayApiParser.convert(sentinelStrategyRule));
                break;
            case GATEWAY_FLOW:
                GatewayRuleManager.loadRules(sentinelGatewayFlowRuleParser.convert(sentinelStrategyRule));
                break;

结果限流并不生效,请问如何解决

SentinelGatewayFilter routeId 会自动加上前缀,访问API时直接报404

xmd2e60i

xmd2e60i1#

API gateway 默认统计维度都是 route 维度,可以按照簇点链路页面里面的 route 名称来配置,或自定义 API 分组来自定义统计维度。

ajsxfq5m

ajsxfq5m2#

哥们我就是这样配置的。但是实际是不生效的哈
GatewayFlowSlot ->checkGatewayParamFlow -> GatewayRuleManager.getConvertedParamRules

SentinelGatewayFilter -> 结果 routeId 变了。。。然后再到 getRulesForResource 时就直接拿不到配置了。。。

ev7lccsx

ev7lccsx3#

你这个配置明显不是 route 的配置。根据簇点链路页面显示的资源名来配置,或自定义 API 分组。

ulydmbyx

ulydmbyx4#

哥们我是 gateway哈。

4bbkushb

4bbkushb5#

URL 资源名不应该出现在这里。确保下 SCA 版本用的最新版本,并且 application.properties 里面 spring.cloud.sentinel.filter.enabled 配置项置为 false

j9per5c4

j9per5c47#

sentinel-parameter-flow-control也已经添加了的

8ulbf1ek

8ulbf1ek8#

SentinelGatewayFilter 里对api分组的

Set<String> matchingApis = pickMatchingApiDefinitions(exchange);
        for (String apiName : matchingApis) {
            Object[] params = paramParser.parseParameterFor(apiName, exchange,
                r -> r.getResourceMode() == SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME);
            asyncResult = asyncResult.transform(
                new SentinelReactorTransformer<>(new EntryConfig(apiName, ResourceTypeConstants.COMMON_API_GATEWAY,
                    EntryType.IN, 1, params))
            );
        }
//这个地方一直显示没有匹配上的。。。没明白我路径按精确配置的。咋还匹配不上呢
 Set<String> pickMatchingApiDefinitions(ServerWebExchange exchange) {
        return GatewayApiMatcherManager.getApiMatcherMap().values()
            .stream()
            .filter(m -> m.test(exchange))
            .map(WebExchangeApiMatcher::getApiName)
            .collect(Collectors.toSet());
    }

ttygqcqt

ttygqcqt9#

我服了。。。原来是因为匹配串没有把应用名给带上所以一直匹配不上。大佬们这文档能不能说明一下哈。

相关问题