spring cloud alibaba
提供的Sentinel
支持方式如下:
- 使用webmvc方式,即使用
SentinelWebInterceptor
拦截器 - 对Feign的支持,需开启
feign.sentinel.enabled
参数 - 对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.jar
、sentinel-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()</