一、sentinel控制臺(tái)安裝
1.下載控制臺(tái)jar包: wget https://github.com/alibaba/Sentinel/releases/download/1.8.3/sentinel-dashboard-1.8.3.jar
2.啟動(dòng)sentinel控制臺(tái)Dashboard
注意:?jiǎn)?dòng) Sentinel 控制臺(tái)需要 JDK 版本為 1.8 及以上版本了讨。
啟動(dòng)命令:nohup java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.3.jar 2>&1 &
3.訪問(wèn)sentinel dashboard
瀏覽器訪問(wèn):http://192.168.0.124:8080/#/login
默認(rèn)用戶密碼為:sentinel
image.png
二内颗、客戶端接入控制臺(tái)
1.springboot項(xiàng)目pom.xml文件中加入依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<!--springcloud gateway添加次依賴 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.8.1</version>
</dependency>
<!--dubbo服務(wù)添加次依賴 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-apache-dubbo-adapter</artifactId>
<version>1.8.1</version>
</dependency>
2.application.yml配置文件中添加sentinel配置
spring:
cloud:
sentinel:
transport:
dashboard: 192.168.0.124:8080
3.查看控制臺(tái)
image.png
三.規(guī)則設(shè)置
sentinel規(guī)則配置方式有兩種:
1.通過(guò) API 直接修改 (loadRules)
FlowRuleManager.loadRules(List<FlowRule> rules); // 修改流控規(guī)則
DegradeRuleManager.loadRules(List<DegradeRule> rules); // 修改降級(jí)規(guī)則
手動(dòng)修改規(guī)則(硬編碼方式)一般僅用于測(cè)試和演示钧排,生產(chǎn)上一般通過(guò)動(dòng)態(tài)規(guī)則源的方式來(lái)動(dòng)態(tài)管理規(guī)則。
2.通過(guò) DataSource 適配不同數(shù)據(jù)源修改
根據(jù)官方說(shuō)明:
上述 loadRules() 方法只接受內(nèi)存態(tài)的規(guī)則對(duì)象均澳,但更多時(shí)候規(guī)則存儲(chǔ)在文件恨溜、數(shù)據(jù)庫(kù)或者配置中心當(dāng)中符衔。DataSource 接口給我們提供了對(duì)接任意配置源的能力。相比直接通過(guò) API 修改規(guī)則糟袁,實(shí)現(xiàn) DataSource 接口是更加可靠的做法判族。
我們推薦通過(guò)控制臺(tái)設(shè)置規(guī)則后將規(guī)則推送到統(tǒng)一的規(guī)則中心,客戶端實(shí)現(xiàn) ReadableDataSource 接口端監(jiān)聽(tīng)規(guī)則中心實(shí)時(shí)獲取變更项戴,流程如下:
image.png
DataSource
擴(kuò)展常見(jiàn)的實(shí)現(xiàn)方式有:
- 拉模式:客戶端主動(dòng)向某個(gè)規(guī)則管理中心定期輪詢拉取規(guī)則形帮,這個(gè)規(guī)則中心可以是 RDBMS、文件周叮,甚至是 VCS 等辩撑。這樣做的方式是簡(jiǎn)單,缺點(diǎn)是無(wú)法及時(shí)獲取變更仿耽;
- 推模式:規(guī)則中心統(tǒng)一推送合冀,客戶端通過(guò)注冊(cè)監(jiān)聽(tīng)器的方式時(shí)刻監(jiān)聽(tīng)變化,比如使用 Nacos氓仲、Zookeeper 等配置中心。這種方式有更好的實(shí)時(shí)性和一致性保證得糜。
Sentinel 目前支持以下數(shù)據(jù)源擴(kuò)展:
- Pull-based: 動(dòng)態(tài)文件數(shù)據(jù)源敬扛、Consul, Eureka
- Push-based: ZooKeeper, Redis, Nacos, Apollo, etcd
生產(chǎn)環(huán)境需使用push模式
pom添加依賴
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-zookeeper</artifactId>
</dependency>
客戶端添加配置
@Configuration
public class ZookeeperSentinelConfiguration {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Value("${spring.application.name:wcyq-payment-manager-web}")
private String appName;
@Value("${spring.cloud.zookeeper.connect-string}")
private String connectString;
private final String RULE_ROOT_PATH = "/sentinel_rule_config";
/**
* 加載sentinel規(guī)則
*/
@PostConstruct
public void loadRules() {
logger.info("加載sentinel流控規(guī)則數(shù)據(jù)加載");
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(connectString,
RULE_ROOT_PATH + "/" +appName + "/flow", rules -> JSON.parseObject(rules, new TypeReference<List<FlowRule>>(){}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
logger.info("加載sentinel降級(jí)規(guī)則數(shù)據(jù)加載");
ReadableDataSource<String, List<DegradeRule>> deGradeRuleDataSource = new ZookeeperDataSource<>(connectString,
RULE_ROOT_PATH + "/" + appName + "/degrade", rules -> JSON.parseObject(rules, new TypeReference<List<DegradeRule>>(){}));
DegradeRuleManager.register2Property(deGradeRuleDataSource.getProperty());
logger.info("加載sentinel訪問(wèn)控制規(guī)則數(shù)據(jù)加載");
ReadableDataSource<String, List<AuthorityRule>> authorityRuleDataSource = new ZookeeperDataSource<>(connectString,
RULE_ROOT_PATH + "/" + appName + "/authority", rules -> JSON.parseObject(rules, new TypeReference<List<AuthorityRule>>(){}));
AuthorityRuleManager.register2Property(authorityRuleDataSource.getProperty());
logger.info("加載sentinel熱點(diǎn)參數(shù)限流規(guī)則數(shù)據(jù)加載");
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleDataSource = new ZookeeperDataSource<>(connectString,
RULE_ROOT_PATH + "/" + appName + "/param", rules -> JSON.parseObject(rules, new TypeReference<List<ParamFlowRule>>(){}));
ParamFlowRuleManager.register2Property(paramFlowRuleDataSource.getProperty());
logger.info("加載sentinel系統(tǒng)保護(hù)限流規(guī)則數(shù)據(jù)加載");
ReadableDataSource<String, List<SystemRule>> systemRuleDataSource = new ZookeeperDataSource<>(connectString,
RULE_ROOT_PATH + "/" + appName + "/system", rules -> JSON.parseObject(rules, new TypeReference<List<SystemRule>>(){}));
SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
}
#dubbo服務(wù)添加統(tǒng)一降級(jí)
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(ManagerWebApplication.class, args);
// 定義dubbo全局fallback
DubboAdapterGlobalConfig.setConsumerFallback((invoker, invocation, e) -> {
logger.error("dubbo服務(wù)調(diào)用觸發(fā)降級(jí),參數(shù):{}, 服務(wù)名:{}, 方法:{}", invocation.getArguments(), invocation.getServiceName(), invocation.getMethodName());
return AsyncRpcResult.newDefaultAsyncResult(Result.builder()
.setCode(StatusCodeEnum.CODE_00100031.getCode())
.setMsg("當(dāng)前系統(tǒng)服務(wù)繁忙,請(qǐng)稍后再試!"), invocation);
});
}