演示jvm锁存在的问题

ops/2024/10/15 20:20:21/

文章目录

  • 1、AlbumInfoApiController --》testLock()
  • 2、redis添加键值对
  • 3、AlbumInfoServiceImpl --》testLock() 没有加锁
  • 4、使用ab工具测试
    • 4.1、安装 ab 工具
    • 4.2、查看 redis 中的值
  • 5、添加本地锁 synchronized
  • 6、集群情况下问题演示

jvm锁:synchronized lock 只能锁住一个jvm内的资源

1、AlbumInfoApiController --》testLock()

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class AlbumInfoApiController {@GetMapping("test/lock")public Result testLock() {this.albumInfoService.testLock();return Result.ok("测试分布式锁案例");}}

2、redis添加键值对

在这里插入图片描述

3、AlbumInfoServiceImpl --》testLock() 没有加锁

    @Overridepublic void testLock(){Object numObj = this.redisTemplate.opsForValue().get("num");if (numObj == null) {this.redisTemplate.opsForValue().set("num", 1);return;}Integer num = Integer.parseInt(numObj.toString());this.redisTemplate.opsForValue().set("num", ++num);}

在这里插入图片描述

4、使用ab工具测试

ab 工具是 Apache Bench阿帕奇基准测试工具),一个由 Apache HTTP Server 项目提供的用于测试 web 服务器性能的命令行工具。ab 主要用于生成 HTTP 请求并发送到 web 服务器,以此来评估服务器的性能和响应能力。它是一个简单但功能强大的工具,广泛用于压力测试和性能测试场景。

之前在redis中,玩过ab测试工具:httpd-tools(yum install -y httpd-tools)

4.1、安装 ab 工具

