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

相关推荐
RainbowSea2 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea2 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑6 小时前
Jpa使用union all
java·spring boot·后端
用户3721574261356 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊7 小时前
Java学习第22天 - 云原生与容器化
java
渣哥9 小时前
原来 Java 里线程安全集合有这么多种
java
间彧9 小时前
Spring Boot集成Spring Security完整指南
java
间彧10 小时前
Spring Secutiy基本原理及工作流程
java
Java水解11 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆13 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试