Spring Cloud Alibaba之 Sentinel( 三 )


@SentinelRestTemplate 注解的限流(blockHandler, blockHandlerClass)和降级(fallback, fallbackClass)属性不强制填写 。
当使用 RestTemplate 调用被 Sentinel 熔断后 , 会返回 RestTemplate request block by sentinel 信息 , 或者也可以编写对应的方法自行处理返回信息 。 这里提供了 SentinelClientHttpResponse 用于构造返回信息 。
Sentinel RestTemplate 限流的资源规则提供两种粒度:

  • httpmethod:schema://host:port/path:协议、主机、端口和路径
  • httpmethod:schema://host:port:协议、主机和端口
以这个 url 并使用 GET 方法为例 。 对应的资源名有两种粒度 , 分别是 GET: 以及 GET:
动态数据源支持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) 。
rule-type 配置表示该数据源中的规则属于哪种类型的规则(flow , degrade , authority , system, param-flow, gw-flow, gw-api-group) 。
当某个数据源规则信息加载失败的情况下 , 不会影响应用的启动 , 会在日志中打印出错误信息 。


推荐阅读