redisson分布式锁并发测试

redisson分布式锁并发测试

模拟秒杀抢购场景,100库存,用jmeter并发测试,起300个线程并发请求2次,总计600个请求数,最后查看库存是否为负数,证明分布式锁是否锁住了库存。

注意:分布式锁并不是实现秒杀最佳方式,本文只是侧重说明分布式锁

一、编写相关代码

1、添加依赖项
dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web:2.2.0.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-redis:2.2.0.RELEASE'
    compile 'org.redisson:redisson-spring-boot-starter:3.11.5'
    compileOnly 'org.projectlombok:lombok:1.18.10'
    annotationProcessor 'org.projectlombok:lombok:1.18.10'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
2、添加启动类
package com.opensource.components.lock;

import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DistributedLockApplication {

    public static void main(String[] args) {
        SpringApplication.run(DistributedLockApplication.class, args);
    }
    
    @Bean
    public Redisson redisson() {
        Config config = new Config();
        config.useClusterServers().addNodeAddress("redis://172.16.10.212:6379", "redis://172.16.10.212:6380", "redis://172.16.10.212:6381");
        return (Redisson) Redisson.create(config);
    }
}
3、添加商品接口类
package com.opensource.components.lock;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class ItemController {
    private final static String ITEM_COUNT = "item:count";
    private final static String LOCK_KEY = "item:count:lock";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private Redisson redisson;


    /**
     * 初始化商品数量
     *
     * @param value
     * @return
     */
    @RequestMapping("/setCount")
    public String setCount(int value) {
        stringRedisTemplate.opsForValue().set(ITEM_COUNT, value + "");
        return "success";
    }

    /**
     * 查询商品数量
     *
     * @return
     */
    @RequestMapping("/getCount")
    public String getCount() {
        return stringRedisTemplate.opsForValue().get(ITEM_COUNT);
    }

    /**
     * 模拟秒杀抢购
     *
     * @return
     */
    @RequestMapping("/spike")
    public String spike() {
        String result = "fail";
        RLock lock = redisson.getLock(LOCK_KEY);
        boolean isLock = false;
        lock.lock(30, TimeUnit.SECONDS);
        if (lock.isLocked()) {
            try {
                int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get(ITEM_COUNT).toString());
                if (stock > 0) {
                    result = "success";
                    stringRedisTemplate.opsForValue().set(ITEM_COUNT, (stock - 1) + "");
                } else {
                    result = "fail";
                }
            } finally {
                if (lock.isHeldByCurrentThread()) {
                    //手动释放锁
                    lock.unlock();
                }
            }
        }
        log.info(Thread.currentThread().getName() + ", result: " + result);
        return result;
    }
}

二、配置jmeter

1、添加Thread Group配置

image-20200330163530072

2、配置http请求

image-20200330164814671

三、执行测试

1、初始化商品数量

image-20200330172025648

2、查询商品数量

image-20200330172118112

3、查询Summary Report

image-20200330174610748

4、再次查询库存为0,说明锁住了,没有发生超卖现象

image-20200330174656019

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值