SpringCloud: sentinel链路限流

一、配置文件要增加

复制代码
      spring.cloud.sentinel.webContextUnify: false

二、在要限流的业务方法上使用@SentinelResource注解

复制代码
package cn.edu.tju.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @SentinelResource(value = "getUser", blockHandler = "myBlockingHandler")
    public String getUser(){
        return " a user...";
    }

    public String myBlockingHandler(BlockException ex){
        System.out.println("链路限流");
        return "链路限流啦";
    }
}

三、定义接口调用业务方法

复制代码
package cn.edu.tju.controller;

import cn.edu.tju.service.UserService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUser1")
    public String getUser1(){
        return userService.getUser();
    }



    @RequestMapping("/getUser2")
    public String getUser2(){
        return userService.getUser();
    }
}

四、在sentinel控制台设置链路限流规则:

五、启动应用,快速刷新接口/getUser1

相关推荐
用户3126874877203 小时前
ConcurrentHashMap 怎么保证线程安全?从分段锁到 CAS+synchronized
java
vipxieliang3 小时前
ValidX vs Apache Commons Validator:功能与性能对比
java·spring boot
SimonKing3 小时前
升级Spring Boot 4后,从 Jackson 2 到 3,到底有哪些变化
java·后端·程序员
吃饱了得干活4 小时前
一篇讲清楚Spring Boot:自动装配、启动器、过滤器、拦截器、设计模式
java·spring boot·后端
lhldsg5 小时前
AI零售系统实战指南:从架构设计到落地部署全解析
java·人工智能·小程序·uni-app·零售
Interview Aid1126 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
mldong6 小时前
换了工作流引擎,前端一行代码没改
java·架构
微尘寒风13 小时前
【Git】的安装和使用
java·git
y = xⁿ14 小时前
DeepSeek Harness 学习日记:关于Agent接口,工具调用的底层实现
android·java·学习
侧耳倾听11114 小时前
jwt使用简介
java·jwt