侧边栏壁纸
博主头像
Epoch

Java开发、Python爬虫、微服务、分布式、前端

  • 累计撰写 94 篇文章
  • 累计创建 111 个标签
  • 累计收到 8 条评论

目 录CONTENT

文章目录

基于Sentinel Dashboard来实现流控配置

Epoch
2020-06-14 / 0 评论 / 0 点赞 / 339 阅读 / 751 字 / 正在检测是否收录...

目标: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)

9((C19%R@4A18@LJF_H8$BJ.png

成功界面如下
5EVKK.png

6.进入spring.application.name对应的菜单,访问"簇点链路",在该列表找到/dash这个REST接口的资源名称

EX8EPYX)R%YV)H8$O.png

7.针对这个资源名称,可以在左右边的操作栏单机"流控"按钮设置流控规则,如下:

ZXY2MMC@({0CKO1BRKSIDW1.png

为了演示效果,我就把这个单机阈值设置为1。

8.新增完成后,我们再次访问http://localhost:8080/dash接口,当QPS超过1时,就可以看到限流效果,并且获得如下输出

ER~ONHG.png

~}FJYRT%H$EX.png

下载链接:sentinel-sample1.rar

0

评论区