springboot 整合redis问题,缓存击穿,穿透,雪崩,分布式锁

boot整合redis 压力测试出现失败

解决方案 排除lettuce 使用jedis

cpp 复制代码
 <!-- 引入redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

最终使用的都是redisTemplate

缓存穿透

分布式锁

分布式锁演示过程

进阶2 设置过期时间和不存在则枷锁

占锁

锁的值应该是一个唯一标识,不然分布式应用,相同的value 会引起此服务将另外的服务的锁给释放掉了

释放锁

删除也必须是原子性的,否则有可能redis在传输过程中已过期,结果没结果删

完整(保证一定会释放锁)

完整操作

使用redisson

引入依赖

cpp 复制代码
<!-- 以后使用Redisson作为所有分布式锁 -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.12.0</version>
        </dependency>

创建配置类

cpp 复制代码
@Configuration
public class MyRedissonConfig {

    /**
     * 所有对Redisson的使用都是通过RedissonClient
     * @return
     * @throws IOException
     */
    @Bean(destroyMethod="shutdown")
    public RedissonClient redisson() throws IOException {
        //1、创建配置
        Config config = new Config();
        config.useSingleServer().setAddress("redis://192.168.1.100:6379");

        //2、根据Config创建出RedissonClient实例
        //Redis url should start with redis:// or rediss://
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }

}

使用

方式2

给锁设置过期时间

最佳实战

我们设置所得过期时间,时间设置的大一些,然后手动枷锁,手动解锁

相关推荐
一叶飘零_sweeeet35 分钟前
2025 年 Redis 面试天花板
redis·缓存·面试
qq_12498707532 小时前
基于springboot的兴趣生活展示交流平台的设计与实现(源码+论文+部署+安装)
java·spring boot·生活·毕设
d***9352 小时前
Redis五种用途
数据库·redis·缓存
weixin_448771722 小时前
SpringBoot默认日志配置文件 logback.xml(log4j+logback)
xml·spring boot·logback
爬山算法3 小时前
Redis(128)Redis的跳表(Skip List)是如何实现的?
数据库·redis·list
Java水解4 小时前
20个高级Java开发面试题及答案!
spring boot·后端·面试
Unstoppable224 小时前
八股训练营第 20 天 | MySQL和Redis的区别是什么?Redis有什么优缺点、为什么用Redis查询会比较快?
数据库·redis·mysql·八股
百***86055 小时前
Spring BOOT 启动参数
java·spring boot·后端
跟着珅聪学java5 小时前
Spring Boot 中整合 MySQL 并打印 SQL 日志
java·spring boot
q***11655 小时前
SpringBoot创建动态定时任务的几种方式
java·spring boot·spring