Spring-boot 2中配置Lettuce作Redis客户端以及Sentinel配置

springboot 2中默认使用lettuce作为redis客户端,下面介绍如何利用lettuce通过sentinel连接redis。

RedisConfiguration类

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfiguration {

    private final RedisProperties redisProperties;

    @Autowired
    public RedisConfiguration(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
    }

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return getLettuceConnectionFactory(redisProperties.getDb());
    }

    /**
     *  redisTemplate配置
     */
    @Bean("redisTemplate")
    @Primary
    public StringRedisTemplate redisTemplate() {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory());
        return stringRedisTemplate;
    }

    /**
     * LettuceConnectionFactory 配置
     *
     * @return LettuceConnectionFactory
     */
    private LettuceConnectionFactory getLettuceConnectionFactory(Integer db) {
        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(secondSentinelConfig(), getLettucePoolingClientConfiguration());
        lettuceConnectionFactory.setDatabase(db);
        return lettuceConnectionFactory;
    }

    /**
     * lettuce 连接池配置
     *
     * @return LettucePoolingClientConfiguration
     */
    private LettucePoolingClientConfiguration getLettucePoolingClientConfiguration() {
        LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(redisProperties.getMaxTotal());
        genericObjectPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
        genericObjectPoolConfig.setMinIdle(redisProperties.getMinIdle());
        genericObjectPoolConfig.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
        genericObjectPoolConfig.setTestOnBorrow(redisProperties.getTestOnBorrow());
        builder.poolConfig(genericObjectPoolConfig);
        builder.commandTimeout(Duration.ofSeconds(60));
        builder.shutdownTimeout(Duration.ofMillis(100));
        return builder.build();
    }

    private RedisSentinelConfiguration sentinelConfig() {
        RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration();
        sentinelConfig.setMaster(redisProperties.getSentinelMaster());
        String[] sentinels = redisProperties.getSentinelNodes().split("\\|");
        List<RedisNode> list = new ArrayList<>();
        for (String sentinel : sentinels) {
            String[] nodes = sentinel.split(":");
            list.add(new RedisNode(nodes[0], Integer.parseInt(nodes[1])));
        }
        sentinelConfig.setSentinels(list);
        sentinelConfig.setPassword(RedisPassword.of(redisProperties.getSentinelPassword()));
        return sentinelConfig;
    }

}

RedisProperties类

@Data //lombok注解 自动生成getter setter
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {

    private Integer maxIdle;
    private Integer minIdle;
    private Integer maxTotal;
    private Long maxWaitMillis;
    private Boolean testOnBorrow;
    private Integer timeout;
    private Integer db;
    private String sentinelPassword;
    private String sentinelMaster;
    private String sentinelNodes;

}

application.properties配置

#redis
redis.db = 1
redis.max-idle = 10
redis.min-idle = 10
redis.max-total = 10
redis.max-wait-millis = 10000
redis.test-on-borrow = true
redis.timeout = 5000
#sentinel
redis.sentinel-master = mymaster
redis.sentinel-nodes = 127.0.0.1:26379|127.0.0.1:36379|127.0.0.1:46379
redis.sentinel-password = test
展开阅读全文

没有更多推荐了,返回首页