[root@localhost ~]# yum install -y httpd-tools
已加载插件:fastestmirror, langpacks
[root@localhost ~]# ab
ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:-n requests     Number of requests to perform-c concurrency  Number of multiple requests to make at a time-t timelimit    Seconds to max. to spend on benchmarkingThis implies -n 50000-s timeout      Seconds to max. wait for each responseDefault is 30 seconds-b windowsize   Size of TCP send/receive buffer, in bytes-B address      Address to bind to when making outgoing connections-p postfile     File containing data to POST. Remember also to set -T-u putfile      File containing data to PUT. Remember also to set -T-T content-type Content-type header to use for POST/PUT data, eg.'application/x-www-form-urlencoded'Default is 'text/plain'-v verbosity    How much troubleshooting info to print-w              Print out results in HTML tables-i              Use HEAD instead of GET-x attributes   String to insert as table attributes-y attributes   String to insert as tr attributes-z attributes   String to insert as td or th attributes-C attribute    Add cookie, eg. 'Apache=1234'. (repeatable)-H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'Inserted after all normal header lines. (repeatable)-A attribute    Add Basic WWW Authentication, the attributesare a colon separated username and password.-P attribute    Add Basic Proxy Authentication, the attributesare a colon separated username and password.-X proxy:port   Proxyserver and port number to use-V              Print version number and exit-k              Use HTTP KeepAlive feature-d              Do not show percentiles served table.-S              Do not show confidence estimators and warnings.-q              Do not show progress when doing more than 150 requests-g filename     Output collected data to gnuplot format file.-e filename     Output CSV file with percentages served-r              Don't exit on socket receive errors.-h              Display usage information (this message)-Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)-f protocol     Specify SSL/TLS protocol(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
ab  -n(一次发送的请求数)  -c(请求的并发数) 访问路径

在这里插入图片描述

[root@localhost ~]# ping 192.168.74.1
PING 192.168.74.1 (192.168.74.1) 56(84) bytes of data.
64 bytes from 192.168.74.1: icmp_seq=1 ttl=64 time=0.582 ms
64 bytes from 192.168.74.1: icmp_seq=2 ttl=64 time=0.427 ms
64 bytes from 192.168.74.1: icmp_seq=3 ttl=64 time=0.342 ms
64 bytes from 192.168.74.1: icmp_seq=4 ttl=64 time=0.370 ms
64 bytes from 192.168.74.1: icmp_seq=5 ttl=64 time=0.426 ms
64 bytes from 192.168.74.1: icmp_seq=6 ttl=64 time=0.548 ms
64 bytes from 192.168.74.1: icmp_seq=7 ttl=64 time=0.791 ms

在这里插入图片描述
在这里插入图片描述

redis中的值重新改为0。

[root@localhost ~]# ab -n 5000 -c 100 http://192.168.74.1:8500/api/album/albumInfo/test/lock
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.74.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requestsServer Software:        
Server Hostname:        192.168.74.1
Server Port:            8500Document Path:          /api/album/albumInfo/test/lock
Document Length:        76 bytesConcurrency Level:      100
Time taken for tests:   5.374 seconds
Complete requests:      5000
Failed requests:        593(Connect: 0, Receive: 0, Length: 593, Exceptions: 0)
Write errors:           0
Total transferred:      2352965 bytes
HTML transferred:       382965 bytes
Requests per second:    930.38 [#/sec] (mean)
Time per request:       107.483 [ms] (mean)
Time per request:       1.075 [ms] (mean, across all concurrent requests)
Transfer rate:          427.57 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        1   19  19.7     17     404
Processing:    20   87  65.9     73     542
Waiting:       15   82  65.0     68     538
Total:         36  106  70.3     90     581Percentage of the requests served within a certain time (ms)50%     9066%    10075%    10880%    11590%    13895%    17698%    33099%    534100%    581 (longest request)

4.2、查看 redis 中的值

在这里插入图片描述

5、添加本地锁 synchronized

    @Overridepublic synchronized void testLock(){Object numObj = this.redisTemplate.opsForValue().get("num");if (numObj == null) {this.redisTemplate.opsForValue().set("num", 1);return;}Integer num = Integer.parseInt(numObj.toString());this.redisTemplate.opsForValue().set("num", ++num);}

在这里插入图片描述
redis中的值重新改为0。

重启之后,使用ab工具压力测试:5000次请求,并发100。

[root@localhost ~]# ab -n 5000 -c 100 http://192.168.74.1:8500/api/album/albumInfo/test/lock
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.74.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requestsServer Software:        
Server Hostname:        192.168.74.1
Server Port:            8500Document Path:          /api/album/albumInfo/test/lock
Document Length:        76 bytesConcurrency Level:      100
Time taken for tests:   23.247 seconds
Complete requests:      5000
Failed requests:        746(Connect: 0, Receive: 0, Length: 746, Exceptions: 0)
Write errors:           0
Total transferred:      2353730 bytes
HTML transferred:       383730 bytes
Requests per second:    215.08 [#/sec] (mean)
Time per request:       464.933 [ms] (mean)
Time per request:       4.649 [ms] (mean, across all concurrent requests)
Transfer rate:          98.88 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    1   4.1      1     196
Processing:     5  446 365.2    414    2722
Waiting:        4  446 365.2    414    2722
Total:          5  447 365.4    415    2734Percentage of the requests served within a certain time (ms)50%    41566%    54875%    62480%    66490%    75095%    80098%   181999%   2408100%   2734 (longest request)

测试完成后,查看redis中的值:
在这里插入图片描述
完美!是否真的完美?
接下来再看集群情况下,会怎样?

6、集群情况下问题演示

启动多个运行实例:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
redis中的值重新改为0。

[root@localhost ~]# ab -n 5000 -c 100 http://192.168.74.1:8500/api/album/albumInfo/test/lock
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.74.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requestsServer Software:        
Server Hostname:        192.168.74.1
Server Port:            8500Document Path:          /api/album/albumInfo/test/lock
Document Length:        76 bytesConcurrency Level:      100
Time taken for tests:   8.714 seconds
Complete requests:      5000
Failed requests:        686(Connect: 0, Receive: 0, Length: 686, Exceptions: 0)
Write errors:           0
Total transferred:      2353430 bytes
HTML transferred:       383430 bytes
Requests per second:    573.79 [#/sec] (mean)
Time per request:       174.280 [ms] (mean)
Time per request:       1.743 [ms] (mean, across all concurrent requests)
Transfer rate:          263.74 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    2   3.2      1      54
Processing:     6  170 106.2    164     565
Waiting:        6  170 106.2    163     565
Total:          7  172 106.8    165     568Percentage of the requests served within a certain time (ms)50%    16566%    21875%    24980%    26990%    31795%    35198%    39899%    436100%    568 (longest request)

由于这三个运行实例的服务名都是 service-album,而网关配置的就是通过服务名负载均衡,我们只要通过网关访问,网关就会给我们做负载均衡了。

再次执行之前的压力测试,查看redis中的值:
在这里插入图片描述
集群情况下又出问题了!!!

以上测试,可以发现:

​ 本地锁只能锁住同一工程内的资源,在分布式系统里面都存在局限性。

此时需要分布式锁。。


http://www.ppmy.cn/ops/114115.html

相关文章

SOCKS5、HTTP 代理IP协议有何区别?

在网络通信领域&#xff0c;代理服务器的选择对于数据安全和传输效率至关重要。SOCKS5代理和HTTP代理作为两种常用的代理类型&#xff0c;各自具有独特的特点和适用场景。本文将深入探讨SOCKS5代理与HTTP代理的区别、特性及应用场景&#xff0c;为用户提供选择指南。 一、SOCK…

Mybatis Plus分页查询返回total为0问题

Mybatis Plus分页查询返回total为0问题 一日&#xff0c;乌云密布&#xff0c;本人看着mybatis plus的官方文档&#xff0c;随手写了个分页查询&#xff0c;如下 Page<Question> questionPage questionService.page(new Page<>(current, size),questionService.g…

8--SpringBoot原理分析、注解-详解(面试高频提问点)

目录 SpringBootApplication 1.元注解 --->元注解 Target Retention Documented Inherited 2.SpringBootConfiguration Configuration Component Indexed 3.EnableAutoConfiguration&#xff08;自动配置核心注解&#xff09; 4.ComponentScan Conditional Co…

VMware虚拟机因磁盘空间不足黑屏无法登录

在虚拟机里存储了一些文件之后&#xff0c;再打开发现进不去了&#xff0c;只有光标一直在左上角&#xff0c;登录的框都是黑的&#xff0c;具体如下&#xff1a; 明明知道登录框的存在却怎么也触碰不到它T_T &#xff0c;先说解决方法&#xff1a; 产生这个问题的原因是因为磁…

针对Docker容器的可视化管理工具—DockerUI

目录 ⛳️推荐 前言 1. 安装部署DockerUI 2. 安装cpolar内网穿透 3. 配置DockerUI公网访问地址 4. 公网远程访问DockerUI 5. 固定DockerUI公网地址 ⛳️推荐 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下…

Nginx实用篇:实现负载均衡、限流与动静分离

Nginx实用篇&#xff1a;实现负载均衡、限流与动静分离 | 原创作者/编辑&#xff1a;凯哥Java | 分类&#xff1a;Nginx学习系列教程 Nginx 作为一款高性能的 HTTP 服务器及反向代理解决方案&#xff0c;在互联网架构中扮演着至关重要的角色。它…

JAVA自助高效安全无人台球茶室棋牌室系统小程序源码

​探索“自助高效安全无人台球茶室棋牌室系统”的奇妙之旅 &#x1f3b1;&#x1f375;&#x1f3b2; &#x1f50d; 初见惊艳&#xff1a;未来娱乐新体验 &#x1f50d; 走进这家无人值守的台球茶室棋牌室&#xff0c;第一感觉就像是穿越到了未来&#xff01;没有繁琐的前台登…

linux网络-----传输层

前言 一.传输层&#xff1a; 数据要交接应用层先通过传输层&#xff08;给哪个程序发数据&#xff09; 传输层作用&#xff1a;负责数据能够从发送端传输接收端。对于应用层来说有许多服务&#xff0c;传输层怎么知道把数据发给那个应用服务&#xff1f; 这时就有了端口号&am…