Springboot: 2.1.8.RELEASEjava
SpringCloud: Greenwich.SR2git
在介紹入門實戰以前,先來介紹一下Sentinel。Sentinel控制檯提供一個輕量級的開源控制檯,它提供機器發現以及健康狀況管理、監控(單機和集羣),規則管理和推送的功能。github
Sentinel控制檯主要功能:web
Sentinel控制檯部署有兩種方案:spring
直接使用官方編譯好的Release版本部署啓動,下載地址:https://github.com/alibaba/Sentinel/releases ,目前最新版本是v1.6.3,下載sentinel-dashboard-1.6.3.jar
,如圖:瀏覽器
可使用最新版本的源碼自行構建Sentinel控制檯,首先須要下載控制檯工程,下載路徑:https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard ,使用命令mvn clean package
將代碼打成jar包便可。session
在CentOS中使用以下命令啓動Sentinel控制檯工程:mvc
注意:啓動 Sentinel 控制檯須要 JDK 版本爲 1.8 及以上版本。app
nohup java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.3.jar >sentinel-dashboard.out 2>&1 &
-Dserver.port=8080
用於指定 Sentinel 控制檯端口爲 8080。啓動完成後,使用瀏覽器訪問:http://ip:8080/ ,這時會進入登陸頁面,從 Sentinel 1.6.0 起,Sentinel 控制檯引入基本的登陸功能,默認用戶名和密碼都是sentinel
,如圖:分佈式
Sentinel Dashboard能夠經過以下參數進行配置:
一樣也能夠直接在Spring的application.properties文件中進行配置。
Sentinel目前已支持Spring Cloud,須要引入spring-boot-starter-web
來觸發sentinel-starter中相關的自動配置。
工程依賴pom.xml以下:
代碼清單:Alibaba/sentinel-springcloud/pom.xml
***
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
spring-cloud-alibaba-dependencies
引入Spring Cloud Alibaba版本控制。spring-cloud-starter-alibaba-sentinel
引入Sentinel組件。
工程依賴pom.xml以下:
代碼清單:Alibaba/sentinel-springcloud/web_mvc/pom.xml
***
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
工程配置application.yml以下:
代碼清單:Alibaba/sentinel-springcloud/web_mvc/src/main/resources/application.yml
***
server: port: 8000 spring: application: name: web-mvc cloud: sentinel: transport: dashboard: 192.168.44.129:8080 port: 8719
建立測試接口HelloController.java以下:
代碼清單:Alibaba/sentinel-springcloud/web_mvc/src/main/java/com/springcloud/web_mvc/controller/HelloController.java
***
@RestController public class HelloController { @GetMapping(value = "/hello") @SentinelResource("hello") public String hello() { return "Hello Web MVC"; } }
@SentinelResource
註解用來標識資源是否被限流、降級。上述例子上該註解的屬性 'hello' 表示資源名。該註解還有一些其餘更精細化的配置,好比忽略某些異常的配置、默認降級函數等等,具體可見以下說明:
注:1.6.0 以前的版本 fallback 函數只針對降級異常(DegradeException)進行處理,不能針對業務異常進行處理。
特別地,若 blockHandler 和 fallback 都進行了配置,則被限流降級而拋出 BlockException 時只會進入 blockHandler 處理邏輯。若未配置 blockHandler、fallback 和 defaultFallback,則被限流降級時會將 BlockException 直接拋出。
啓動子工程web_mvc,啓動成功後打開瀏覽器訪問:http://localhost:8000/hello ,能夠看到頁面正常顯示Hello Web MVC
,屢次刷新後打開Sentinel Dashboard,在Sentinel控制檯上已經能夠看到咱們的web-mvc應用了,如圖:
Sentinel目前已經支持WebFlux,須要配合spring-boot-starter-webflux
依賴觸發 sentinel-starter中WebFlux相關的自動化配置。具體接入方式以下:
工程依賴pom.xml以下:
代碼清單:Alibaba/sentinel-springcloud/web_flux/pom.xml
***
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
spring-boot-starter-webflux
引入WebFlux的相關依賴,Sentinel的依賴已在父工程引入。
代碼清單:Alibaba/sentinel-springcloud/web_flux/src/main/resources/application.yml
server: port: 9000 spring: application: name: web-flux cloud: sentinel: transport: dashboard: 192.168.44.129:8080 port: 8720
代碼清單:Alibaba/sentinel-springcloud/web_flux/src/main/java/com/springcloud/web_flux/controller/HelloController.java
***
@RestController public class HelloController { @GetMapping("/hello") @SentinelResource("hello") public Mono<String> mono() { return Mono.just("Hello Web Flux") .transform(new SentinelReactorTransformer<>("resourceName")); } }
啓動子工程web_flux,打開瀏覽器訪問:http://localhost:9000/hello ,頁面正常顯示Hello Web Flux
,屢次刷新頁面後打開Sentinel控制檯,能夠看到web_flux工程正常註冊,測試成功,如圖: