SpringBoot 接口并发限制(Semaphore)

简介: SpringBoot 接口并发限制(Semaphore)

可以使用 JMeter 辅助测试 https://blog.csdn.net/weixin_45014379/article/details/124190381

@RestController
@RequestMapping({"/Test"})
public class test { 
    Logger logger = LoggerFactory.getLogger(this.getClass());
 
    // 使用 Semaphore 并发限制3个 超过阻塞
    private final Semaphore permit = new Semaphore(3, true);
    @GetMapping(value = {"/download"})
    public String download(HttpServletRequest request) {        
        String result = "";
        try {
            // 获取令牌     
            logger.info("锁住 {}", Thread.currentThread().getName());
            permit.acquire();
            logger.info("下载 {}", Thread.currentThread().getName());
            Thread.sleep(5000);
            result = "Success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        } finally {
            // 释放令牌
            permit.release(); 
            logger.info("释放 {}", Thread.currentThread().getName());
        } 
    }
}
[2022-11-29 14:33:05.042] [http-nio-6201-exec-1] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-1
[2022-11-29 14:33:05.042] [http-nio-6201-exec-1] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-1
[2022-11-29 14:33:05.097] [http-nio-6201-exec-4] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-4
[2022-11-29 14:33:05.097] [http-nio-6201-exec-4] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-4
[2022-11-29 14:33:05.156] [http-nio-6201-exec-3] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-3
[2022-11-29 14:33:05.156] [http-nio-6201-exec-3] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-3
[2022-11-29 14:33:05.204] [http-nio-6201-exec-5] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-5
[2022-11-29 14:33:05.205] [http-nio-6201-exec-5] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-5
[2022-11-29 14:33:05.241] [http-nio-6201-exec-2] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-2
[2022-11-29 14:33:05.241] [http-nio-6201-exec-2] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-2
[2022-11-29 14:33:05.291] [http-nio-6201-exec-6] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-6
[2022-11-29 14:33:05.292] [http-nio-6201-exec-6] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-6
[2022-11-29 14:33:05.341] [http-nio-6201-exec-7] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-7
[2022-11-29 14:33:05.341] [http-nio-6201-exec-7] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-7
[2022-11-29 14:33:05.396] [http-nio-6201-exec-8] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-8
[2022-11-29 14:33:05.396] [http-nio-6201-exec-8] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-8
[2022-11-29 14:33:05.442] [http-nio-6201-exec-9] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-9
[2022-11-29 14:33:05.442] [http-nio-6201-exec-9] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-9
[2022-11-29 14:33:05.492] [http-nio-6201-exec-10] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-10
[2022-11-29 14:33:05.492] [http-nio-6201-exec-10] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-10
[2022-11-29 14:33:05.542] [http-nio-6201-exec-11] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-11
[2022-11-29 14:33:05.592] [http-nio-6201-exec-13] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-13
[2022-11-29 14:33:05.642] [http-nio-6201-exec-12] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-12
[2022-11-29 14:33:05.691] [http-nio-6201-exec-14] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-14
[2022-11-29 14:33:05.742] [http-nio-6201-exec-16] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-16
[2022-11-29 14:33:05.792] [http-nio-6201-exec-15] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-15
[2022-11-29 14:33:05.842] [http-nio-6201-exec-17] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-17
[2022-11-29 14:33:05.892] [http-nio-6201-exec-18] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-18
[2022-11-29 14:33:05.942] [http-nio-6201-exec-19] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-19
[2022-11-29 14:33:05.992] [http-nio-6201-exec-20] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-20
[2022-11-29 14:33:08.045] [http-nio-6201-exec-1] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-1
[2022-11-29 14:33:08.045] [http-nio-6201-exec-11] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-11
[2022-11-29 14:33:08.105] [http-nio-6201-exec-4] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-4
[2022-11-29 14:33:08.105] [http-nio-6201-exec-13] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-13
[2022-11-29 14:33:08.165] [http-nio-6201-exec-3] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-3
[2022-11-29 14:33:08.165] [http-nio-6201-exec-12] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-12
[2022-11-29 14:33:08.210] [http-nio-6201-exec-5] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-5
[2022-11-29 14:33:08.210] [http-nio-6201-exec-14] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-14
[2022-11-29 14:33:08.256] [http-nio-6201-exec-2] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-2
[2022-11-29 14:33:08.256] [http-nio-6201-exec-16] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-16
[2022-11-29 14:33:08.302] [http-nio-6201-exec-6] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-6
[2022-11-29 14:33:08.302] [http-nio-6201-exec-15] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-15
[2022-11-29 14:33:08.347] [http-nio-6201-exec-7] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-7
[2022-11-29 14:33:08.347] [http-nio-6201-exec-17] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-17
[2022-11-29 14:33:08.408] [http-nio-6201-exec-8] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-8
[2022-11-29 14:33:08.408] [http-nio-6201-exec-18] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-18
[2022-11-29 14:33:08.454] [http-nio-6201-exec-19] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-19
[2022-11-29 14:33:08.454] [http-nio-6201-exec-9] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-9
[2022-11-29 14:33:08.498] [http-nio-6201-exec-10] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-10
[2022-11-29 14:33:08.498] [http-nio-6201-exec-20] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-20
[2022-11-29 14:33:11.047] [http-nio-6201-exec-11] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-11
[2022-11-29 14:33:11.109] [http-nio-6201-exec-13] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-13
[2022-11-29 14:33:11.167] [http-nio-6201-exec-12] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-12
[2022-11-29 14:33:11.213] [http-nio-6201-exec-14] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-14
[2022-11-29 14:33:11.258] [http-nio-6201-exec-16] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-16
[2022-11-29 14:33:11.304] [http-nio-6201-exec-15] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-15
[2022-11-29 14:33:11.350] [http-nio-6201-exec-17] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-17
[2022-11-29 14:33:11.411] [http-nio-6201-exec-18] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-18
[2022-11-29 14:33:11.458] [http-nio-6201-exec-19] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-19
[2022-11-29 14:33:11.503] [http-nio-6201-exec-20] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-20
Disconnected from the target VM, address: '127.0.0.1:7367', transport: 'socket'
Process finished with exit code -1


