Sentinel链路流控模式失效的解决方法

Sentinel链路流控模式失效的解决方法

问题描述

我使用的环境
spring-cloud-alibaba-dependencies:2.2.0 RELEASE
sentinel-datasource-nacos: 1.7.1
控制层及服务层的代码如下所示:

import com.srcb.springcloud.service.ChainLimitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FlowLimitController
{
    @Autowired
    ChainLimitService service;

    @GetMapping("/testA")
    public String testA() {
        return service.testA();
    }

    @GetMapping("/testB")
    public String testB() {
        return service.testA();
    }
}
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Service;

@Service
public class ChainLimitService {
    //  指定资源名,并指定降级方法
    @SentinelResource(value = "testA", blockHandler = "testAFallback")
    public String testA() {
        return "testA";
    }

    public String testAFallback(BlockException ex) {
        return "ex testA";
    }
}

同时在Sentinel Dashboard上配置流控规则
流控模式
这样配置完成之后,按照预期的效果,当多次访问/testA路径,服务器将会响应 ex testA,而多次访问/testB路径,服务器依旧响应 testA

然而,多次尝试后,不管/testA还是/testB路径,都没有触发流控规则。

解决方法

  1. pom.xml中增加sentinel-web-servlet的依赖,我使用的版本是1.7.1

    <dependency>
    	<groupId>com.alibaba.csp</groupId>
    	<artifactId>sentinel-web-servlet</artifactId>
    </dependency>
    
  2. 在项目中添加一个FilterContextConfig配置类,自己构建CommonFilter实例

    import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class FilterContextConfig {
    	/**
         * @NOTE 在spring-cloud-alibaba v2.1.1.RELEASE及前,sentinel1.7.0及后,关闭URL PATH聚合需要通过该方式,spring-cloud-alibaba v2.1.1.RELEASE后,可以通过配置关闭:spring.cloud.sentinel.web-context-unify=false
         * 手动注入Sentinel的过滤器,关闭Sentinel注入CommonFilter实例,修改配置文件中的 spring.cloud.sentinel.filter.enabled=false
         * 入口资源聚合问题:https://github.com/alibaba/Sentinel/issues/1024 或 https://github.com/alibaba/Sentinel/issues/1213
         * 入口资源聚合问题解决:https://github.com/alibaba/Sentinel/pull/1111
         */
        @Bean
        public FilterRegistrationBean sentinelFilterRegistration() {
            FilterRegistrationBean registration = new FilterRegistrationBean();
            registration.setFilter(new CommonFilter());
            registration.addUrlPatterns("/*");
            // 入口资源关闭聚合
            registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
            registration.setName("sentinelFilter");
            registration.setOrder(1);
            return registration;
        }
    }
    
  3. 重新配置链路流控规则
    启动微服务,在Sentinel Dashboard中重新配置流控规则

尝试多次访问/testA路径
链路流控生效
链路流控生效

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值