Spring Cloud入门-Sentinel实现服务限流、熔断与降级(Hoxton版本)
import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelMvcInterceptor;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SentinelConfig {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(sentinelMvcInterceptor()).addPathPatterns("/**");
}
};
}
@Bean
public SentinelMvcInterceptor sentinelMvcInterceptor() {
return new SentinelMvcInterceptor();
}
}
这段代码定义了一个配置类SentinelConfig
,其中创建了SentinelResourceAspect
和SentinelMvcInterceptor
的Bean,以便于在Spring Cloud应用中启用Sentinel对Spring MVC的支持。同时,通过实现WebMvcConfigurer
接口,将Sentinel的拦截器注册到所有路径上,以便对所有API接口进行流量控制和资源保护。
评论已关闭