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

给锁设置过期时间

最佳实战

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

相关推荐
用户31268748772012 小时前
AI Agent 开发实战(六):用 Spring AI 搭建你的第一个 Agent
spring boot·openai
zhangjw3412 小时前
第36篇:Spring Boot进阶:Web开发+参数校验+全局异常处理
前端·spring boot·后端
完美火龙篇 四月的友12 小时前
SpringBoot 即时聊天 IM 完整实现(HTTP会话管理 \+ WebSocket实时推送 \+ 离线消息)
spring boot·websocket·http
Bug收容所13 小时前
12305项目学习day5
java·spring boot·redis·mysql·spring·rocketmq
智码看视界13 小时前
Day33-数据层 × 中间件AI化篇:Redis缓存经典问题-击穿、穿透、雪崩的终极解决方案
数据库·redis·缓存·中间件·穿透·雪崩·击穿
后端观测站1 天前
第九章:Redis 为什么会发生缓存穿透、击穿、雪崩?
redis
小Ti客栈1 天前
Spring Boot 集成 Springdoc-OpenAPI 与 Knife4j实现接口文档与可视化调试
java·spring boot·后端
paopaokaka_luck1 天前
基于Springboot3+Vue3的高校选课系统(AI选课助手、协同过滤算法、分享到微博、扣扣、Echarts图形化分析)
网络·spring boot·网络协议·echarts
小Ti客栈1 天前
Spring Boot 整合 Swagger2 和 Knife4j实现接口文档与可视化调试
java·spring boot·后端