Spring Cloud Alibaba系列教程之使用Sentinel实现接口限流

最近管点闲事浪费了很多时间,感谢网友libinwalan的留言提醒。及时纠正路线,继续跟你们一块儿学习Spring Cloud Alibaba。java

Nacos做为注册中心和配置中心的基础教程,到这里先告一段落,后续与其余结合的内容等讲到的时候再一块儿拿出来讲,否则内容会有点跳跃。接下来咱们就来一块儿学习一下Spring Cloud Alibaba下的另一个重要组件:Sentinel。web

Sentinel是什么
Sentinel的官方标题是:分布式系统的流量防卫兵。从名字上来看,很容易就能猜到它是用来做服务稳定性保障的。对于服务稳定性保障组件,若是熟悉Spring Cloud的用户,第一反应应该就是Hystrix。可是比较惋惜的是Netflix已经宣布对Hystrix中止更新。那么,在将来咱们还有什么更好的选择呢?除了Spring Cloud官方推荐的resilience4j以外,目前Spring Cloud Alibaba下整合的Sentinel也是用户能够重点考察和选型的目标。spring

Sentinel的功能和细节比较多,一篇内容很难介绍完整。因此下面我会分多篇来一一介绍Sentinel的重要功能。本文就先从限流入手,说说如何把Sentinel整合到Spring Cloud应用中,以及如何使用Sentinel Dashboard来配置限流规则。经过这个简单的例子,先将这一套基础配置搭建起来。segmentfault

使用Sentinel实现接口限流
Sentinel的使用分为两部分:session

sentinel-dashboard:与hystrix-dashboard相似,可是它更为强大一些。除了与hystrix-dashboard同样提供实时监控以外,还提供了流控规则、熔断规则的在线维护等功能。
客户端整合:每一个微服务客户端都须要整合sentinel的客户端封装与配置,才能将监控信息上报给dashboard展现以及实时的更改限流或熔断规则等。
下面咱们就分两部分来看看,如何使用Sentienl来实现接口限流。app

部署Sentinel Dashboard
补充(2019-04-28):本文案例已升级Spring Cloud Alibaba 0.2.2,因为该版本中升级了Sentinel到1.5.2,因此对sentinel-dashboard作一次升级。可是sentinel-dashboard的1.5.2版本的打包文件没有提供下载,若是必定要该版本的话,须要本身编译。这里笔者尝试了一下直接使用1.6.0的sentinel-dashboard,暂时也没有发现什么问题,因此就以这个版本为例。curl

下载地址:sentinel-dashboard-1.6.0.jar
其余版本:Sentinel/releases
同以往的Spring Cloud教程同样,这里也不推荐你们跨版本使用,否则可能会出现各类各样的问题。分布式

经过命令启动:spring-boot

java -jar sentinel-dashboard-1.6.0.jar
sentinel-dashboard不像Nacos的服务端那样提供了外置的配置文件,比较容易修改参数。不过没关系,因为sentinel-dashboard是一个标准的spring boot应用,因此若是要自定义端口号等内容的话,能够经过在启动命令中增长参数来调整,好比:-Dserver.port=8888。微服务

默认状况下,sentinel-dashboard以8080端口启动,因此能够经过访问:localhost:8080来验证是否已经启动成功,若是一切顺利的话,能够看到以下页面:

file

注意:只有1.6.0及以上版本,才有这个简单的登陆页面。默认用户名和密码都是sentinel。对于用户登陆的相关配置能够在启动命令中增长下面的参数来进行配置:

-Dsentinel.dashboard.auth.username=sentinel: 用于指定控制台的登陆用户名为 sentinel;
-Dsentinel.dashboard.auth.password=123456: 用于指定控制台的登陆密码为 123456;若是省略这两个参数,默认用户和密码均为 sentinel
-Dserver.servlet.session.timeout=7200: 用于指定 Spring Boot 服务端 session 的过时时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟;
输入帐户密码登陆后,能够看到以下页面:

file

整合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这个服务以及接口调用的实时监控了。具体以下图所示:

file

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

file

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

file

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

阈值类型选择:QPS
单机阈值:2
综合起来的配置效果就是,该接口的限流策略是每秒最多容许2个请求进入。

点击新增按钮以后,能够看到以下界面:

file

其实就是左侧菜单中流控规则的界面,这里能够看到当前设置的全部限流策略。

验证限流规则
在完成了上面全部内容以后,咱们能够尝试一下快速的调用这个接口,看看是否会触发限流控制,好比:

$ curl localhost:8001/hello
didispace.com
$ curl localhost:8001/hello
didispace.com
$ curl localhost:8001/hello
Blocked by Sentinel (flow limiting)
能够看到,快速的调用两次/hello接口以后,第三次调用被限流了。
更多精彩文章,关注公众号【ToBeTopJavaer】,更有以下数万元精品vip资源免费等你来拿!!!
1574903758(1).jpg

1574903796(1).jpg

相关文章
相关标签/搜索