如何在spring boot redis会话实现中捕获redis连接失败?

a1o7rhls  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(347)

我已经在springbootjava应用程序上使用lettuceconnectionfactory实现了redis会话管理。我想不出一个方法来捕捉redis连接失败。有没有办法捕捉到连接故障?
Spring Boot版本:2.2.6
代码:

@Configuration
@PropertySource("classpath:application.properties")
@EnableRedisHttpSession
public class HttpSessionConfig extends AbstractHttpSessionApplicationInitializer {

    Logger logger = LoggerFactory.getLogger(HttpSessionConfig.class);

    @Value("${spring.redis.host}")
    private String redisHostName;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Value("${spring.redis.password}")
    private String redisPassword;

    private @Value("${spring.redis.custom.command.timeout}")
    Duration redisCommandTimeout;

    private @Value("${spring.redis.timeout}")
    Duration socketTimeout;

    @Bean
    LettuceConnectionFactory lettuceConnectionFactory() {

        final SocketOptions socketOptions = SocketOptions.builder().connectTimeout(socketTimeout).build();

        final ClientOptions clientOptions = ClientOptions.builder()
                .socketOptions(socketOptions)
                .build();

        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .commandTimeout(redisCommandTimeout)
                .clientOptions(clientOptions)
                .readFrom(ReadFrom.SLAVE_PREFERRED)
                .build();
        RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(redisHostName,
                redisPort);

        final LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(serverConfig,
                clientConfig);
        return lettuceConnectionFactory;
    }

    @Bean
    public RedisTemplate<Object, Object> sessionRedisTemplate() {
        final RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory());
        return template;
    }     

    @Bean
    public ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题