首页 > 其他分享 >Spring Cloud Alibaba系列(一)限流与防护组件Sentinel的简单使用

Spring Cloud Alibaba系列(一)限流与防护组件Sentinel的简单使用

时间:2023-03-20 23:02:18浏览次数:42  
标签:Spring boot Alibaba 限流 spring Sentinel 控制台 cloud

Sentinel是Spring Cloud Alibaba体系的安全防护组件,我们可以使用它以“非业务侵入”方式实现限流,熔断,服务降级需求。

一. 下载并启动Sentinel控制台

  从GitHub网址https://github.com/alibaba/Sentinel/releases/tag/1.8.6中下载控制台jar,用以下命令启动:

 H:\> java  '-Dserver.port=8090' '-jar' sentinel-dashboard-1.8.6.jar

  启动成功的界面如下:

 

 

 在浏览器输入验证:http://localhost:8090/ 

 出现登录界面

 

 

 输入用户名和密码,用户名和密码都是sentinel,登录成功

 

 

 二. 在项目中引入sentinel依赖包

在Spring Boot项目中引入以下依赖:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在Spring Boot项目的resource目录的application.yml中添加如下配置:

spring:
  cloud:
    sentinel:
      transport:
        # 与sentinel控制台交互的端口
        port: 9000
        # sentinel控制台的工作地址和端口
        dashboard: localhost:8090

在控制器类中新增@SentinelResource注解,通过注解value参数设置该方法在Sentinel控制台里的标识符,其中@SentinelResource(value = "hello")就是我们需要新增的注解

@RestController
public class Demo {
    @RequestMapping("/hello")
    @SentinelResource(value = "hello")
  public String hello() {
return "hello";
}
}

三. 在Sentinel控制台里设置限流参数

由于Sentinel采用的是懒加载机制,首先需要先访问对应接口才能在控制台里见到对应项目,才能进行接下来的设置,在浏览器中访问接口,访问接口成功后随后访问Sentinel控制台,已经可以看到对应项目

 

 

 单击左侧的【流控规则】菜单,点击【新增流控规则】

 

 

 输入资源名和单击阈值

 

 

 在浏览器中快速刷新验证流控是否生效,由于我们刚刚设置“每秒限流1次”规则,多次快速刷新将突破该限制,出现以下默认错误界面,确认限流成功

 

同时在开发工具控制台可以看到应用打印了如下日志,也说明限流成功

 

标签:Spring,boot,Alibaba,限流,spring,Sentinel,控制台,cloud
From: https://www.cnblogs.com/fxhui/p/17238253.html

相关文章

  • 16_SpringBoot_开发者工具_了解
    ​ 使用开发者工具包不需要重启。监听内容改变。1导入依赖 <dependency><groupId>org.springframework.boot</groupId><artifact......
  • 16_SpringBoot_开发者工具_了解
    ​ 使用开发者工具包不需要重启。监听内容改变。1导入依赖 <dependency><groupId>org.springframework.boot</groupId><artifact......
  • SpringBoot 统一功能处理
     SpringBoot统一功能处理前言一、用户登录权限效验1.1最初的用户登录验证1.2SpringAOP用户统一登录验证的问题1.3Spring拦截器1.3.1准备工作1.3.2......
  • SpringMVC-lesson01-servlet复习2-2023-03-20
    1、欢迎页设置在web文件夹下新建form.jsp:input的类型是text,名字是:method。一旦输入add或delete提交,就会到HelloServlet进行跳转<%--CreatedbyIntelliJI......
  • springboot防止XSS攻击和sql注入
     文章目录1.XSS跨站脚本攻击①:XSS漏洞介绍②:XSS漏洞分类③:防护建议2.SQL注入攻击①:SQL注入漏洞介绍②:防护建议3.SpringBoot中如何防止XSS攻击和......
  • 12_SpringBoot_整合Thymeleaf_掌握
    ​   Thymeleaf的主要目标是将优雅的自然模板带到开发工作流程中,并将HTML在浏览器中正确显示,并且可以作为静态原型,让开发团队能更容易地协作。Thymeleaf能够处理HTML......
  • 12_SpringBoot_整合Thymeleaf_掌握
    ​   Thymeleaf的主要目标是将优雅的自然模板带到开发工作流程中,并将HTML在浏览器中正确显示,并且可以作为静态原型,让开发团队能更容易地协作。Thymeleaf能够处理HTML......
  • 15_SpringBoot_模板引擎总结_了解
    jsp优点:1、功能强大,可以写java代码2、支持jsp标签(jsptag)3、支持表达式语言(el)4、官方标准,用户群广,丰富的第三方jsp标签库缺点:性能问题。不支持前后端分离freemarkerFreeMa......
  • 15_SpringBoot_模板引擎总结_了解
    jsp优点:1、功能强大,可以写java代码2、支持jsp标签(jsptag)3、支持表达式语言(el)4、官方标准,用户群广,丰富的第三方jsp标签库缺点:性能问题。不支持前后端分离freemarkerFreeMa......
  • spring security中@PreAuthorize、@PostAuthorize、@PreFilter和@PostFilter四者的区
    springsecurity中@PreAuthorize、@PostAuthorize、@PreFilter和@PostFilter四者的区别https://cxhzt.blog.csdn.net/article/details/80873268?spm=1001.2101.3001.6650.......