spring cloud alibaba整合sentinel之webmvc拦截器方式源码简析

本文聚焦于Spring Cloud Alibaba整合Sentinel的webmvc方式,特别是SentinelWebInterceptor拦截器的源码分析。内容涵盖SentinelWebAutoConfiguration启动入口、SentinelProperties配置及拦截器核心逻辑,涉及URL资源清洗和Sentinel保护。
摘要由CSDN通过智能技术生成

spring cloud alibaba提供的Sentinel支持方式如下:

  1. 使用webmvc方式,即使用SentinelWebInterceptor拦截器
  2. 对Feign的支持,需开启feign.sentinel.enabled参数
  3. 对RestTemplate的支持,使用@SentinelRestTemplate注解

本文主要说明使用webmvc方式的源码简析,看本文前需要对Sentinel组件有一定了解。项目依赖的spring cloud的版本信息如下:

Spring Cloud Alibaba Version Sentinel Version Nacos Version
2.2.3.RELEASE 1.8.0 1.3.3

涉及到的关键jar:
spring-cloud-starter-alibaba-sentinel-2.2.3.RELEASE.jarsentinel-spring-webmvc-adapter-1.8.0.jar

1.程序加载入口SentinelWebAutoConfiguration

按照套路,分析的入口肯定starter的某一个AutoConfiguration里。

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
@ConditionalOnClass(SentinelWebInterceptor.class)
@EnableConfigurationProperties(SentinelProperties.class)
public class SentinelWebAutoConfiguration implements WebMvcConfigurer {
   

	private static final Logger log = LoggerFactory
			.getLogger(SentinelWebAutoConfiguration.class);
	@Autowired
	private SentinelProperties properties;
	@Autowired
	private Optional<UrlCleaner> urlCleanerOptional;
	@Autowired
	private Optional<BlockExceptionHandler> blockExceptionHandlerOptional;
	@Autowired
	private Optional<RequestOriginParser> requestOriginParserOptional;
	@Autowired
	private Optional<SentinelWebInterceptor> sentinelWebInterceptorOptional;
    //将拦截器添加到spring mvc的配置中
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
   
		if (!sentinelWebInterceptorOptional.isPresent()) {
   
			return;
		}
		SentinelProperties.Filter filterConfig = properties.getFilter();
		registry.addInterceptor(sentinelWebInterceptorOptional.get()</
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值