因为使用sentinel ,每次重启服务的时候,sentinel 内的配置就失效了,需要重新配置sentinel ,所以我们要使用sentinel 的本地配置,在本地配置好sentinel 后,即使是重启了也能自动配置上。
一,配置服务消费者的yml 文件
#通过文件读取限流规则
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
datasource:
ds1:
file:
file: classpath:flowrule.json
data-type: json
rule-type: flow
二,在服务消费者的resources 配置文件夹下面添加 文件
文件名称要和yml 配置文件的名称一致:flowrule.json
内容为:
[ { "resource": "orderFindById", "controlBehavior": 0, "count": 1, "grade": 1, "limitApp": "default", "strategy": 0 } ]
- resource:资源名,即限流规则的作用对象
- count: 限流阈值
- grade: 限流阈值类型(QPS 或并发线程数)
- limitApp: 流控针对的调用来源,若为 default 则不区分调用来源
- strategy: 调用关系限流策略
- controlBehavior: 流量控制效果(直接拒绝、Warm Up、匀速排队) SpringCloud alibaba集成Sentinel后只需要在配置文件中进行相关配置,即可在 Spring 容器中自动注册 DataSource,这点很方便。配置文件添加如下配置
三,随后重启服务消费者
会看到已经有了我们配置的