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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值