使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案

本文主要是介绍使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案》在SpringBoot应用中,我们经常使用​​@Cacheable​​注解来缓存数据,以提高应用的性能...

@Cacheable注解Redis时,Redis宕机或其他原因连不上,继续调用原方法的解决方案

在Spring Boot应用中,我们经常使用​​@Cacheable​​注解来缓存数据,以提高应用的性能。当选择Redis作为缓存存储时,如果Redis服务因某种原因不可用(如宕机、网络问题等),默认情况下,​​@Cacheable​​注解会抛出异常,导致整个请求失败。本文将探讨如何在Redis不可用时,让​​@Cacheable​​注解继续调用原方法,确保服务的可用性和稳定性。

1. 问题背景

1.1 ​​@Cacheable​​注解的基本使用

​@Cacheable​​是Spring框架提供的一个注解,用于标识一个方法的结果需要被缓存。当该方法被调用时,Spring会先检查缓存中是否存在对应的数据,如果存在,则直接返回缓存中的数据;如果不存在,则执行方法并将结果存入缓存。

1.2 Redis宕机的影响

当Redis服务宕机或网络连接出现问题时,​​@Cacheable​​注解尝试访问Redis时会抛出异常,例如​​org.springframework.data.redis.RedisConnectionFailureException​​。这会导致方法调用失败,影响用户体验和系统稳定性。

2. 解决方案

2.1 使用自定义异常处理器

可以通过自定义异常处理器来捕获Redis连接异常,并在捕获到异常时继续调用原方法。具体步骤如下:

2.1.1 创建自定义异常处理器

首先,创建一个自定义异常处理器类,用于处理Redis连接异常。

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.CacheErrorHandler;
public class CustomCacheErrorHandler implements CacheErrorHandler {
    @Override
    public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
        // 处理读取缓存时的异常
        System.out.println("Cache get error: " + exception.getMessage());
    }
    @Override
    public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
        // 处理写入缓存时的异常
        System.out.println("Cache put error: " + exception.getMessage());
    }
    @Override
    public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
        // 处理清除缓存时的异常
        System.out.println("Cache evict error: " + exception.getMessage());
    }
    @Override
    public void handleCacheClearError(RuntimeException exception, Cache cache) {
        // 处理清空缓存时的异常
        System.out.println("Cache clear error: " + exception.getMessage());
    }
}

2.1.2 配置自定义异常处理器

在Spring Boot配置文件中,配置自定义的异常处理器。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.cache.annotation.EnableCaching;
@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public CustomCacheErrorHandler customCacheErrorHandler() {
        return new CustomCacheErrorHandler();
    }
}

2.2 使用​​@Cacheable​​的​​unless​​属性

​@Cacheable​​注解提供了一个​​unless​​属性,可以在缓存操作成功后决定是否将结果存入缓存。虽然这个属性不能直接解决Redis宕机的问题,但可以结合其他逻辑来实现类似的效果。

2.3 使用​​@Cacheable​的​​cache-null-values​​属性

设置​​@Cacheable​​注解的​​cache-null-values​​属性为​​false​​,这样即使Redis不可用,也不会将​​null​​值存入缓存。

@Cacheable(value = "myCache", cacheNullValues = false)
public User getUserById(Long id) {
    return userRepository.findById(id).orElse(null);
}

2.4 使用降级策略

在Redis不可用时,可以采用降级策略,例如从数据库中直接获取数据。这可以通过自定义的缓存管理器来实现。

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
javascriptimport Java.util.Optional;
@Configuration
public class CustomCacheManager {
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("myCache") {
            @Override
            public Cache getCache(String name) {
                Cache cache = super.getCache(name);
                if (cache == null) {
                    // 如果Redis不可用,使用本地缓存
                    cache = new ConcurrentMapCache(name);
                }
     php           return cache;
            }
        };
    }
}

我们可以在Redis不可用时,确保​​@Cacheable​​​注解继续调用原方法,从而提高系统的稳定性和可用性。具体实现方式包括自定义异常处理器、使用​​unless​​​和​​cache-null-values​​属性、以及降级策略。在使用Spring框架结合Redis实现缓存功能时,如果Redis宕机或由于其他原因导致连接不上Redis,可以通过配置​​CacheManager​​来实现当缓存不可用时自动回退到原始方法的调用。这样可以保证系统的可用性和稳定性。

以下是一个具体的实现示例:

添加依赖:首先确保你的项目中已经添加了Spring Boot和Redis的相关依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

配置Redis连接:在application.properties中配置Redis连接信息。

spring.redis.host=localhost
spring.redis.port=6379

自定义CacheManager:创建一个自定义的CacheManager,在其中处理Redis不可用的情况。

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2jsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1)) // 设置默认过期时间
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
        cacheConfigurations.put("myCache", config);
        return new FallbackRedisCacheManager(redisConnectionFactowww.chinasem.cnry, config, cacheConfigurations);
    }
}

