SpringBoot集成Redisson操作Redis

目录

一、前言

Redisson 是一个在 Redis 的基础上实现的 Java 驻内存数据网格,Redisson相比较与Jedis和Lettuce来说最大的区别就是,Redisson提供了很多分布式相关操作服务,例如,分布式锁,分布式集合,可通过Redis支持延迟队列等,一般建议Lettuce + Redisson一起使用,需要使用Redis高级功能就使用Redisson,如果不需要使用高级功能优先推荐使用Lettuce。

二、基础集成配置(redis单节点)

工程结构

2.1、POM

java 复制代码
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.17.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.2、添加配置文件

java 复制代码
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    private String redissonUrl = "redis://172.16.8.169:6379";
    private String password = "123456";
    private Integer datebase = 0;

    @Bean
    public RedissonClient redisson() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress(redissonUrl)
                .setPassword((password == null || "".equals(password)) ? null : password)
                .setDatabase(datebase)
                // 连接空闲超时,如果当前连接池里的连接数量超过了最小空闲连接数,而同时有连接空闲时间超过了该数值,那么这些连接将会自动被关闭,并从连接池里去掉。时间单位是毫秒。
                .setIdleConnectionTimeout(10000)
                // 连接超时,同节点建立连接时的等待超时。时间单位是毫秒。
                .setConnectTimeout(10000)
                // 命令等待超时,等待节点回复命令的时间。该时间从命令发送成功时开始计时。
                .setTimeout(1000)
                // 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时
                .setRetryAttempts(3)
                // 命令重试发送时间间隔,在一条命令发送失败以后,等待重试发送的时间间隔。时间单位是毫秒。
                .setRetryInterval(1500)
                // 每个连接的最大订阅数量。
                .setSubscriptionsPerConnection(5)
                // 用于发布和订阅连接的最小保持连接数(长连接)。Redisson内部经常通过发布和订阅来实现许多功能。长期保持一定数量的发布订阅连接是必须的。
                .setSubscriptionConnectionMinimumIdleSize(1)
                // 用于发布和订阅连接的连接池最大容量。连接池的连接数量自动弹性伸缩。
                .setSubscriptionConnectionPoolSize(50)
                // 最小保持连接数(长连接)。长期保持一定数量的连接有利于提高瞬时写入反应速度。
                .setConnectionMinimumIdleSize(50)
                // 连接池最大容量。连接池的连接数量自动弹性伸缩。
                .setConnectionPoolSize(100)
                // 监测DNS的变化情况的时间间隔。时间单位是毫秒。
                .setDnsMonitoringInterval(5000)
                // PING 心跳时间,单位毫秒。
                .setPingConnectionInterval(10000);
        // 0 cpu * 2
        config.setThreads(0);
        // 0 cpu * 2
        config.setNettyThreads(0);
        // 使用json序列化方式
        config.setCodec(new JsonJacksonCodec());

        //创建客户端(发现创建RedissonClient非常耗时,基本在2秒-4秒左右)
        return Redisson.create(config);
    }
}

2.3、添加启动类

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RedissonApplication {
    public static void main(String[] args) {
        SpringApplication.run(RedissonApplication.class);
    }
}

2.4、添加测试类测试redisson操作redis

java 复制代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes =  RedissonApplication.class)
public class RedissonTest {
    @Autowired
    private RedissonClient redissonClient;
    @Test
    public void t1(){
        String key = "key1";
        System.out.println("获取Bucket");
        RBucket<Object> bucket = redissonClient.getBucket(key);
        System.out.println("插入数据到redis");
        bucket.set("value1");
        Object value = bucket.get();
        System.out.println("从redis中获取到值为 "+value);
        Boolean delete = bucket.delete();
        System.out.println("删除redis中值 "+delete);
    }
}
相关推荐
魔尔助理顾问2 小时前
系统整理Python的循环语句和常用方法
开发语言·后端·python
程序视点3 小时前
Java BigDecimal详解:小数精确计算、使用方法与常见问题解决方案
java·后端
GEM的左耳返3 小时前
Java面试全攻略:Spring生态与微服务架构实战
spring boot·redis·spring cloud·微服务·kafka·java面试
愿你天黑有灯下雨有伞3 小时前
Spring Boot SSE实战:SseEmitter实现多客户端事件广播与心跳保活
java·spring boot·spring
你的人类朋友3 小时前
❤️‍🔥微服务的拆分策略
后端·微服务·架构
程序员勋勋13 小时前
Redis的String数据类型底层实现
数据库·redis·缓存
颜颜yan_4 小时前
Python面向对象编程详解:从零开始掌握类的声明与使用
开发语言·redis·python
AI小智4 小时前
后端变全栈,终于可以给大家推出我的LangChain学习小站了!
后端
lkf197115 小时前
商品中心—1.B端建品和C端缓存
开发语言·后端·缓存
我的ID配享太庙呀5 小时前
Django 科普介绍:从入门到了解其核心魅力
数据库·后端·python·mysql·django·sqlite