sentinel控制臺源碼:https://download.csdn.net/download/yixin605691235/89543923
sentinel控制臺jar包:https://download.csdn.net/download/yixin605691235/89543931
不同環境直接修改jar包中的application.yml文件中的nacos地址就可以了。
一、網關限流配置及使用
1、nacos配置
新增文件gdebs-gateway-sentinel.yml、gdebs-gateway-sentinel-dynamic.properties
spring:cloud:sentinel:eager: true#配置網關scg:fallback:content-type: application/json# 模式 response、redirectmode: response# 響應狀態碼response-status: ${sentinel.renturn.code}# 響應信息response-body: ${sentinel.renturn.msg}transport:dashboard: ${sentinel.dashboard.host}:${sentinel.dashboard.port}filter:enabled: falseip: gdebs-gateway-service# 控制臺數據持久化配置datasource:ds1:nacos:server-addr: ${sentinel.nacos.host}:${sentinel.nacos.port}username: ${sentinel.nacos.name}password: ${sentinel.nacos.pwd}namespace: ${sentinel.nacos.namespace}group-id: ${sentinel.nacos.group}data-id: ${spring.application.name}-sentinel-flow-rules.jsondata-type: jsonrule-type: gw-flowds2:nacos:server-addr: ${sentinel.nacos.host}:${sentinel.nacos.port}username: ${sentinel.nacos.name}password: ${sentinel.nacos.pwd}namespace: ${sentinel.nacos.namespace}group-id: ${sentinel.nacos.group}data-id: ${spring.application.name}-sentinel-api-rules.jsondata-type: jsonrule-type: gw-api-groupgateway:# spring cloud gateway 路由配置方式discovery:locator:#表明gateway開啟服務注冊和發現的功能enabled: true#將請求路徑上的服務名配置為小寫lower-case-service-id: true
# sentinel控制臺信息
sentinel.dashboard.host=xxxx
sentinel.dashboard.port=xxx# sentinel返回信息
sentinel.renturn.msg= 對不起,已經被限流了!!!
sentinel.renturn.code= xxx# sentinel數據持久化配置
sentinel.nacos.host=xxx.xx.xx.xx
sentinel.nacos.port=xxxx
sentinel.nacos.name=xx
sentinel.nacos.pwd=nacxxos
sentinel.nacos.namespace=xxx
sentinel.nacos.group=xxx
spring.application.name=xxx
2、網關應用增加依賴:
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-spring-cloud-gateway-adapter</artifactId></dependency>
3、控制臺配置限流規則
http://localhost:8081/#/login
帳號/密碼:sentinel/sentinel
此處注意:sentinel控制臺采用懶加載,因此需要現有請求才能看到鏈路
API管理:可以根據請求路徑來設置API組
設置網關可以按照API分組設置,也可以設置app應用的總體限流
流控方式有快速失敗和勻速排隊兩種:
快速失敗可以設置Burst size,這是一個突刺個數,如果QPS設置為1,突刺數設置為10 ,當高并發請求時,首先能消耗的請求數是11個,超過11則拒絕,后續還是可通過一個請求。
勻速排隊可以設置等待時長毫秒,當高并發請求超過QPS時,進入等待狀態,如果超過等待時長則拒絕。
二、資源和規則配置說明
官網詳細說明:basic-api-resource-rule | Sentinel
1、資源定義
主流框架默認適配:系統當前使用的springcloud本身支持請求鏈路
另外一種方式通過注解引用的方式:@SentinelResource
注解需要增加一個依賴
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.8.6</version>
</dependency>
@RestController
@RequestMapping(value = "/sentinel")
@Api(value = "sentinel", tags = {"testController"})
public class TestController {@SentinelResource(value = "tesSentinel" , blockHandler = "myBlockHander")@PostMapping("/tesSentinel")public String tesSentinel() {return "1";}// 限流或者熔斷之后執行的方法public String myBlockHander(BlockException blockException){if(blockException instanceof FlowException){// 限流異常return "您被限流了";}else if(blockException instanceof DegradeException){// 熔斷異常return "您被熔斷了";}return "被限制了";}
}
2、規則定義
可以通過代碼設置規則,也可以通過控制臺設置規則
private void initFlowQpsRule() {List<FlowRule> rules=new ArrayList<>();System.out.println("限流初始化規則");// 定義一個限流規則FlowRule flowRule=new FlowRule();flowRule.setResource("tesSentinel"); // 資源名|必須參數flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 限流指標:QPS/線程數 |必須參數flowRule.setCount(1); // 限流數量(上一步 QPS 或線程數的值) |必須參數flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT); //調用關系限流策略【非必須設置】flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER); // 流控效果【非必須設置】 排隊等待flowRule.setMaxQueueingTimeMs(1000); // 等待超時時間flowRule.setClusterMode(false); // 是否集群限流【非必須設置,默認非集群】rules.add(flowRule);FlowRuleManager.loadRules(rules);}
提供的關聯流控和鏈路流控可以應用到一些特殊的業務場景的流控