SpringBoot集成redisson操作redis

1 引入maven

只需引入redisson-spring-boot-starter就可以了,不过这里需要注意springboot与redisson的版本,因为官方推荐redisson版本与springboot版本配合使用。版本不匹配会导致这种问题。

将 Redisson 与 Spring Boot 库集成。取决于Spring Data Redis模块,支持 Spring Boot 1.3.x - 2.4.x

这句话是官方说的,不过现在的2.5.x也是支持的,只需要注意springboot最低版本不要低于1.3.x即可。

redisson-spring-data与Spring Boot version的版本对应关系

redisson-spring-data module nameSpring Boot version
redisson-spring-data-16 1.3.x
redisson-spring-data-17 1.4.x
redisson-spring-data-18 1.5.x
redisson-spring-data-20 2.0.x
redisson-spring-data-21 2.1.x
redisson-spring-data-22 2.2.x
redisson-spring-data-23 2.3.x
redisson-spring-data-24 2.4.x

点击redisson-spring-boot-starter进去

org.redisson
redisson-spring-data-26
${project.version}

依赖的springboot版本为2.6

若与项目中版本不匹配
可如此处理

maven依赖

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.13.2</version>
        </dependency>

2 配置文件

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=xxxx
spring.redis.timeout=3000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=20
spring.redis.lettuce.pool.min-idle=0
spring.redis.redisson.config.singleServerConfig.idleConnectionTimeout=10000
spring.redis.redisson.config.singleServerConfig.pingTimeout=1000
spring.redis.redisson.config.singleServerConfig.connectTimeout=10000
spring.redis.redisson.config.singleServerConfig.timeout=3000
spring.redis.redisson.config.singleServerConfig.retryAttempts=3
spring.redis.redisson.config.singleServerConfig.retryInterval=1500
spring.redis.redisson.config.singleServerConfig.reconnectionTimeout=3000
spring.redis.redisson.config.singleServerConfig.failedAttempts=3
spring.redis.redisson.config.singleServerConfig.password=null
spring.redis.redisson.config.singleServerConfig.subscriptionsPerConnection=5
spring.redis.redisson.config.singleServerConfig.clientName=null
spring.redis.redisson.config.singleServerConfig.address=redis://127.0.0.1:6379
spring.redis.redisson.config.singleServerConfig.subscriptionConnectionMinimumIdleSize=1
spring.redis.redisson.config.singleServerConfig.subscriptionConnectionPoolSize=50
spring.redis.redisson.config.singleServerConfig.connectionMinimumIdleSize=32
spring.redis.redisson.config.singleServerConfig.connectionPoolSize=64
spring.redis.redisson.config.singleServerConfig.database=0
spring.redis.redisson.config.singleServerConfig.dnsMonitoringInterval=5000
spring.redis.redisson.config.threads=0
spring.redis.redisson.config.nettyThreads=0
spring.redis.redisson.config.transportMode=NIO

3 使用RedissonClient

配置好后,就可以直接在项目中注入 RedissonClient 就可以了。

@Slf4j
@Service
public class RedissonService {

    @Autowired
    private RedissonClient redissonClient;


    //设置token值
    public void setToken(String key, String value) {
        RBucket<String> bucket = redissonClient.getBucket(key, new StringCodec());
        bucket.set(value, 60*60*2, TimeUnit.SECONDS);
    }

    //获取token值
    public String getToken(String key) {
        RBucket<String> bucket = redissonClient.getBucket(key,new StringCodec());
        return bucket.get();
    }

    //判断redis的值是否存在
    private boolean hasKey(String key) {
        RBucket<String> bucket = redissonClient.getBucket(key, new StringCodec());
        return bucket.isExists();
    }

}

 

1.Unable to decode data String解码问题

在写或者取数据的时候加入编码模式,修改代码为

    //设置token值
    public void setToken(String key, String value) {
        RBucket<String> bucket = redissonClient.getBucket(key, new StringCodec());
        bucket.set(value, 60*60*2, TimeUnit.SECONDS);
    }

    //判断redis的值是否存在
    private boolean hasKey(String key) {
        RBucket<String> bucket = redissonClient.getBucket(key, new StringCodec());
        return bucket.isExists();
    }

 

参考:

https://blog.csdn.net/DerekSu/article/details/106472042

https://blog.csdn.net/s_156/article/details/130847610

posted @ 2024-01-04 17:18  思凡念真  阅读(3312)  评论(0编辑  收藏  举报