Sentinel 原生版本的規則管理經過API 將規則推送至客戶端並直接更新到內存中,並不能直接用於生產環境。不過官方也提供了一種 Push模式,擴展讀數據源ReadableDataSource
,規則中心統一推送,客戶端經過註冊監聽器的方式時刻監聽變化,好比使用 Nacos、Zookeeper 等配置中心。這種方式有更好的實時性和一致性保證。這裏咱們經過配置 Nacos 來實現流控規則的統一存儲配置。html
控制檯推送規則至配置中心,客戶端經過監聽事件從配置中心獲取流控規則。java
pom.xml 引入:git
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <version>1.6.3</version> </dependency>
配置文件:github
# nacos的訪問地址,配置參考 https://blog.52itstyle.vip/archives/4174/ spring.cloud.sentinel.datasource.ds.nacos.server-addr=47.104.187.19:8848 #nacos中存儲規則的dataId,對於dataId使用了${spring.application.name}變量,這樣能夠根據應用名來區分不一樣的規則配置 spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-flow-rules #nacos中存儲規則的groupId spring.cloud.sentinel.datasource.ds.nacos.groupId=SENTINEL_GROUP #定義存儲的規則類型 spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow
修改 pom.xml,原來的<scope>test</scope>
去掉:spring
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
把 src/test
下面的包 com.alibaba.csp.sentinel.dashboard.rule.nacos
拷貝到src/main/java
下面。架構
修改 NacosConfig:app
/** * @author Eric Zhao * @since 1.4.0 */ @Configuration public class NacosConfig { @Value("${nacos.address}") private String address; @Bean public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() { return JSON::toJSONString; } @Bean public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() { return s -> JSON.parseArray(s, FlowRuleEntity.class); } @Bean public ConfigService nacosConfigService() throws Exception { Properties properties = new Properties(); properties.put("serverAddr",address); return ConfigFactory.createConfigService(properties); } }
application.properties 配置引入 Nacos:ide
# nacos的訪問地址,配置參考 https://blog.52itstyle.vip/archives/4174/ nacos.address=47.104.197.19:8848
FlowControllerV2
指定對應的 Bean 開啓 Nacos 適配。spring-boot
@Autowired @Qualifier("flowRuleNacosProvider") private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; @Autowired @Qualifier("flowRuleNacosPublisher") private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
修改sidebar.html
頁面, 流控規則路由從 dashboard.flowV1
改爲 dashboard.flow
ui
<-- nacos 動態規則配置--> <li ui-sref-active="active" ng-if="!entry.isGateway"> <a ui-sref="dashboard.flow({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控規則</a> </li>
如圖所示,界面會多了一個回到單機頁面的按鈕,這裏咱們新增一個流控規則。
登陸 Nacos 後臺,配置管理->配置列表:
點擊進入配置詳情,配置內容以下:
[{ "app": "blog", "clusterConfig": { "fallbackToLocalWhenFail": true, "sampleCount": 10, "strategy": 0, "thresholdType": 0, "windowIntervalMs": 1000 }, "clusterMode": false, "controlBehavior": 0, "count": 2.0, "gmtCreate": 1568617671812, "gmtModified": 1568622253889, "grade": 1, "id": 6, "ip": "10.136.168.88", "limitApp": "default", "port": 8720, "resource": "blogView", "strategy": 0 }]
生產環境下,推送規則正確作法應該是 配置中心控制檯/Sentinel 控制檯 → 配置中心 → Sentinel 數據源 → Sentinel。
https://gitee.com/52itstyle/spring-boot-blog
https://github.com/alibaba/Sentinel
https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard