链接:https://pan.baidu.com/s/1ma9TX38TabEbGdOyDSobvQ?pwd=s9zu
- Sentinel控制台启动
java -Dserver.port=8888 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar
- Sentinel控制台访问
http://localhost:8888 端口8888为启动命令设置的,默认用户名和密码均为sentinel - 添加父项目pom依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> <springboot.version>2.3.2.RELEASE</springboot.version> <springcloudalibaba.version>2.2.4.RELEASE</springcloudalibaba.version> </properties> <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>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springboot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${springcloudalibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- 添加子项目pom依赖
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-transport-simple-http</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-parameter-flow-control</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> </dependencies>
- VM options配置sentinel控制台
-Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-helloWorld 端口号8888要和Sentinel控制台启动时设置的端口号一致,Dproject.name为该项目在sentinel控
制台显示的名称 - 注解方式定义资源
/** * 添加配置类 */ @Configuration public class SentinelAspectConfiguration { @Bean public SentinelResourceAspect sentinelResourceAspect() { return new SentinelResourceAspect(); } }
/** * 注解方式定义资源 * @SentinelResource value 资源名称 * @SentinelResource blockHandler 调用被限流/降级/系统保护的时候调用的方法 * @return */ @SentinelResource(value = "helloWorld",blockHandler = "blockHandlerForHelloWorld") @RequestMapping("helloWorld") public String helloWorld(){ return "Sentinel 大爷你好!by 注解方式@SentinelResource"+System.currentTimeMillis(); } /** * 原方法调用被限流/降级/系统保护的时候调用 * @param ex * @return */ public String blockHandlerForHelloWorld(BlockException ex) { ex.printStackTrace(); return "系统繁忙,请稍后!"; }
- 测试流控资源helloWorld
多次快速刷新:http://localhost:8090/helloWorld,由于helloWorld还未在Sentinel控制台定义,所以请求不会触发流控规则(注意:请求过流控资源后Sentinel控制台才会显示出项目名称) - Sentinel控制台添加流控资源helloWorld后再次测试
多次快速刷新:http://localhost:8090/helloWorld,触发流控规则后进入blockHandlerForHelloWorld方法,返回报错提示 - 流控规则分析
- 降级规则分析
- Sentinel整合Nacos
Sentinel定义的限流规则默认存储在内存中,Sentinel控制台重启后定义的限流规则将全部丢失,需要和Nacos进行整合实现规则数据的持久化(Nacos),注意:项目启动后,Nacos通过Json方式添加或者修改的流控规则,会被实时同步到Sentinel控制台,Sentinel原有的流控规则被覆盖,Sentinel控制台添加的流控规则不会被反向加载到Nacos,因此流控规则需要在Nacos添加,Sentinel控制台仅用来观测