• 使用redisson实现分布式锁


    一: 添加依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson</artifactId>
                <version>3.12.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>

    二: redisson加入spring容器

    @Configuration
    public class WebConfig {
        @Bean
        public Redisson redisson() {
            // 1. Create config object
            Config config = new Config();
            config.useSingleServer()
                    .setAddress("redis://127.0.0.1:6379")
                    .setDatabase(0);
            // 2. Create Redisson instance
            return (Redisson) Redisson.create(config);
        }
    }

    三: 代码实现

    @RestController
    @SpringBootApplication
    @RequiredArgsConstructor
    public class RedisDistributeLockApplication {
    
        final StringRedisTemplate redisTemplate;
        final Redisson redisson;
    
        public static void main(String[] args) {
            SpringApplication.run(RedisDistributeLockApplication.class, args);
        }
    
    
        private static final String KEY_STOCK = "stock";
        private static final String KEY_LOCK = "lock";
    
    
        @GetMapping("/decrStock")
        public void decrStock() {
    
            RLock rLock = redisson.getLock(KEY_LOCK);
            rLock.lock(60, TimeUnit.SECONDS);
            try {
                String s = redisTemplate.opsForValue().get(KEY_STOCK);
                assert s != null;
                int stock = Integer.parseInt(s);
                if (stock > 0) {
                    redisTemplate.opsForValue().set(KEY_STOCK, String.valueOf(stock - 1));
                    System.out.println("扣减成功,库存stock:" + (stock - 1));
                } else {
                    System.out.println("扣减失败,库存不足!");
                }
            } finally {
                rLock.unlock();
            }
    
        }
    }

    四: 使用jemeter压测

    200线程同时执行,循环5次

    测试结果: 符合预期

  • 相关阅读:
    spring boot 配置示例
    MyBatis 常用标签用法
    http请求头部常用参数
    CentOS7使用firewalld打开关闭防火墙与端口
    java8 base64
    MD5工具类
    各种远程登录工具
    MySql 常用命令
    spring-boot-mybaits 开启事务
    springboot 项目打包到 linux下无法 运行
  • 原文地址:https://www.cnblogs.com/alenblue/p/12895041.html
Copyright © 2020-2023  润新知