【Spring Cloud】Sentinel限流

控制台下载https://github.com/alibaba/Sentinel/releases

复制代码
# 控制台启动
java -Dserver.port=10888 -Dcsp.sentinel.dashboard.server=localhost:10888 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
  1. 引入依赖
xml 复制代码
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- 支持nacos作为数据源  -->
<dependency>
	<groupId>com.alibaba.csp</groupId>
	<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
  1. 使用nacos作为数据源,定义流控规则
yml 复制代码
spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:10888
      datasource:
        #流控规则
        flow: # 指定数据源名称 限流
          # 指定nacos数据源
          nacos:
            server-addr: http://127.0.0.1:8848
            # 指定配置文件
            dataId: ${spring.application.name}-flow-rules
            # 指定分组
            groupId: SENTINEL_GROUP
            # 指定配置文件规则类型
            rule-type: flow
            # 指定配置文件数据格式
            data-type: json
  1. 流控配置内容为

在 Sentinel 中,可以使用 JSON 格式的配置文件来配置流量控制规则。以下是一个简单的示例,演示了如何编写一个 JSON 格式的 Sentinel 流量控制规则配置文件:

限流

复制代码
${spring.application.name}-flow-rules
json
[
    {
        "resource":"findByPage",
        "limitApp":"default",
        "grade": 1 ,
        "count":1,
        "strategy": 0,
		"controlBehavior": 0
    }
]

对应一个com.alibaba.csp.sentinel.slots.block.flow.FlowRule的对象列表

resource:需要限流的资源名称,可以是路径名称,也可以是SentinelResource注解的value值

limitApp:对应的黑名单/白名单,不同 origin 用 , 分隔,如 appA,appB

grade:限流类型,这里使用 QPS(每秒查询率)。

count:每秒查询率的阈值,例如 100。

strategy:流控模式,此处直接使用直接方式

controlBehavior: 流控效果, 0 快速失败 1 Warm Up 2 排队等待

4.流控代码中使用

复制代码
@Service("goodsFishInfoService")
public class GoodsFishInfoServiceImpl extends ServiceImpl<GoodsFishInfoMapper, GoodsFishInfo> implements GoodsFishInfoService {

    @Resource
    private TokenUtil tokenUtil;

    @Resource
    GoodsFishInfoMapper goodsFishInfoMapper;

    @Resource
    FilterKeyWordMapper filterKeyWordMapper;

    @Override
    @SentinelResource(value = "findByPage", blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
    public ResultModelPage<GoodsFishInfo> findByPage(GoodsFishInfoVO goodsFishInfoVO) {
        PageHelper.startPage(goodsFishInfoVO.getPage(), goodsFishInfoVO.getPageSize());
        goodsFishInfoVO.setDelFlag(DelFlagEnum.ENABLE.getStatus());
        Page<GoodsFishInfo> page = (Page<GoodsFishInfo>) baseMapper.findByPageVo(goodsFishInfoVO);
        return ResultModelPage.resultModelTrue(Paging.of(page));
    }
}

public final class ExceptionUtil {
	/**
     * 此处返回值和参数必须要和注解对应的方法一致 且后面添加一个BlockException参数
     * 必须为 public static 方法
     * @param goodsFishInfoVO
     * @param ex
     * @return
     */
    public static ResultModelPage handleException(GoodsFishInfoVO goodsFishInfoVO, BlockException ex) {
        System.out.println("Oops: " + ex.getClass().getCanonicalName());
        return ResultModelPage.resultModelFalse("系统繁忙稍后再试");
    }
}

结果展示:

相关推荐
过客随尘11 小时前
网关扩容了,流量却还是打不过去?企业级 Spring Cloud Gateway 生产动态化部署实战
spring cloud·微服务·架构
堕落年代13 小时前
Ollama CPU 推理大提示词优化实测报告(细致化数据版)
java·后端·spring
cfm_291413 小时前
基于OAuth2.0实现微服务SSO单点登录
后端·spring·微服务·架构
攻城有术14 小时前
专项攻克-springcloud及其组件
后端·spring·spring cloud
叶总没有会15 小时前
5.1知识库概念进阶
java·人工智能·spring·阿里云·ai·原型模式
砍材农夫16 小时前
物联网实战|Spring Boot MQTT平台 |emqx broker实战
spring boot·spring·spring cloud·mybatis
2401_8949155319 小时前
GEO 定位优化源码搭建常见报错排查:数据库、伪静态、接口调试
java·数据库·网络协议·tcp/ip·spring·unity
篮框坏了20 小时前
配置改了不生效?我翻出 8 个坑,没一个报错
后端·spring cloud
sugar__salt21 小时前
Spring、Spring Boot 与配置文件核心知识点详解
java·spring boot·后端·spring·java-ee·maven·intellij-idea
wangjialelele21 小时前
Spring Cloud 全体系学习笔记(REST、Nacos、Feign、Gateway)
java·spring boot·笔记·学习·spring·spring cloud·gateway