Spring Cloud Alibaba基础教程:使用Sentinel实现接口限流

12次阅读

共计 3467 个字符,预计需要花费 9 分钟才能阅读完成。

最近管点闲事浪费了不少时间,感谢网友 libinwalan 的留言提醒。及时纠正路线,继续跟大家一起学习 Spring Cloud Alibaba。
Nacos 作为注册中心和配置中心的基础教程,到这里先告一段落,后续与其他结合的内容等讲到的时候再一起拿出来说,不然内容会有点跳跃。接下来我们就来一起学习一下 Spring Cloud Alibaba 下的另外一个重要组件:Sentinel。
Sentinel 是什么
Sentinel 的官方标题是:分布式系统的流量防卫兵。从名字上来看,很容易就能猜到它是用来作服务稳定性保障的。对于服务稳定性保障组件,如果熟悉 Spring Cloud 的用户,第一反应应该就是 Hystrix。但是比较可惜的是 Netflix 已经宣布对 Hystrix 停止更新。那么,在未来我们还有什么更好的选择呢?除了 Spring Cloud 官方推荐的 resilience4j 之外,目前 Spring Cloud Alibaba 下整合的 Sentinel 也是用户可以重点考察和选型的目标。
Sentinel 的功能和细节比较多,一篇内容很难介绍完整。所以下面我会分多篇来一一介绍 Sentinel 的重要功能。本文就先从限流入手,说说如何把 Sentinel 整合到 Spring Cloud 应用中,以及如何使用 Sentinel Dashboard 来配置限流规则。通过这个简单的例子,先将这一套基础配置搭建起来。
使用 Sentinel 实现接口限流
Sentinel 的使用分为两部分:

sentinel-dashboard:与 hystrix-dashboard 类似,但是它更为强大一些。除了与 hystrix-dashboard 一样提供实时监控之外,还提供了流控规则、熔断规则的在线维护等功能。
客户端整合:每个微服务客户端都需要整合 sentinel 的客户端封装与配置,才能将监控信息上报给 dashboard 展示以及实时的更改限流或熔断规则等。

下面我们就分两部分来看看,如何使用 Sentienl 来实现接口限流。
部署 Sentinel Dashboard
本文采用的 spring cloud alibaba 版本是 0.2.1,可以查看依赖发现当前版本使用的是 sentinel 1.4.0。为了顺利完成本文的内容,我们可以挑选同版本的 sentinel dashboard 来使用是最稳妥的。
下载地址:https://github.com/alibaba/Se… 其他版本:https://github.com/alibaba/Se…
同以往的 Spring Cloud 教程一样,这里也不推荐大家跨版本使用,不然可能会出现各种各样的问题。
通过命令启动:
java -jar sentinel-dashboard-1.4.0.jar
sentinel-dashboard 不像 Nacos 的服务端那样提供了外置的配置文件,比较容易修改参数。不过不要紧,由于 sentinel-dashboard 是一个标准的 spring boot 应用,所以如果要自定义端口号等内容的话,可以通过在启动命令中增加参数来调整,比如:-Dserver.port=8888。
默认情况下,sentinel-dashboard 以 8080 端口启动,所以可以通过访问:localhost:8080 来验证是否已经启动成功,如果一切顺利的话,可以看到如下页面:

整合 Sentinel
第一步:在 Spring Cloud 应用的 pom.xml 中引入 Spring Cloud Alibaba 的 Sentinel 模块:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
第二步:在 Spring Cloud 应用中通过 spring.cloud.sentinel.transport.dashboard 参数配置 sentinel dashboard 的访问地址,比如:
spring.application.name=alibaba-sentinel-rate-limiting
server.port=8001

# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:8080
第三步:创建应用主类,并提供一个 rest 接口,比如:
@SpringBootApplication
public class TestApplication {

public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}

@Slf4j
@RestController
static class TestController {

@GetMapping(“/hello”)
public String hello() {
return “didispace.com”;
}

}

}
第四步:启动应用,然后通过 postman 或者 curl 访问几下 localhost:8001/hello 接口。
$ curl localhost:8001/hello
didispace.com
此时,在上一节启动的 Sentinel Dashboard 中就可以当前我们启动的 alibaba-sentinel-rate-limiting 这个服务以及接口调用的实时监控了。具体如下图所示:

配置限流规则
在完成了上面的两节之后,我们在 alibaba-sentinel-rate-limiting 服务下,点击簇点链路菜单,可以看到如下界面:

其中 /hello 接口,就是我们上一节中实现并调用过的接口。通过点击流控按钮,来为该接口设置限流规则,比如:

这里做一个最简单的配置:

阈值类型选择:QPS
单机阈值:2

综合起来的配置效果就是,该接口的限流策略是每秒最多允许 2 个请求进入。
点击新增按钮之后,可以看到如下界面:

其实就是左侧菜单中流控规则的界面,这里可以看到当前设置的所有限流策略。
验证限流规则
在完成了上面所有内容之后,我们可以尝试一下快速的调用这个接口,看看是否会触发限流控制,比如:
$ curl localhost:8001/hello
didispace.com
$ curl localhost:8001/hello
didispace.com
$ curl localhost:8001/hello
Blocked by Sentinel (flow limiting)
可以看到,快速的调用两次 /hello 接口之后,第三次调用被限流了。
代码示例
本文介绍内容的客户端代码,示例读者可以通过查看下面仓库中的 alibaba-sentinel-rate-limiting 项目:

Github:https://github.com/dyc87112/SpringCloud-Learning/

Gitee:https://gitee.com/didispace/SpringCloud-Learning/

如果您对这些感兴趣,欢迎 star、follow、收藏、转发给予支持!
参考资料
下面是 Sentinel 的仓库地址与官方文档,读者也可以自己查阅文档学习:

Github
Sentinel 官方文档
Spring Cloud Alibaba Sentinel 文档

专题推荐

Spring Boot 基础教程
Spring Cloud 基础教程

正文完
 0