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

解决方法

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 {
    @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、重新配置链路流控规则,如下图:

刷新 /sayHello2多次,可以看到限流成功了,如下图 :

后台console报com.alibaba.csp.sentinel.slots.block.flow.FlowException: null

上一篇:ServletConfig与ServletContext详解-ServletConfig实例


下一篇:MongoDB笔记02