Spring Cloud Alibaba之 Sentinel( 二 )
spring-cloud-starter-openfeign这是一个 FeignClient 的简单使用示例:
@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)public interface EchoService {@GetMapping(value = "http://kandian.youth.cn/echo/{str}")String echo(@PathVariable("str") String str);}class FeignConfiguration {@Beanpublic EchoServiceFallback echoServiceFallback() {return new EchoServiceFallback();}}class EchoServiceFallback implements EchoService {@Overridepublic String echo(@PathVariable("str") String str) {return "echo fallback";}}Feign 对应的接口中的资源名策略定义:httpmethod:protocol://requesturl 。 @FeignClient 注解中的所有属性 , Sentinel 都做了兼容 。
EchoService 接口中方法 echo 对应的资源名为 GET:http://service-provider/echo/{str} 。
RestTemplate 支持Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护 , 在构造 RestTemplate bean的时候需要加上 @SentinelRestTemplate 注解 。
@Bean@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)public RestTemplate restTemplate() {return new RestTemplate();}@SentinelRestTemplate 注解的属性支持限流(blockHandler, blockHandlerClass)和降级(fallback, fallbackClass)的处理 。
其中 blockHandler 或 fallback 属性对应的方法必须是对应 blockHandlerClass 或 fallbackClass 属性中的静态方法 。
该方法的参数跟返回值跟 org.springframework.http.client.ClientHttpRequestInterceptor#interceptor 方法一致 , 其中参数多出了一个 BlockException 参数用于获取 Sentinel 捕获的异常 。
比如上述 @SentinelRestTemplate 注解中 ExceptionUtil 的 handleException 属性对应的方法声明如下:
public class ExceptionUtil {public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {...}}应用启动的时候会检查 @SentinelRestTemplate 注解对应的限流或降级方法是否存在 , 如不存在会抛出异常
@SentinelRestTemplate 注解的限流(blockHandler, blockHandlerClass)和降级(fallback, fallbackClass)属性不强制填写 。
当使用 RestTemplate 调用被 Sentinel 熔断后 , 会返回 RestTemplate request block by sentinel 信息 , 或者也可以编写对应的方法自行处理返回信息 。 这里提供了 SentinelClientHttpResponse 用于构造返回信息 。
Sentinel RestTemplate 限流的资源规则提供两种粒度:
- httpmethod:schema://host:port/path:协议、主机、端口和路径
- httpmethod:schema://host:port:协议、主机和端口
动态数据源支持SentinelProperties 内部提供了 TreeMap 类型的 datasource 属性用于配置数据源信息 。
比如配置 4 个数据源:
spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.jsonspring.cloud.sentinel.datasource.ds1.file.rule-type=flow#spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json#spring.cloud.sentinel.datasource.ds1.file.data-type=custom#spring.cloud.sentinel.datasource.ds1.file.converter-class=org.springframework.cloud.alibaba.cloud.examples.JsonFlowRuleListConverter#spring.cloud.sentinel.datasource.ds1.file.rule-type=flowspring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848spring.cloud.sentinel.datasource.ds2.nacos.data-id=sentinelspring.cloud.sentinel.datasource.ds2.nacos.group-id=DEFAULT_GROUPspring.cloud.sentinel.datasource.ds2.nacos.data-type=jsonspring.cloud.sentinel.datasource.ds2.nacos.rule-type=degradespring.cloud.sentinel.datasource.ds3.zk.path = /Sentinel-Demo/SYSTEM-CODE-DEMO-FLOWspring.cloud.sentinel.datasource.ds3.zk.server-addr = localhost:2181spring.cloud.sentinel.datasource.ds3.zk.rule-type=authorityspring.cloud.sentinel.datasource.ds4.apollo.namespace-name = applicationspring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = sentinelspring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = http://kandian.youth.cn/index/testspring.cloud.sentinel.datasource.ds4.apollo.rule-type=param-flow这种配置方式参考了 Spring Cloud Stream Binder 的配置 , 内部使用了 TreeMap 进行存储 , comparator 为 String.CASE_INSENSITIVE_ORDER。d1, ds2, ds3, ds4 是 ReadableDataSource 的名字 , 可随意编写 。 后面的 file, zk, nacos , apollo 就是对应具体的数据源 。 它们后面的配置就是这些具体数据源各自的配置 。
每种数据源都有两个共同的配置项: data-type、 converter-class 以及 rule-type 。
data-type 配置项表示 Converter 类型 , Spring Cloud Alibaba Sentinel 默认提供两种内置的值 , 分别是 json 和 xml (不填默认是json) 。 如果不想使用内置的 json 或 xml 这两种 Converter , 可以填写 custom 表示自定义 Converter , 然后再配置 converter-class 配置项 , 该配置项需要写类的全路径名(比如 spring.cloud.sentinel.datasource.ds1.file.converter-class=org.springframework.cloud.alibaba.cloud.examples.JsonFlowRuleListConverter) 。
- Spring security CSRF 跨域访问限制问题
- Spring Boot搭建的一个在线文件预览系统
- 面试官:问你一个,Spring事务是如何传播的?
- 对Spring MVC接口进行Mock测试
- SpringBoot+MyBatis+MySQL读写分离实现
- SpringBoot构造流程源码分析:Web应用类型推断
- 搭建私有Sentry日志收集系统并集成到springboot
- Spring事务原理?事务在方法间如何传播?为什么会失效?
- SpringBoot扫描不到组件?给你提供几种方案
- 分布式锁结合SpringCache
