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整合,希望对你有用

相关推荐
phltxy2 分钟前
Redis 持久化机制
java·redis·git
IT_陈寒11 分钟前
被JavaScript的隐式类型转换坑到怀疑人生,记录这次离谱经历
前端·人工智能·后端
Gerardisite16 分钟前
企业微信客户管理系统实战:标签、分层与自动化流程搭建
java·python·机器人·自动化·企业微信
递归尽头是星辰17 分钟前
跳表为核:串联 Redis、ES 与业务架构的底层思想复用
redis·elasticsearch·跳表·数据结构的应用·中间件底层原理
ch.ju20 分钟前
Java程序设计(第3版)第三章——数组的定义方式
java·开发语言
Francek Chen21 分钟前
【大数据存储与管理】云数据库:03 云数据库系统架构
大数据·数据库·分布式·架构
wzl2026121321 分钟前
流量浪费的底层原因:基于SpringBoot构建企微客户精细化运营系统
spring boot·后端·企业微信
2601_9577867727 分钟前
AI 原生营销矩阵系统:分布式素材管理与多租户权限控制技术实现
人工智能·分布式·矩阵
楼田莉子34 分钟前
Linux网络:多路转接IO
服务器·c++·后端·软件构建
Chloeis Syntax35 分钟前
JavaEE学习日记(2)---文件操作和IO
java·笔记·学习·java-ee