目标:Sentinel的基本应用
工具:IDEA–2020.1、Sentinel Maven Spring Boot
学习目标:学习基于Sentinel Dashboard来实现流控配置
本次学习的工程下载链接放到文本最后面
1.创建一个springboot工程导入springcloud相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
还有相关的包管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.启动Sentinel Dashboard
3.在application.yml中增加如下配置
spring:
application:
name: springboot-sentinel-sample
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:7777
spring.cloud.sentinel.transport.dashboard指向的是Sentinel Dashboard的服务器地址(我们这里用的是本地地址),可以实现对留空数据的监控和流控规则的分发。
4.提供一个REST接口,代码如下
@RestController
public class DashboardController {
@GetMapping("/dash")
private String dash(){
return "Hello dash";
}
}
我们先不对他进行资源处理,我们到时候直接去Sentinel Dashboard中进行配置。
5.启动服务,访问http://localhost:8080/dash ,由于没有进行配置流控规则,所以不存在限流行为
此时,我们springcloud集成sentinel的配置完成了,我们进入Sentinel Dashboard这个来实现流控规则的处理吧!
6.访问http://localhost:7777/进入Sentinel Dashboard(账号密码都是sentinel)
成功界面如下
6.进入spring.application.name对应的菜单,访问"簇点链路",在该列表找到/dash这个REST接口的资源名称
7.针对这个资源名称,可以在左右边的操作栏单机"流控"按钮设置流控规则,如下:
为了演示效果,我就把这个单机阈值设置为1。
8.新增完成后,我们再次访问http://localhost:8080/dash接口,当QPS超过1时,就可以看到限流效果,并且获得如下输出
下载链接:sentinel-sample1.rar
评论区