• 使用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次

    测试结果: 符合预期

  • 相关阅读:
    书到用时方恨少---记录读书历程
    JAVASCRIPT数据类型(值类型-引用类型-类型总览)
    jQuery基本API小结(下)---工具函数-基本插件
    jQuery基本API小结(上)--选择器-DOM操作-动画-Ajax
    【转】javascript 执行环境,变量对象,作用域链
    JavaScript知识总结--对象的相关概念
    JavaScript知识总结--引用类型(Object-Array-Function-Global-Math)
    JavaScript知识总结--历史-html引用方式-基础概念
    Java--神奇的hashcode
    Java-从堆栈常量池解析equals()与==
  • 原文地址:https://www.cnblogs.com/alenblue/p/12895041.html
Copyright © 2020-2023  润新知