赞
踩
Sentinel首先属于spring-cloud-alibaba下面的组件,因此,第一步要引入spring-cloud-alibaba依赖
文档:
https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
#Springboot/Cloud 应用端口
server:
port: 8082
#应用名称
spring:
application:
name: limiter
cloud:
sentinel:
transport:
dashboard: localhost:8080 #sentinel控制台服务端地址
HTTP 埋点:
Sentinel starter 默认为所有的 HTTP 服务提供了限流埋点,如果只想对 HTTP 服务进行限流,那么只需要引入依赖,无需修改代码。
package com.gblfy.distributedlimiter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SentinelLimiterController {
@GetMapping("/sentinel")
public String sentinel() {
return "sentinel";
}
}
第一次要先访问接口一次,sentinel控制台才会显示应用信息
http://localhost:8082/sentinel
刷新sentinel控制台显示
QPS每秒查询速率
由于咱们设置的每秒流控是1,每秒只能允许1个请求通过,测试1秒请求数量>1,反馈结果
得出结论当请求数量>1时,会返回Blocked by Sentinel (flow limiting)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。