首页 > 其他分享 >演示jvm锁存在的问题

演示jvm锁存在的问题

时间:2024-09-21 18:49:29浏览次数:3  
标签:存在 演示 ms Completed bytes 74.1 192.168 jvm requests

文章目录


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() 没有加锁

    @Override
    public 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 benchmarking
                    This implies -n 50000
    -s timeout      Seconds to max. wait for each response
                    Default 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 attributes
                    are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
                    are 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 requests


Server Software:        
Server Hostname:        192.168.74.1
Server Port:            8500

Document Path:          /api/album/albumInfo/test/lock
Document Length:        76 bytes

Concurrency 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] received

Connection 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     581

Percentage of the requests served within a certain time (ms)
  50%     90
  66%    100
  75%    108
  80%    115
  90%    138
  95%    176
  98%    330
  99%    534
 100%    581 (longest request)

4.2、查看 redis 中的值

在这里插入图片描述

5、添加本地锁 synchronized

    @Override
    public 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 requests


Server Software:        
Server Hostname:        192.168.74.1
Server Port:            8500

Document Path:          /api/album/albumInfo/test/lock
Document Length:        76 bytes

Concurrency 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] received

Connection 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    2734

Percentage of the requests served within a certain time (ms)
  50%    415
  66%    548
  75%    624
  80%    664
  90%    750
  95%    800
  98%   1819
  99%   2408
 100%   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 requests


Server Software:        
Server Hostname:        192.168.74.1
Server Port:            8500

Document Path:          /api/album/albumInfo/test/lock
Document Length:        76 bytes

Concurrency 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] received

Connection 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     568

Percentage of the requests served within a certain time (ms)
  50%    165
  66%    218
  75%    249
  80%    269
  90%    317
  95%    351
  98%    398
  99%    436
 100%    568 (longest request)

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

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

以上测试,可以发现:

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

此时需要分布式锁。。

标签:存在,演示,ms,Completed,bytes,74.1,192.168,jvm,requests
From: https://blog.csdn.net/m0_65152767/article/details/142407390

相关文章

  • JVM内存学习
    java虚拟机在执行程序的过程中会将内存划分为不同的数据区域JVM分为五个区域:虚拟机栈、本地方法栈、方法区、堆、程序计数器。JVM五个区中虚拟机栈、本地方法栈、程序计数器为线程私有,方法区和堆为线程共享区JVM不同区域的占用内存大小不同,一般情况下堆最大,程序计数器较......
  • 铺先生:接手转店要留意什么风险?这些意识要存在
    接手转店要留意什么风险?做任何事情,几乎都会存在一定的风险,别看如今转店这么的风风火火,但接手起来也是需要承担很大的风险的,我们在接手转店的时候需要注意这几个问题,下面小编就来跟大家说说吧。1. 为何选择转店对方为何而选择将店铺转让出去?这个问题值得深思,当对方谈起这个问题的时......
  • java重点学习-JVM类加载器+垃圾回收
    12.7类加载器JVM只会运行二进制文件,类加载器的作用就是将字节码文件加载到JVM中,从而让Java程序能够启动起来。类加载器有哪些启动类加载器(BootStrapClassLoader):加载JAVAHOME/jre/lib目录下的库扩展类加载器(ExtClassLoader):主要加载JAVAHOME/jre/lib/ext目录中的类......
  • JVM —— 类加载器的分类,双亲委派机制
    文章目录一、类加载器的分类【理解】1.1概述1.2JDK8及之前的版本1.2.1启动类加载器1.2.2扩展类加载器和应用程序类加载器扩展类加载器应用程序类加载器1.3JDK9之后的类加载器1.4ClassLoader中的两个方法【应用】二、双亲委派模型【理解】2.1什么是双亲委派机制面试题:类的双......
  • Sql 数据存在就返回匹配数据 不存在就新增数据
    本次样例的执行内容是,匹配就返回匹配数据的id,不匹配就新增数据方式一DECLARE@userIdINTifnotEXISTS(select1fromdbo.UserwherePId=4ANDCode='XX')BEGININSERTINTOdbo.User(PId,Code)VALUES(4,'XX');SELECT@userId=IDENT_CURRENT('User');END......
  • 易优eyoucms网站数据表或视图不存在[错误代码] SQLSTATE[42S02],数据库表不存在。
    数据表或视图不存在[错误代码]SQLSTATE[42S02]:Basetableorviewnotfound:1146Table'fszojdk.web2023_archives'doesn'texist根据提供的错误信息 SQLSTATE[42S02]:Basetableorviewnotfound:1146Table'fszojdk.web2023_archives'doesn'texist,......
  • 易优eyoucms网站详情页报错报错 \core\library\think\Loader.php 类不存在:app\co
    类不存在:app\common\model\Pic,这个错误表明PHP无法找到类 app\common\model\Pic。这通常是因为类文件未被正确加载或命名空间配置不正确导致的。以下是一些可能的解决步骤:1.确认类文件路径确保类文件 Pic 的路径正确并且文件存在。检查文件路径确认 app\common\model......
  • py3.7+win10的cv2.xfeatures2d_SIFT.create()函数不存在问题
    python3.7环境window1064位cv2包问题。问题做图片处理用opencv-python做模板匹配的时候会用个sift模型,就会用到cv2.xfeatures2d_SIFT.create()这个函数,在我正要用它增加自己知识,巴拉巴啦....的时候,咦?!这是个什么鬼哦,没有这个函数呢。百度发现需要什么卸载原版本,换成opencv-......
  • 查询字符串在数据库哪些表那些列存在/根据字符串快速定位表定位列
    1SETQUOTED_IDENTIFIEROFF2GO3SETANSI_NULLSOFF4GO56IFEXISTS(SELECT*FROMdbo.sysobjectsWHEREid=OBJECT_ID(N'sp_FindString')ANDOBJECTPROPERTY(id,N'IsProcedure')=1)7DROPPROCEDUREsp_FindString8GO......
  • JVM--解析运行期优化与JIT编译器
    JVM开发团队一直在努力,缩小Java与C/C++语言在运行效率上的差距。本篇博客,我们来谈一谈JVM(HotSpot)为了提高Java程序的运行效率,都实现了哪些激动人心的技术~1JIT编译器的引入首先我们这篇文章中所说的编译器都是指JVM的组成部分之一---即时编译器(JIT),与生成Java字节码的javac编译......