使用Jmeter模拟1秒发送100个请求

在分布式系统中,使用syncnized是没有用的,无法达到锁的效果。因为实例都运行在不同的机器上,syncrinized关键字是把锁加在当前的类或对象上。运行在不同额实例上的程序类对象什么的肯定是完全不一样的,下面即用一个简单的例子来证明:

首先搭建一个如下架构的简单测试环境:

使用Jmeter模拟1秒发送100个请求_第1张图片

1. 在虚拟机上安装并配置后一个nginx服务器,nginx的配置文件如下。

   下面配置的192.168.0.112 server是我自己电脑的ip,我的IDEA就运行在这个ip上,我通过修改yml的端口号,在IDEA中启动了2个服务8080和8081。这里用到了nginx的负载均衡功能。

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream redistock {
        server 192.168.0.112:8080 weight=1;
        server 192.168.0.112:8081 weight=1;
    }       
        
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://redistock; 
       }
}

2. 下载安装redis,并启动一个redis服务,在redis中set一个stock值为50.

3. 在IDEA中用如下代码启动2个服务。

Application.java

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

IndexController.java

@RestController
public class IndexController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @RequestMapping("/getPosition")
    public String deductStock(){

        Integer restStock = Integer.valueOf(stringRedisTemplate.opsForValue().get("stock"));
        synchronized (this) {
            if (restStock > 0) {
               int tmpStock =  restStock-1;
                stringRedisTemplate.opsForValue().set("stock", tmpStock + "");
                System.out.println("left stock count: " + tmpStock);
            } else {
                System.out.println("Error, no stock left");
            }
        }
        return  "You still have "+ restStock + " shares";
    }
}

application.yml 

192.168.0.101 是我虚拟机的ip地址

server:
  port: 8080

spring:
  redis:
    host: 192.168.0.101
    port: 6379

4. 修改上面的yml的端口后8081,再启动一次。

这样就搭建好了以上的架构。

使用Jmeter进行压测:

下载地址:http://jmeter.apache.org/download_jmeter.cgi

下载并解压下列包到本地,打开bin目录下的jmeter.bat文件进行运行。

使用Jmeter模拟1秒发送100个请求_第2张图片

新建组

使用Jmeter模拟1秒发送100个请求_第3张图片

创建Http request 

使用Jmeter模拟1秒发送100个请求_第4张图片

创建报告 

使用Jmeter模拟1秒发送100个请求_第5张图片

在组中配置,100个请求在0秒中内发出,连续发1遍

使用Jmeter模拟1秒发送100个请求_第6张图片

配置request: 

使用Jmeter模拟1秒发送100个请求_第7张图片

执行结果如下,可以看到有大量的重复值,可以看出在分布式系统中,syncnized是没有用的。

Application1 8080:

left stock count: 48
left stock count: 47
left stock count: 47
left stock count: 45
left stock count: 45
left stock count: 44
left stock count: 43
left stock count: 43
left stock count: 42
left stock count: 42
left stock count: 42
left stock count: 42
left stock count: 42
left stock count: 41
left stock count: 40
left stock count: 40
left stock count: 40
left stock count: 38
left stock count: 37
left stock count: 37
left stock count: 37
left stock count: 34
left stock count: 34
left stock count: 34
left stock count: 33
left stock count: 33
left stock count: 31
left stock count: 30
left stock count: 30
left stock count: 28
left stock count: 26
left stock count: 25
left stock count: 25
left stock count: 24
left stock count: 23
left stock count: 22
left stock count: 21
left stock count: 20
left stock count: 19
left stock count: 19
left stock count: 18
left stock count: 18
left stock count: 18
left stock count: 17
left stock count: 16
left stock count: 15
left stock count: 15
left stock count: 14
left stock count: 13
left stock count: 12

Application2:8081

left stock count: 49
left stock count: 48
left stock count: 48
left stock count: 46
left stock count: 45
left stock count: 44
left stock count: 43
left stock count: 43
left stock count: 43
left stock count: 42
left stock count: 42
left stock count: 42
left stock count: 42
left stock count: 42
left stock count: 41
left stock count: 39
left stock count: 39
left stock count: 38
left stock count: 37
left stock count: 36
left stock count: 36
left stock count: 35
left stock count: 34
left stock count: 34
left stock count: 34
left stock count: 32
left stock count: 31
left stock count: 30
left stock count: 30
left stock count: 29
left stock count: 27
left stock count: 26
left stock count: 25
left stock count: 24
left stock count: 23
left stock count: 22
left stock count: 22
left stock count: 21
left stock count: 20
left stock count: 19
left stock count: 19
left stock count: 18
left stock count: 18
left stock count: 17
left stock count: 16
left stock count: 16
left stock count: 15
left stock count: 15
left stock count: 14
left stock count: 12

 

你可能感兴趣的:(redis,分布式系统,Redis分布式锁,redis)