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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章