四.Sentinel
1.知識點
官網

主要特性
2.下載下傳
點選這裡下載下傳
運作
進入目錄,cmd
java -jar sentinel-dashboard-1.7.2.jar
使用者和密碼都是sentinel
3.建構項目(8401)
①建立module
②編寫pom檔案
<dependencies>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- sentinel-datasource-naso 持久化 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--引入自定義的api通用包 可以使用公用的entities-->
<dependency>
<groupId>com.hry.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--監控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
③修改yml檔案
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
#nacos服務注冊中心位址
server-addr: localhost:8848
sentinel:
transport:
#配置sentinel dashboard位址
dashboard: localhost:8080
#預設8719端口,端口被占用會自動從8719開始依次+1開始掃描,直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: "*"
④建立主啟動類
package com.hry.springcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelMain8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelMain8401.class, args);
}
}
⑤業務代碼
@RestController
public class FlowLimitController {
@GetMapping(value = "/testA")
public String testA(){
return "testA";
}
@GetMapping(value = "/testB")
public String testB(){
return "testB";
}
}
⑥将nacos配置改回去
略,叢集改回單機 端口号改回8848
⑦測試
cmd啟動啟動nacos8848,sentinel8080,啟動微服務8401
重新整理網頁發現啥也沒有
由于sentinel采用懶加載,是以我們要先通路一次8401。
然後重新整理,有了
重新整理幾次A和B
結論:sentinel8080正在監控8401
4.流控規則
①快速失敗
(1)QPS直接快速失敗
QPS = req/sec = 請求數/秒
設值每秒請求數為1
通路testA,1s點一次
1s内通路多次,被sentinel限流
(2)線程直接快速失敗
修改controller代碼
讓A休眠0.8s
@GetMapping(value = "/testA")
public String testA(){
try {
TimeUnit.MILLISECONDS.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "testA";
}
兩個一起通路testA
(3)關聯(以QPS為例)
将controller改回注釋掉sleep
建立流控
現在瘋狂通路B,導緻A限流
②預熱(warm up)
建立
剛開始5s會限流,之後正常
③排隊等待
編輯規則
5.降級規則(熔斷降級)
sentinel斷路器沒有半開狀态
①RT
(1)controller增加方法
@GetMapping(value = "/testC")
public String testC(){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "testC";
}
(2)新增降級規則
(3)測試
測試狂點F5
②異常比例
controller新增
@GetMapping(value = "/testD")
public String testD(){
int a = 1/0;
return "testD";
}
③異常數
controller
@GetMapping(value = "/testE")
public String testE(){
int a = 1/0;
return "testE";
}
6.熱點規則
@SentinelResource與@HystrixCommand相似
①修改controller
@GetMapping(value = "/testHotKey")
@SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey") //value任意,比如abc,唯一即可
public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
@RequestParam(value = "p2",required = false)String p2){
return "testHotKey";
}
public String deal_testHotKey(String p1, String p2, BlockException exception){
//sentinel 系統預設為Blocked by sentinel(flow limiting)
return "########################deal_testHotKey";
}
②配置熱點
注意這兩個與controller方法相比對 一個帶/ 一個不帶/
配置熱點,參數索引是指傳入的參數的下标從0開始 p1是0 p2是1
③測試
包含p1的1s超過1次通路都會flow limit而且執行了我們指定的兜底方法。如果不指定兜底方法前台會直接顯示error page
比如localhost:8401/testHotKey?p1=1&p2=2
比如localhost:8401/testHotKey?p1=1
而不包含p1的就可以正常通路
比如localhost:8401/testHotKey
比如localhost:8401/testHotKey?p2=2
④參數例外項目
(1)配置
編輯配置,點進階,可以設定一個或多個例外值
(2)測試
(3)注意
如果手動添加
運作時異常,兜底方法是不能處理的,這樣會前端會進入error page頁面,如何處理後面會說到。
7.系統規則
新增
測試
設定了系統規則,controller中所有請求都需要遵循規則。
比如按照如上圖所示設定,QPS,每秒超過1次就會限流。