您好,登錄后才能下訂單哦!
在Sentinel控制臺(tái)對(duì)某個(gè)微服務(wù)的接口資源配置了流控、降級(jí)等規(guī)則后,若重啟了該微服務(wù),那么配置的相關(guān)規(guī)則就會(huì)丟失,因?yàn)镾entinel默認(rèn)將規(guī)則存放在內(nèi)存中。每次重啟微服務(wù)都得重新配置規(guī)則顯然是不合理的,所以我們需要將配置好的規(guī)則進(jìn)行持久化存儲(chǔ),而Sentinel提供了兩種規(guī)則持久化模式:
本小節(jié)先介紹一下拉模式(pull),該模式的架構(gòu)圖如下:
因?yàn)樾枰x寫本地文件,所以實(shí)現(xiàn)拉模式需要編寫一些代碼,首先在項(xiàng)目中添加如下依賴:
<!-- Sentinel Datasource -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-extension</artifactId>
</dependency>
由于Sentinel有好幾種規(guī)則,所以需要寫的代碼也有點(diǎn)多,具體代碼如下示例:
package com.zj.node.contentcenter.sentinel;
import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.*;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
/**
* 規(guī)則持久化 - 拉模式
*
* @author 01
* @date 2019-08-02
**/
@Slf4j
public class FileDataSourceInitial implements InitFunc {
/**
* 定義并實(shí)現(xiàn)各個(gè)規(guī)則對(duì)象的轉(zhuǎn)換器,用于將json格式的數(shù)據(jù)轉(zhuǎn)換為相應(yīng)的Java對(duì)象
*/
private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<FlowRule>>() {
}
);
private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<DegradeRule>>() {
}
);
private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<SystemRule>>() {
}
);
private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<AuthorityRule>>() {
}
);
private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<ParamFlowRule>>() {
}
);
@Override
public void init() throws Exception {
// 規(guī)則持久化文件所存放的路徑及文件名,可以按需求自行修改
String ruleDir = System.getProperty("user.home") + "/sentinel/rules";
String flowRulePath = ruleDir + "/flow-rule.json";
String degradeRulePath = ruleDir + "/degrade-rule.json";
String systemRulePath = ruleDir + "/system-rule.json";
String authorityRulePath = ruleDir + "/authority-rule.json";
String paramFlowRulePath = ruleDir + "/param-flow-rule.json";
// 目錄路徑及文件若不存在則創(chuàng)建
this.mkdirIfNotExits(ruleDir);
this.createFileIfNotExits(flowRulePath);
this.createFileIfNotExits(degradeRulePath);
this.createFileIfNotExits(systemRulePath);
this.createFileIfNotExits(authorityRulePath);
this.createFileIfNotExits(paramFlowRulePath);
// 注冊(cè)各個(gè)規(guī)則的可讀寫數(shù)據(jù)源
this.registerFlowRWDS(flowRulePath);
this.registerDegradeRWDS(degradeRulePath);
this.registerSystemRWDS(systemRulePath);
this.registerAuthorityRWDS(authorityRulePath);
this.registerParamRWDS(paramFlowRulePath);
}
/**
* 注冊(cè)流控規(guī)則的可讀寫數(shù)據(jù)源
*/
private void registerFlowRWDS(String flowRulePath) throws FileNotFoundException {
// 構(gòu)建可讀數(shù)據(jù)源,用于定時(shí)讀取本地的json文件
ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(
flowRulePath,
flowRuleListParser
);
// 將可讀數(shù)據(jù)源注冊(cè)至FlowRuleManager,當(dāng)文件里的規(guī)則內(nèi)容發(fā)生變化時(shí),就會(huì)更新到緩存里
FlowRuleManager.register2Property(flowRuleRDS.getProperty());
// 構(gòu)建可寫數(shù)據(jù)源
WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
flowRulePath,
this::toJson
);
// 將可寫數(shù)據(jù)源注冊(cè)至transport模塊的WritableDataSourceRegistry中
// 這樣收到控制臺(tái)推送的規(guī)則時(shí),Sentinel會(huì)先更新到內(nèi)存,然后將規(guī)則寫入到文件中
WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
}
/**
* 注冊(cè)降級(jí)規(guī)則的可讀寫數(shù)據(jù)源
*/
private void registerDegradeRWDS(String degradeRulePath) throws FileNotFoundException {
ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
degradeRulePath,
degradeRuleListParser
);
DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
degradeRulePath,
this::toJson
);
WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
}
/**
* 注冊(cè)系統(tǒng)規(guī)則的可讀寫數(shù)據(jù)源
*/
private void registerSystemRWDS(String systemRulePath) throws FileNotFoundException {
ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
systemRulePath,
systemRuleListParser
);
SystemRuleManager.register2Property(systemRuleRDS.getProperty());
WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
systemRulePath,
this::toJson
);
WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
}
/**
* 注冊(cè)授權(quán)規(guī)則的可讀寫數(shù)據(jù)源
*/
private void registerAuthorityRWDS(String authorityRulePath) throws FileNotFoundException {
ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
authorityRulePath,
authorityRuleListParser
);
AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
authorityRulePath,
this::toJson
);
WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
}
/**
* 注冊(cè)熱點(diǎn)參數(shù)規(guī)則的可讀寫數(shù)據(jù)源
*/
private void registerParamRWDS(String paramFlowRulePath) throws FileNotFoundException {
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
paramFlowRulePath,
paramFlowRuleListParser
);
ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
paramFlowRulePath,
this::toJson
);
ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
}
private void mkdirIfNotExits(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
boolean result = file.mkdirs();
log.info("創(chuàng)建目錄: {} filePath: {}", result ? "成功" : "失敗", filePath);
}
}
private void createFileIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
boolean result = file.createNewFile();
log.info("創(chuàng)建文件: {} filePath: {}", result ? "成功" : "失敗", filePath);
}
}
private <T> String toJson(T t) {
return JSON.toJSONString(t);
}
}
這里有兩個(gè)重要的API:
FileRefreshableDataSource
定時(shí)從指定文件中讀取規(guī)則JSON文件【上圖中的本地文件】,如果發(fā)現(xiàn)文件發(fā)生變化,就更新規(guī)則緩存FileWritableDataSource
接收控制臺(tái)規(guī)則推送,并根據(jù)配置,修改規(guī)則JSON文件【上圖中的本地文件】編寫完以上代碼后,還需要在項(xiàng)目的 resources/META-INF/services
目錄下創(chuàng)建一個(gè)文件,名為 com.alibaba.csp.sentinel.init.InitFunc
,如下圖所示:
然后編輯文件內(nèi)容如下:
# 修改為上面FileDataSourceInitial的包名類名全路徑
com.zj.node.contentcenter.sentinel.FileDataSourceInitial
完成以上步驟后,重啟項(xiàng)目,此時(shí)就可以自行測(cè)試一下規(guī)則是否能持久化存儲(chǔ)了。
拉模式的優(yōu)缺點(diǎn):
如果有了解過(guò)規(guī)則持久化相關(guān)配置的小伙伴可能會(huì)有疑問(wèn),Spring Cloud Alibaba不是提供了如下配置了嗎?為什么要全部自己寫呢?
spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json
spring.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=com.alibaba.cloud.examples.JsonFlowRuleListConverter
#spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
關(guān)于這個(gè)問(wèn)題,可以查看一下這個(gè)Issues
官方文檔:
在上一小節(jié)中,我們了解了規(guī)則持久化中拉模式的原理及使用方式,本小節(jié)將介紹在生產(chǎn)環(huán)境中更為常用的推模式(push)。
推模式的架構(gòu)圖如下:
使用推模式進(jìn)行規(guī)則的持久化還是稍微有些麻煩的,因?yàn)樾枰膭?dòng)Sentinel控制臺(tái)的源碼,對(duì)控制臺(tái)的改造主要是為了實(shí)現(xiàn):
這里僅演示對(duì)流控規(guī)則的改造讓其支持推模式的規(guī)則持久化,因?yàn)槠渌?guī)則的改造過(guò)程也是類似的,稍微琢磨一下就可以了。首先需要下載Sentinel的源碼包,我這里使用的是1.6.3版本:
下載并解壓完成后,使用IDE打開(kāi)sentinel-dashboard這個(gè)項(xiàng)目,如下:
第一步:修改該項(xiàng)目的pom.xml文件,找到如下依賴項(xiàng):
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
</dependency>
將<scope>test</scope>
一行注釋掉,即修改為如下:
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!-- <scope>test</scope> -->
</dependency>
第二步:找到 sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
目錄,將整個(gè)目錄拷貝到 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
下,如下圖所示:
拷貝完成后rule包結(jié)構(gòu)如下圖:
第三步:修改流控規(guī)則Controller,到com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2
類的源碼中找到這一段:
@Autowired
@Qualifier("flowRuleDefaultProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleDefaultPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
修改@Qualifier
注解的內(nèi)容,將其修改為:
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
第四步:打開(kāi)sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
文件,找到一段被注釋的代碼,如下:
<!--<li ui-sref-active="active" ng-if="entry.appType==0">-->
<!--<a ui-sref="dashboard.flow({app: entry.app})">-->
<!--<i class="glyphicon glyphicon-filter"></i> 流控規(guī)則 V1</a>-->
<!--</li>-->
只需要把注釋解開(kāi),即改為:
<li ui-sref-active="active" ng-if="entry.appType==0">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控規(guī)則 V1</a>
</li>
到這步為止,我們就對(duì)sentinel-dashboard源碼改造完畢了,現(xiàn)在流控規(guī)則就可以支持推模式的持久化了,接下來(lái)就是編譯、啟動(dòng)以及測(cè)試。
打開(kāi)idea的terminal,執(zhí)行如下命令進(jìn)行打包編譯:
mvn clean package -DskipTests
然后進(jìn)入target目錄,使用如下命令執(zhí)行jar包,啟動(dòng)Sentinel控制臺(tái):
java -jar sentinel-dashboard.jar
注:也可以選擇直接在idea中點(diǎn)擊啟動(dòng)按鈕來(lái)啟動(dòng)Sentinel控制臺(tái),效果是一樣的
啟動(dòng)完成后,使用瀏覽器打開(kāi),可以看到Sentinel的菜單欄中比之前多出了一項(xiàng)流控規(guī)則 V1:
注:若沒(méi)有顯示該項(xiàng),可以嘗試清除瀏覽器緩存或換個(gè)瀏覽器打開(kāi)
改造完控制臺(tái)后,接下來(lái)開(kāi)始改造客戶端,首先在項(xiàng)目中添加如下依賴:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
然后添加各個(gè)規(guī)則配置:
spring:
cloud:
sentinel:
datasource:
# 名稱隨意
flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
# 規(guī)則類型,取值見(jiàn):
# org.springframework.cloud.alibaba.sentinel.datasource.RuleType
rule-type: flow
degrade:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
rule-type: degrade
system:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-system-rules
groupId: SENTINEL_GROUP
rule-type: system
authority:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
rule-type: authority
param-flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
rule-type: param-flow
完成以上步驟后重啟項(xiàng)目,然后回到Sentinel控制臺(tái)里的流控規(guī)則 V1中新增流控規(guī)則,之所以不在簇點(diǎn)鏈路中添加,是因?yàn)榇攸c(diǎn)鏈路中的按鈕依舊是調(diào)用之前的邏輯添加到內(nèi)存中。新增流控規(guī)則如下:
新增完成后,到Nacos Server的配置列表上,可以看到該規(guī)則的配置數(shù)據(jù),證明已經(jīng)持久化存儲(chǔ)到Nacos了:
若直接在Nacos上修改流控規(guī)則,然后刷新Sentinel控制臺(tái),控制臺(tái)上的顯示也會(huì)被修改
此時(shí)重啟Sentinel控制臺(tái)和微服務(wù),然后刷新控制臺(tái),可以發(fā)現(xiàn)該流控規(guī)則依舊存在:
以上折騰了那么多只實(shí)現(xiàn)了流控規(guī)則的持久化,這還是因?yàn)楣俜綔?zhǔn)備好了示例代碼。Sentinel有若干種規(guī)則,例如降級(jí)規(guī)則、系統(tǒng)規(guī)則、授權(quán)規(guī)則、熱點(diǎn)規(guī)則等,都需要使用類似的方式,修改 com.alibaba.csp.sentinel.dashboard.controller
包中對(duì)應(yīng)的Controller,才能實(shí)現(xiàn)持久化,所以本小節(jié)僅是拋磚引玉,其他規(guī)則可以自行動(dòng)手參考著實(shí)現(xiàn)。
推模式優(yōu)缺點(diǎn):
官方文檔:
在生產(chǎn)環(huán)境使用Sentinel是必須要實(shí)現(xiàn)規(guī)則持久化的,而通過(guò)以上兩個(gè)小節(jié)的學(xué)習(xí),我們可以得知不管使用哪個(gè)模式都需要進(jìn)行相應(yīng)的改動(dòng),其中想要實(shí)現(xiàn)貼近生產(chǎn)使用的推模式需要改動(dòng)的地方更多更麻煩。
如果不想做這些麻煩的改動(dòng),又希望在生產(chǎn)環(huán)境使用Sentinel的話,則需要考慮使用阿里云提供的在線托管Sentinel控制臺(tái)(AHAS),該在線Sentinel控制臺(tái)實(shí)現(xiàn)了推模式的規(guī)則持久化并可用于生產(chǎn):
接下來(lái)演示一下如何使用這個(gè)在線的Sentinel控制臺(tái),根據(jù)開(kāi)通說(shuō)明文檔注冊(cè)了阿里云賬戶后,進(jìn)入開(kāi)通頁(yè)面:
根據(jù)提示開(kāi)通完成后,進(jìn)入管理控制臺(tái),點(diǎn)擊接入應(yīng)用流控:
進(jìn)入應(yīng)用接入界面后可以選擇不同的應(yīng)用接入,這里按照Spring Boot應(yīng)用接入的說(shuō)明完成相應(yīng)步驟:
需要說(shuō)明一下的是,第二步中的HTTP接口埋點(diǎn)和普通接口埋點(diǎn)都可以省略掉,因?yàn)?code>spring-cloud-starter-alibaba-sentinel依賴包里已經(jīng)實(shí)現(xiàn)了,但需要排除該依賴中的sentinel-transport-simple-http
模塊,避免連接了本地的Sentinel控制臺(tái),即修改依賴如下并添加AHAS Client依賴:
<!-- Sentinel -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<!-- 排除該模塊的目的是不與本地的Sentinel控制臺(tái)進(jìn)行通信,以免造成不必要的干擾 -->
<exclusions>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- ahas client -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>spring-boot-starter-ahas-sentinel-client</artifactId>
<version>1.3.3</version>
</dependency>
除了修改依賴以外,還需要將配置文件中關(guān)于Sentinel控制臺(tái)的配置都注釋掉,因?yàn)榇藭r(shí)我們連接的是在線的Sentinel控制臺(tái)。如下:
然后只需要添加AHAS的啟動(dòng)參數(shù):
ahas:
license: xxxxxxxxx
namespace: default
project:
name: ${spring.application.name}
完成以上步驟后,重啟項(xiàng)目并訪問(wèn)該項(xiàng)目的接口,應(yīng)用正常接入的情況下可以在阿里云的控制臺(tái)中看到該應(yīng)用的展示,如下:
點(diǎn)擊該應(yīng)用就可以進(jìn)入到Sentinel控制臺(tái),可以看到這里基本上和我們自己搭建的Sentinel控制臺(tái)是差不多的,同樣支持實(shí)時(shí)監(jiān)控、查看簇點(diǎn)鏈路及配置各種規(guī)則:
例如流控規(guī)則的添加也是一樣的,只是界面稍微好看一點(diǎn)而已:
至此就完成將應(yīng)用接入AHAS了,由于操作與Sentinel控制臺(tái)基本一樣這里就不展開(kāi)介紹了,可以自行測(cè)試搗鼓一下,反正可視化的頁(yè)面使用起來(lái)還是比較容易的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。