Redis在我的应用程序中是可选的,这是要求之一,即使Redis关闭,应用程序也必须能够启动而不会出现任何问题。
我可以使用spring-data-redis 1.8.1版本处理此问题,但是当我升级到2.0.1时会引起问题。
我能够捕获在此期间引发的异常,LettuceConnectionFactory但是现在它引发了异常,并且控制权未返回给调用类。
请让我知道如何使Redis bean实例化成为可选,以便即使Redis关闭,应用程序服务器也可以正常启动。
SpringRedisConfig.java
@Configuration
public class SpringRedisConfig{
private static final Logger logger = LoggerFactory.getLogger(SpringRedisConfig.class);
@Value("${redis.config.file.path}")
private String redisConfigFilePath;
@Value("${redis.timeout}")
private long redisTimeOut;
public @Bean RedisTemplate redisTemplate(){
RedisTemplate<String, Object> redisTemplate = null;
try {
redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericToStringSerializer<>(Long.class));
redisTemplate.setConnectionFactory(connectionFactory());
}catch(Exception e){
logger.error("Error getting Redis Template connection ",e);
}
return redisTemplate;
}
@Bean
public LettuceConnectionFactory connectionFactory() {
LettuceConnectionFactory connectionFactory = null;
try {
ClientOptions clientOptions = …Run Code Online (Sandbox Code Playgroud)