SpringBoot4.0新特性-Resilience之并发限制

前一篇文章我们讲了失败重试,本篇我们继续来看另一个新特性 - @ConcurrencyLimit并发限制,简单的说,它可以限制方法的并发访问数。

使用入门

@ConcurrencyLimit@Retryable类似,可以加在单个方法上,设置方法级别的并发限制,也可以加在类上,给类中所有的通过代理调用的方法统一设置并发限制,使用起来非常简单,仅需2步:

  • 在启动类添加@EnableResilientMethods
java 复制代码
@EnableResilientMethods
@SpringBootApplication
public class Springboot4Application {}
  • 在业务类或者方法上添加@ConcurrencyLimit
java 复制代码
@ConcurrencyLimit(limit = 1)
@GetMapping(path = "/demo")
public String demo(){
    log.info("ConcurrencyLimitController demo");
    return "demo";
}
  • limit = 1:说明本方法同时只允许1个并发访问,如果有多余的并发请求会一直阻塞等待。
  • 加在controller的方法上等于实现了一个简易版的限流功能,可以对接口做一定程度的保护。

测试一下效果:

java 复制代码
public static void main(String[] args) throws Exception{
    System.out.println("start at " + LocalDateTime.now());
    int threadCount = 5;
    CountDownLatch start = new CountDownLatch(1);
    CountDownLatch stop = new CountDownLatch(threadCount);
    for(int i=0; i<threadCount; i++){
        new Thread(()->{
            try {
                start.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            String res = new RestTemplate().getForObject("http://localhost:8080/api/concurrency-limit/demo", String.class);
            System.out.println(res);
            stop.countDown();
        }).start();
    }
    start.countDown();
    stop.await();
    System.out.println("end at " + LocalDateTime.now());
}

我们使用5个线程并发访问前面的接口,因为接口只允许1个并发,因此总执行时长会超过5秒钟,日志如下:

plain 复制代码
2026-01-23T12:52:43  INFO 11868 --- [nio-exec-4] c.g.x.s.c.ConcurrencyLimitController     : ConcurrencyLimitController demo
2026-01-23T12:52:44  INFO 11868 --- [nio-exec-2] c.g.x.s.c.ConcurrencyLimitController     : ConcurrencyLimitController demo
2026-01-23T12:52:45  INFO 11868 --- [nio-exec-5] c.g.x.s.c.ConcurrencyLimitController     : ConcurrencyLimitController demo
2026-01-23T12:52:46  INFO 11868 --- [nio-exec-3] c.g.x.s.c.ConcurrencyLimitController     : ConcurrencyLimitController demo
2026-01-23T12:52:47  INFO 11868 --- [nio-exec-1] c.g.x.s.c.ConcurrencyLimitController     : ConcurrencyLimitController demo

实现原理

@ConcurrencyLimit背后是ConcurrencyThrottleSupport, 因此我们也可以用编程的方式手动的来做并发控制,比如我们可以利用现成的SyncTaskExecutor来实现与前面相同的并发限制功能:

java 复制代码
SyncTaskExecutor taskExecutor = new SyncTaskExecutor(){
    {
        // 设置并发数
        this.setConcurrencyLimit(1);
    }
};
@GetMapping(path = "/demo2")
public String demo2()throws Exception{
    return taskExecutor.execute(()->{
        log.info("ConcurrencyLimitController demo2");
        Thread.sleep(1000);
        return "demo";
    });
}

更多SpringBoot4.0的新特性请参考:SpringBoot4.0新特性

相关推荐
咩咩啃树皮1 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
鱟鲥鳚2 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
大模型码小白3 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司4 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地5 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
wuqingshun3141595 小时前
TCP超时重传机制是为了解决什么问题?
java
莫逸风7 小时前
【AgentScope 2.0】 0. 学习指南
java·llm·agent·agentscope
z123456789868 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
yaoxin5211238 小时前
470. Java 反射 - Member 接口与 AccessFlag
java·开发语言·python
做个文艺程序员9 小时前
Linux第24篇:Java应用监控体系搭建:Prometheus+Grafana可视化运维
java·grafana·prometheus