目录
相关文章
|
1月前
|
并行计算 Java 数据处理
SpringBoot高级并发实践:自定义线程池与@Async异步调用深度解析
SpringBoot高级并发实践:自定义线程池与@Async异步调用深度解析
150 0
|
18天前
|
Java 开发者 Spring
精通SpringBoot:16个扩展接口精讲
【10月更文挑战第16天】 SpringBoot以其简化的配置和强大的扩展性,成为了Java开发者的首选框架之一。SpringBoot提供了一系列的扩展接口,使得开发者能够灵活地定制和扩展应用的行为。掌握这些扩展接口,能够帮助我们写出更加优雅和高效的代码。本文将详细介绍16个SpringBoot的扩展接口,并探讨它们在实际开发中的应用。
36 1
|
24天前
|
存储 安全 Java
|
24天前
|
存储 算法 安全
SpringBoot 接口加密解密实现
【10月更文挑战第18天】
|
22天前
|
监控 Java 开发者
掌握SpringBoot扩展接口:提升代码优雅度的16个技巧
【10月更文挑战第20天】 SpringBoot以其简化配置和快速开发而受到开发者的青睐。除了基本的CRUD操作外,SpringBoot还提供了丰富的扩展接口,让我们能够更灵活地定制和扩展应用。以下是16个常用的SpringBoot扩展接口,掌握它们将帮助你写出更加优雅的代码。
42 0
|
2月前
|
SQL JSON Java
springboot 如何编写增删改查后端接口,小白极速入门,附完整代码
本文为Spring Boot增删改查接口的小白入门教程,介绍了项目的构建、配置YML文件、代码编写(包括实体类、Mapper接口、Mapper.xml、Service和Controller)以及使用Postman进行接口测试的方法。同时提供了SQL代码和完整代码的下载链接。
springboot 如何编写增删改查后端接口,小白极速入门,附完整代码
|
2月前
|
存储 前端开发 Java
springboot文件上传和下载接口的简单思路
本文介绍了在Spring Boot中实现文件上传和下载接口的简单思路。文件上传通过`MultipartFile`对象获取前端传递的文件并存储,返回对外访问路径;文件下载通过文件的uuid名称读取文件,并通过流的方式输出,实现文件下载功能。
springboot文件上传和下载接口的简单思路
|
2月前
|
存储 数据采集 Java
Spring Boot 3 实现GZIP压缩优化:显著减少接口流量消耗!
在Web开发过程中,随着应用规模的扩大和用户量的增长,接口流量的消耗成为了一个不容忽视的问题。为了提升应用的性能和用户体验,减少带宽占用,数据压缩成为了一个重要的优化手段。在Spring Boot 3中,通过集成GZIP压缩技术,我们可以显著减少接口流量的消耗,从而优化应用的性能。本文将详细介绍如何在Spring Boot 3中实现GZIP压缩优化。
301 6
|
1月前
|
存储 NoSQL Java
Spring Boot项目中使用Redis实现接口幂等性的方案
通过上述方法,可以有效地在Spring Boot项目中利用Redis实现接口幂等性,既保证了接口操作的安全性,又提高了系统的可靠性。
37 0
|
2月前
|
Java 网络架构
springboot配合thymeleaf,调用接口不跳转页面只显示文本
springboot配合thymeleaf,调用接口不跳转页面只显示文本
121 0