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工程正常注册,测试成功,如图: