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

相关推荐
望获linux17 分钟前
【实时Linux实战系列】Linux 内核的实时组调度(Real-Time Group Scheduling)
java·linux·服务器·前端·数据库·人工智能·深度学习
Never_Satisfied20 分钟前
在 JavaScript 中,删除数组中内容为xxx的元素
java·前端·javascript
MC丶科28 分钟前
【SpringBoot常见报错与解决方案】端口被占用?Spring Boot 修改端口号的 3 种方法,第 3 种 90% 的人不知道!
java·linux·spring boot
怪兽201432 分钟前
Redis常见性能问题和解决方案
java·数据库·redis·面试
zz-zjx33 分钟前
JVM 内存结构与 GC 机制详解( 实战优化版)
java·jvm·tomcat
nvvas1 小时前
Android Studio JAVA开发按钮跳转功能
android·java·android studio
CV工程师丁Sir1 小时前
Rokid设备连接全解析:蓝牙与Wi-Fi通信源码深度剖析
java
zoyation1 小时前
多线程简介和在JAVA中应用
java·开发语言
rechol1 小时前
类与对象(中)笔记整理
java·javascript·笔记
周杰伦_Jay1 小时前
【Spring Boot从入门到精通】原理、实战与最佳实践
java·spring boot·后端