使用Sentinel实现接口限流(十一)
上一篇文章我们已经对 Sentinel 有个简单的了解,接下来我们将讲解 Sentinel的具体使用。
使用 Sentinel 实现接口限流
Sentinel的使用分为两部分:
- sentinel-dashboard: 与 hystrix-dashboard类似,但是它的功能更加强大。除了与 hystrix-dashboard 一样提供实时监控之外,还提供了流控规则、熔断规则的在线维护等功能。
- 客户端整合:每个微服务客户端都需要整合 sentinel 的客户端封装与配置,才能将监控信息上报给 dashboard 展示以及实时的更改限流或熔断规则等。
下面我们就分两部分来看,如果使用Sentinel来实现接口限流。
部署Sentinel Dashboard
- 下载地址:Sentinel/releases
这里我采用最新版:sentinel-dashboard-1.6.2.jar
命令行启动
java -jar sentinel-dashboard-1.6.2.jar
sentinel-dashboard 不像Nacos的服务端那样还提供了外置配置文件,比较容易修改参数。不过没关系,由于 sentinel-dashboard是一个标准的SpringBoot应用,所以如果需要自定义端口号等配置的话,可以通过在启动参数中增加参数来调整,比如:-Dserver.port=8888
。
默认情况下,sentinel-dashboard以8080端口启动,所以可以通过访问: http://localhost:8080 来验证是否启动成功,如果一切顺利,可以看到如下界面:
![](https://img.haomeiwen.com/i11464886/50cdae0e020ed931.png)
注意: 只要 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 分钟;
输入用户名密码登录后,会看下如下页面:
![](https://img.haomeiwen.com/i11464886/9c48972c4a0877bd.png)
整合Sentinel
第一步: 创建 alibaba-sentinel-rate-limiting
web应用,并在的pom.xml 中引入 Spring Cloud Alibaba的Sentinel模块依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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 应用中 application.properties
配置 sentinel dashboard 的访问地址:
注意:不要写在
bootstrap.properties
,不然无效
spring.application.name=alibaba-sentinel-rate-limiting
server.port=9005
# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:8080
第三步: 创建应用主类和rest接口:
@SpringBootApplication
public class SentinelRateLimitApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelRateLimitApplication.class, args);
}
}
@Slf4j
@RestController
public class TestController {
@GetMapping("/hello")
public String hello() {
return "hello sentinel dashboard";
}
}
第四步: 启动应用,然后通过 postman 访问:http://localhost:9005/hello 接口。
此时,在Sentinel Dashboard界面中就可以看到我们启动的这个服务以及接口调用的实时监控了。如下图所示:
![](https://img.haomeiwen.com/i11464886/c269625918005634.png)
配置限流规则
在完成了上面配置以后,我们在 Sentinel 控制台的 alibaba-sentinel-rate-limiting
服务下,单击 ** 簇点链路** 菜单,可以看到如下页面:
![](https://img.haomeiwen.com/i11464886/eb2d4cca7f52c474.png)
其中 ** /hello** 接口,就是我们实现并调用的测试接口。通过单击 流控 按钮,来为这个接口设置限流规则,比如:
![](https://img.haomeiwen.com/i11464886/1d4ddece4868d495.png)
这里做简单的配置:
- 阈值类型选择: QPS
- 单击阈值:2
综合起来的配置效果就是:该接口的限流测量是每秒最多允许2个请求进入。
单击 新增 按钮后,可以看到下图:
![](https://img.haomeiwen.com/i11464886/440dddec12bff482.png)
其实就是左侧菜单 ** 流控规则** 的界面,这里可看到当前设置的所有限流策略。
验证限流规则
在完成上面所有配置之后,我们可以尝试下快速的调用这个接口,看看是否会触发限流控制。
这里我们就不用什么压力测试工具,就简单的通过 curl
快速手动访问。可以看到调用 两次 /hello
接口后,第三次调用被限流(flow limiting
)了。
$ curl localhost:8001/hello
didispace.com
$ curl localhost:8001/hello
didispace.com
$ curl localhost:8001/hello
Blocked by Sentinel (flow limiting)