SpringBoot整合Redisson,赶紧整起来!

SpringBoot整合Redisson


提示:以下是本篇文章正文内容

一、Redisson 是什么?

Redisson是一个基于Java的开源的、高级的Redis客户端,它实现了Redis的分布式和响应式特性,Redisson能够让Java开发者更方便地与Redis进行交互。

简单来说Redisson就是一个Redis的客户端,比RedisTemplate更高级,更简单。

二、使用场景

  • 分布式锁(最常用)Redisson实现分布式锁是非常简单的
java 复制代码
@Resource
private RedissonClient redissonClient;

RLock rLock = redissonClient.getLock(lockName);
try {
    boolean isLocked = rLock.tryLock(expireTime, TimeUnit.MILLISECONDS);
    if (isLocked) {
        // TODO
    }
  } catch (Exception e) {
    rLock.unlock();
  }
  • 基于redis实现队列

三、使用步骤

1.引入相关依赖

xml 复制代码
<dependencies>
    <!-- redisson -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.20.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
</dependencies>

2.application.yml配置

配置如下:

yaml 复制代码
spring:
    redis:
        database: 1
        host: xxx
        port: xxx
        password: xxx   # 密码(默认为空)
        timeout: 6000ms  # 连接超时时长(毫秒)
        jedis:
            pool:
                max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
                max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)
                max-idle: 10      # 连接池中的最大空闲连接
                min-idle: 5       # 连接池中的最小空闲连接

3.创建RedissonConfig

编写类 RedissonConfig.java

java 复制代码
package com.uhu.redis;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.database}")
    private int database;

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Bean(value = "redissonClient", destroyMethod = "shutdown")
    public RedissonClient redissonClient() throws Exception {

        Config config = new Config();
        config.useSingleServer().setAddress(String.format("redis://%s:%s", this.host, this.port));
        if (!this.password.isEmpty()) {
            config.useSingleServer().setPassword(this.password);
        }
        config.useSingleServer().setDatabase(this.database);

        StringCodec codec = new StringCodec();
        config.setCodec(codec);
        return Redisson.create(config);
    }

}

4.开始使用

java 复制代码
@Resource
private RedissonClient redissonClient;

总结

通过以上几个简单的步骤,我们就可以完成Redisson整合,希望对你有用

相关推荐
昵称为空C2 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
架构师沉默2 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
RoyLin3 小时前
TypeScript设计模式:适配器模式
前端·后端·node.js
该用户已不存在3 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
Moonbit3 小时前
MoonBit 正式加入 WebAssembly Component Model 官方文档 !
前端·后端·编程语言
Goland猫3 小时前
电商架构图
后端
Java中文社群4 小时前
重要:Java25正式发布(长期支持版)!
java·后端·面试
我是天龙_绍4 小时前
Whisper 通过 mp3输出中文
后端
zjjuejin4 小时前
Maven环境搭建
后端·maven
我是天龙_绍4 小时前
项目根目录有requirements.txt 如何安装
后端