springcloud(11)sentinel安裝和基本使用
作為阿里版的hystrix,sentinel簡化了配置方式,提供了視覺化介面網站和便捷的配置方式,更加貼合實際的使用方式,各種優點使得sentinel成為服務降級熔斷流控等的最佳選擇。
1.安裝啟用
https://github.com/alibaba/Sentinel/releases
官網選擇合適的版本下載,其本體是jar檔案。
java -jar sentinel-dashboard-1.7.2.jar
由於下載的安裝包是jar,所以直接在cmd中啟動即可。
登入地址預設是localhost:8080,賬號密碼都是sentinel,第一次進入介面會是空白,且只有訪問過應用一遍,介面內才會出現對應的應用程式的資訊。
2.配置應用專案連線sentinel。
1)配置pom檔案
<dependencies> <!-- SpringCloud ailibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependencyView Code> <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.bai</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </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>
2)配置yml
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinal-service
cloud:
nacos:
discovery:
#Nacos服務註冊中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentin dashboard地址
dashboard: localhost:8080
# 預設8719埠,假如被佔用了會自動從8719埠+1進行掃描,直到找到未被佔用的 埠
port: 8719
datasource: #配置流控規則持久化
ds1:
nacos:
server-addr: localhost:8848
dataId: cloudalibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
management:
endpoints:
web:
exposure:
include: '*'
View Code
3)啟動類常規配置
@SpringBootApplication @EnableDiscoveryClient public class SentinelMain8401 { public static void main(String[] args) { SpringApplication.run(SentinelMain8401.class,args); } }View Code
4)controller測試
@RestController @Slf4j public class FlowLimitController { @GetMapping(value = "/testA") public String testA(){ return "******this is A"; } @GetMapping(value = "/testB") public String testB(){ log.info(Thread.currentThread().getName()+"\t"+"***testB"); return "******this is B"; } @GetMapping(value = "/testD") public String testD(){ /* try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } log.info("this is testD RT");*/ int a=10/0; log.info("this is testD 異常比例"); return "!!!!!TESTD"; }@GetMapping(value = "/testE") public String testE(){ int a=10/0; log.info("this is testE 異常數"); return "!!!!!TESTE"; } @GetMapping(value = "/hotkey") @SentinelResource(value = "hotkey",blockHandler = "hotkeyhandler") public String hotkey(@RequestParam(value = "p1",required = false)String p1, @RequestParam(value = "p2",required = false)String p2){ return ">>>>>>hotkey is ready"; } public String hotkeyhandler(String p1, String p2, BlockException exception){ return ".....hotkey is failed o(╥﹏╥)o"; } }View Code
3.常規使用
1)流控規則
點選主頁面的流控規則選擇新增規則
資源名,自己要訪問方法的地址
閾值型別,QPS指一秒內通過的訪問量,執行緒數則是啟用多少個執行緒來訪問同一個資源。
單機閾值,例如寫1,選QPS那麼一秒內多於1次訪問testA就會進行流量控制,強制報錯,執行緒同理。
流控模式,直接則是遇到閾值就直接失敗且預設,關聯則是可以在一個資源上關聯另一個資源,當資源訪問量超過閾值就會強制停止另一個資源進行控制。
流控效果,快速失敗是預設,warm up 是為了避免系統突然一段時間內進行了高併發的情況,對閾值進行限制,分為一段段的增長,最終達到閾值。排隊等待即訪問量過來根據規則一個個通過。
2)降級規則
區別於hystrix,sentinel通過網頁可以配置三種降級策略。
RT:建議去檢視官網,有明確的限定規則,即1秒內5次以上訪問,且平均訪問時間不得超過4900,如若超過以上限制就會進行降級處理。
異常比例:一定時間內錯誤達到一定比例就會觸發降級。
異常數:一定時間內錯誤達到一定數就會觸發降級。(以上配置需要嚴格遵守官網標準)
3.熱點規則
熱點規則是為了對方法中的某一個引數進行限流控制,可以精確到特定的引數,而且可以對指定的變數進行額外的控制。
熱點規則只支援QPS,需要配合sentinelresource註解
@SentinelResource(value = "hotkey",blockHandler = "hotkeyhandler")
blockhandler為熱點限定的方法出現錯誤的時候兜底的另一個方法。(暫定後續在寫)