五小步快速集成使用sentinel限流

1、环境和资源准备

五小步快速集成使用sentinel限流

文章插图
sentinel支持许多流控方式,比如:单机限流、熔断降级、集群限流、系统保护规则、黑白名单授权等 。
本文介绍如何快速集成使用sentinel,文中以单机限流为例 , 使用代码而非控制台配置的方式限流 。
  • sentinel官网地址:https://sentinelguard.io/zh-cn/index.html
  • Github地址:https://github.com/alibaba/Sentinel
  • 本文采用的版本是1.8.0,下载地址:https://github.com/alibaba/Sentinel/releases/tag/v1.8.0
  • sentinel-dashboard下载地址:https://github.com/alibaba/Sentinel/releases/download/v1.8.0/sentinel-dashboard-1.8.0.jar
  • 本文使用的项目地址:https://github.com/yclxiao/spring-sentinel-demo,代码中有一部分使用的是官方demo 。
2、启动sentinel-dashboard从上文地址下载sentinel-dashboard,然后执行命令启动:JAVA -jar sentinel-dashboard-1.8.0.jar
启动完毕后,通过http://localhost:8080/#/dashboard访问dashboard , 出现如下界面:
五小步快速集成使用sentinel限流

文章插图
3、项目集成sentinel项目中集成sentinel分如下5步 。
3.1、引入pom<!-- 这是sentinel的核心依赖 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.8.0</version></dependency><!-- 这是将自己项目和sentinel-dashboard打通的依赖 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.0</version></dependency><!-- 这是使用sentinel对限流资源进行AOP --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-annotation-aspectj</artifactId><version>1.8.0</version></dependency>3.2、增加sentinel-aop@Configurationpublic class AopConfiguration {@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();}}3.3、增加sentinel.properties配置在Application.properties同级目录下,增加sentinel.properties文件 , 配置内容如下:
# 集成到sentinel的项目名称project.name=spring-sentinel-demo# 对应的sentinel-dashboard地址csp.sentinel.dashboard.server=localhost:8080
五小步快速集成使用sentinel限流

文章插图
同时需要加载sentinel.properties配置,有两种加载方式,选择一种即可 , 如下:
五小步快速集成使用sentinel限流

文章插图
3.4、设置需要被限流的资源给需要被限流的资源打上注解@SentinelResource,使用方式如下 。
  • 默认情况下,超出配置的流控阈值后,直接抛出 FlowException(BlockException) 异常,可以使用blockHandler自定义 。
  • fallback用于配置熔断降级的方法,当发生慢调用、异常数、异常比例数,会调用fallback方法 。
  • 可以针对部分异常情况做忽略处理 , 不再触发熔断降级 。
@Servicepublic class TestServiceImpl implements top.mangod.springsentineldemo.service.TestService {@Override@SentinelResource(value = https://www.isolves.com/it/cxkf/bk/2023-09-19/"test", blockHandler = "handleException", blockHandlerClass = {top.mangod.springsentineldemo.service.ExceptionUtil.class})public void test() {System.out.println("Test");}@Override@SentinelResource(value = "hello", fallback = "helloFallback")public String hello(long s) {if (s < 0) {throw new IllegalArgumentException("invalid arg");}return String.format("Hello at %d", s);}@Override@SentinelResource(value = "helloAnother", defaultFallback = "defaultFallback",exceptionsToIgnore = {IllegalStateException.class})public String helloAnother(String name) {if (name == null || "bad".equals(name)) {throw new IllegalArgumentException("oops");}if ("foo".equals(name)) {throw new IllegalStateException("oops");}return "Hello, " + name;}public String helloFallback(long s, Throwable ex) {// Do some log here.ex.printStackTrace();return "Oops, error occurred at " + s;}public String defaultFallback() {System.out.println("Go to default fallback");return "default_fallback";}}3.5、指定和加载流控规则文中我使用代码方式制定流控规则 , 在控制台中也可以直接配置流控规则,为什么不使用控制台方式呢?
如果是类似云原生的部署环境,比如:将spring应用打成Docker镜像,然后在部署到Kube.NETes中,部署之后Pod地址是会变化 。


推荐阅读