如何在Redis连接失败时绕过Spring Boot应用程序上的Redis会话?

时间:2019-11-01 19:32:25

标签: spring-boot redis spring-session lettuce

通过遵循其中的一些示例应用程序和Spring文档,我已经在我的Spring Boot java应用程序上使用LettuceConnectionFactory实现了Redis会话管理。当它可以连接到Redis服务器时,它工作正常。但是,应用程序在连接失败时不会响应,因为它反复尝试连接。我已经做了很多研究以找到解决方案,但还找不到任何东西。我的问题是,

  1. 在重试一定次数后无法连接到Redis时,是否有一种方法可以切换到Spring会话(甚至可能是JDBC会话)?
  2. 是否可以设置连接工厂中的重试次数?我在spring.redis属性列表中看不到任何属性。

感谢您的帮助。

于11/4/2019更新

应用程序属性:


spring.session.store-type=redis
spring.session.redis.flush-mode=on-save
spring.session.redis.namespace=spring:session
spring.redis.host=*****.windows.net
spring.redis.port=6380
spring.redis.ssl=true
spring.redis.password=****************
spring.redis.timeout=5000ms
spring.redis.custom.command.timeout=1000ms

HttpSessionConfig.java:


@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;
    }
}

依赖项:


<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
   <version>2.1.4.RELEASE</version>
</dependency>
<dependency>
   <groupId>biz.paluch.redis</groupId>
   <artifactId>lettuce</artifactId>
   <version>3.5.0.Final</version>
</dependency>
<dependency>
   <groupId>io.lettuce</groupId>
   <artifactId>lettuce-core</artifactId>
   <version>5.1.6.RELEASE</version>
   <optional>true</optional>
</dependency>

如果需要更多详细信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

我们正在使用 @Profile(value =“ production”)用于RedisHttpSession和 DevSessionConfig的@Profile(value =“ development”)

然后可以在application.properties中设置spring.profiles.active