实现FallbackRedisCacheManager:创建一个自定义的CacheManager,在Redis不可用时回退到内存缓存。

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFailureException;
import java.util.Arrays;
import java.util.List;
public class FallbackRedisCacheManager extends RedisCacheManager {
    private final CacheManager fallbackCacheManager;
    public FallbackRedisCacheManager(RedisConnectionFactory connectionFactory, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations) {
        super(connectionFactory, defaultCacheConfiguration, initialCacheConfigurations);
        SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
        simpleCacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("fallbackCache")));
        simpleCacheManager.afterPropertiesSet();
        this.fallbackCacheManager = simpleCacheManager;
    }
    @Override
    public Cache getCache(String name) {
        try {
            return super.getCache(name);
        } catch (RedisConnectionFailureException e) {
            return fallbackCacheManager.getCache(name);
        }
    }
}

使用@Cacheable注解:在需要缓存的方法上使用@Cacheable注解。

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class MyService {
    @Cacheable(value = "myCache", key = "#id")
    public String getData(String id) {
        // 模拟数据获取过程
        System.out.println("Fetching data from database for ID: " + id);
        return "Data for ID: " + id;
    }
}

通过上述配置,当Redis不可用时,​​FallbackRedisCacheManager​​会捕获到​​RedisConnectionFailureException​​异常,并回退到内存缓存。这样可以确保即使Redis宕机,系统仍然能够正常运行并返回数据。在使用Spring Cache与Redis结合时,如果Redis出现宕机或连接问题,可以通过配置​​CacheManager​​和实现自定义的​​CacheErrorHandler​​来确保即使缓存不可用,业务逻辑也能正常运行。以下是一个详细的解决方案示例:

1. 添加依赖

首先,确保你的项目中已经添加了Spring Boot Starter Cache和Spring Boot Starter Data Redis的依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifaandroidctId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>

2. 配置RedisTemplate

配置​​RedisTemplate​​以使用JSON序列化方式存储对象:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StrinlbDIaQEBEgRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

3. 配置CacheManager

配置​​CacheManager​​以使用Redis作为缓存存储:

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import java.time.Duration;
@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(60)) // 设置缓存过期时间为60分钟
                .disableCachingNullValues();
        return RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(config)
                .build();
    }
}

4. 实现自定义的CacheErrorHandler

实现自定义的​​CacheErrorHandler​​,以便在缓存操作失败时进行处理:

import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueException;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.CacheErrorHandler;
@Configuration
public class CacheConfig extends CachingConfigurerSupport {
    @Override
    public CacheErrorHandler errorHandler() {
        return new CacheErrorHandler() {
            @Override
            public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
                // 处理缓存读取错误
                System.out.println("Cache get error: " + exception.getMessage());
            }
            @Override
            public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
                // 处理缓存写入错误
                System.out.println("Cache put error: " + exception.getMessage());
            }
            @Override
            public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
                // 处理缓存删除错误
                System.out.println("Cache evict error: " + exception.getMessage());
            }
            @Override
            public void handleCacheClearError(RuntimeException exception, Cache cache) {
                // 处理缓存清除错误
                System.out.println("Cache clear error: " + exception.getMessage());
            }
        };
    }
}

5. 使用@Cacheable注解

在需要缓存的方法上使用​​@Cacheable​​注解:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
    @Cacheable(value = "users", key = "#userId")
    public User getUserById(String userId) {
        // 模拟从数据库获取用户信息
        System.out.println("Fetching user from database: " + userId);
        return new User(userId, "John Doe");
    }
}

6. 测试

你可以通过模拟Redis宕机或断开连接来测试上述配置是否生效。例如,可以临时关闭Redis服务,然后调用​​getUserById​​方法,查看是否能够正常返回数据。

总结

通过以上配置,当Redis不可用时,​​CacheErrorHandler​​会捕获到缓存操作的异常,并打印错误信息。同时,由于​​@Cacheable​​注解的默认行为是当缓存不可用时直接调用原方法,因此业务逻辑不会受到影响。这样可以确保系统的高可用性和稳定性。

到此这篇关于使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案的文章就介绍到这了,更多相关@Cacheable注解 Redis宕机内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!

这篇关于使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1154725

相关文章

MySQL查看表的历史SQL的几种实现方法

《MySQL查看表的历史SQL的几种实现方法》:本文主要介绍多种查看MySQL表历史SQL的方法,包括通用查询日志、慢查询日志、performance_schema、binlog、第三方工具等,并... 目录mysql 查看某张表的历史SQL1.查看MySQL通用查询日志(需提前开启)2.查看慢查询日志3.

MySQL底层文件的查看和修改方法

《MySQL底层文件的查看和修改方法》MySQL底层文件分为文本类(可安全查看/修改)和二进制类(禁止手动操作),以下按「查看方法、修改方法、风险管控三部分详细说明,所有操作均以Linux环境为例,需... 目录引言一、mysql 底层文件的查看方法1. 先定位核心文件路径(基础前提)2. 文本类文件(可直

Java实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

检查 Nginx 是否启动的几种方法

《检查Nginx是否启动的几种方法》本文主要介绍了检查Nginx是否启动的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1. 使用 systemctl 命令(推荐)2. 使用 service 命令3. 检查进程是否存在4

Java方法重载与重写之同名方法的双面魔法(最新整理)

《Java方法重载与重写之同名方法的双面魔法(最新整理)》文章介绍了Java中的方法重载Overloading和方法重写Overriding的区别联系,方法重载是指在同一个类中,允许存在多个方法名相同... 目录Java方法重载与重写:同名方法的双面魔法方法重载(Overloading):同门师兄弟的不同绝