054 redisson

文章目录

java 复制代码
package com.xd.cubemall.product.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyRedissonConfig {

    /**
     * 注册RedissonClient对象
     */
    @Bean(destroyMethod="shutdown")
    RedissonClient redissonClient(){
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        RedissonClient redissonClient = Redisson.create(config);

        return redissonClient;

    }

}

使用Redisson演示可重入锁

java 复制代码
    /**
     * 使用Redisson演示可重入锁
     * @return
     */
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        //获取一把锁
        RLock lock = redissonClient.getLock("my-lock");
        //加锁
        lock.lock();
        try{
            System.out.println("加锁成功,执行业务。。。" + Thread.currentThread().getId());
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //解锁
            System.out.println("解锁。。。" + Thread.currentThread().getId());
            lock.unlock();
        }


        return "hello";
    }

读写锁

java 复制代码
    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){
        //获取一把锁
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        // 加 写锁
        String s = "";
        RLock writeLock = lock.writeLock();

        try{
            //1.改数据加 写锁,读数据加 读锁
            writeLock.lock();
            System.out.println("写锁加锁成功..." + Thread.currentThread().getId());
            s = UUID.randomUUID().toString();
            Thread.sleep(15000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            writeLock.unlock();
            System.out.println("写锁。。释放。。"+Thread.currentThread().getId());

        }
        return s;
    }


    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        String s = "";
        //加 读锁
        RLock readLock = lock.readLock();
        readLock.lock();
        try {
            System.out.println("读锁...加锁成功..."+Thread.currentThread().getId());
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
            System.out.println("读锁。。释放。。。"+Thread.currentThread().getId());

        }
        return s;
    }

信号量

java 复制代码
    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore semaphore = redissonClient.getSemaphore("park");
        //当车位减少为0时,还想获取车位,必须要等go()调用时,释放一个车位,才能进行执行
        semaphore.acquire();//占有一个车位

        return "ok";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() {
        RSemaphore semaphore = redissonClient.getSemaphore("park");
        semaphore.release();
        return "ok";
    }

闭锁

java 复制代码
    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();//等待其他闭锁都完成
        return "关门了";
    }


    @ResponseBody
    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("id") Long id) {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();//计数-1
        return id + "桌的人都走了。。。";
    }

获取三级分类redisson分布式锁

java 复制代码
    @Autowired
    private RedissonClient redissonClient;
    /**
     * 获取三级分类(redisson分布式锁)
     * @return
     */

    public List<CategoryVo> getCategoryJsonFromWithRedissonLock() {

        String uuid = UUID.randomUUID().toString();
        //1.占分布式
        RLock lock = redissonClient.getLock("CategoryJson-lock");

        lock.lock();
        List<CategoryVo> dataFromDb = null;
        try {
            dataFromDb = getDataFromDb();
        } finally {
            lock.unlock();
        }

        return dataFromDb;




    }
相关推荐
小小工匠6 天前
Redisson - 分布式锁和同步器
分布式锁·redisson·同步器
救救孩子把7 天前
深入解析 RedissonMultiLock —— 分布式联锁的原理与实战
redis·分布式·redisson
马走日mazouri9 天前
Redisson 分布式锁原理
java·redis·redisson
怦然心动~19 天前
springboot 3 集成Redisson
java·spring boot·redisson
东阳马生架构1 个月前
分布式锁—5.Redisson的读写锁一
分布式·分布式锁·redisson
东阳马生架构1 个月前
分布式锁—4.Redisson的联锁和红锁二
分布式锁·redisson
东阳马生架构1 个月前
分布式锁—3.Redisson的公平锁一
分布式·分布式锁·redisson
mikey棒棒棒1 个月前
Redis——优惠券秒杀问题(分布式id、一人多单超卖、乐悲锁、CAS、分布式锁、Redisson)
数据库·redis·lua·redisson·watchdog·cas·并发锁