Spring Boot 整合Redisson

本文介绍如何在Spring Boot项目中使用Redisson 3.16.2,并配置单机模式,提供RedissonClient的创建和配置文件详解。通过实例演示如何在控制器中实现RLock的使用和测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版本说明

spring-boot: 2.3.5.RELEASE
redisson: 3.16.2

引入依赖

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.16.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-actuator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

在resources下新建redisson.yaml配置文件(单机模式)

参照官网说明: https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#21-%E7%A8%8B%E5%BA%8F%E5%8C%96%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95
在这里插入图片描述

编写配置类

package com.example.demo.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.IOException;

/**
 * @author xxf
 * @class Redisson
 * @date 2021/9/14 15:19
 */
@Configuration
public class RedissonSpringDataConfig {

    @Bean
    public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
        return new RedissonConnectionFactory(redisson);
    }

    /**
     * @description redisson配置
     * @author xxf
     * @date 2021/9/15 10:52
     * @param configFile
     * @return
     */
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
        Config config = Config.fromYAML(configFile.getInputStream());
        //测试看门狗(默认30s)
        config.setLockWatchdogTimeout(50000);
        String s = config.toYAML();
        System.out.println(s);
        return Redisson.create(config);
    }

}

编写测试类

package com.example.demo.controller;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author xxf
 * @class RedissonTestController
 * @date 2021/9/14 13:24
 */
@RestController
public class RedissonTestController {

    @Autowired
    private RedissonClient redissonClient;

    private static String KEY = "mykey";

    @GetMapping("/testRedisson")
    public String testRedisson() {
        RLock lock = redissonClient.getLock(KEY);
        //判断是否有任务在执行
        if (lock.isLocked()) {
            System.out.println(System.currentTimeMillis() + "任务已被锁定,稍后再试");
            return "fail";
        }
        try {
            lock.lock();
            System.out.println("执行任务中...");
            //执行20s
            Thread.sleep(20000);
            System.out.println("任务完成");
        } catch (Exception e) {
            System.out.println("获取锁异常");
        } finally {
            //释放锁
            lock.unlock();
        }

        return "ok";
    }
}

启动项目测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值