崛起于Springboot2.X之哨兵模式Redis(16)

本文详细介绍了如何在Spring Boot应用中配置和使用Redis哨兵模式,包括配置文件的修改及必要的Java配置类实现。通过示例展示了如何避免常见误解,如区分Redis和Jedis的集成。

介绍:springboot的哨兵模式如果只是使用redis的情况下,在单机redis 的前提下,直接在配置文件添加几行配置就可以了,因为springboot有自动配置加载的功能就可以了

1、把单机的redis中的配置文件的单机redis注释:

#spring.redis.database=0
#spring.redis.host=localhost
#spring.redis.port=6379
#spring.redis.password=
#spring.redis.timeout=10000ms

注释掉之后,改为:

#redis哨兵模式
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.1.20:26379,192.168.1.20:36379,192.168.1.20:46379

添加配置类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisSentinelConfig extends CachingConfigurerSupport {

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

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

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

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

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

    @Value("${spring.redis.sentinel.nodes}")
    private String redisNodes;

    @Value("${spring.redis.sentinel.master}")
    private String master;

    //redis哨兵配置
    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(){
        RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
        String[] host = redisNodes.split(",");
        for(String redisHost : host){
            String[] item = redisHost.split(":");
            String ip = item[0];
            String port = item[1];
            configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
        }
        configuration.setMaster(master);
        return configuration;
    }
    //连接redis的工厂类
    @Autowired
    private JedisPoolConfig jedisPoolConfig;

    @Autowired
    private RedisSentinelConfiguration redisSentinelConfiguration;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory =
                new JedisConnectionFactory(redisSentinelConfiguration, jedisPoolConfig);
        return jedisConnectionFactory;
    }

    //配置RedisTemplate,设置添加序列化器,key 使用string序列化器,value 使用Json序列化器,还有一种简答的设置方式,改变defaultSerializer对象的实现。
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    @Bean
    public RedisTemplate<Object, Object> redisTemplate() {
        //StringRedisTemplate的构造方法中默认设置了stringSerializer
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        //设置开启事务
        template.setEnableTransactionSupport(true);
        //set key serializer
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);

        template.setConnectionFactory(jedisConnectionFactory());
        template.afterPropertiesSet();
        return template;
    }

}

这样我们的springboot与redis的哨兵模式搭建成功了。

 

2、疑问:

我们pom只添加了redis依赖,并没有jedis!!!没有jedis,文中出现的jedis是我们依赖redis中里面的java类,不是单独集成jedis的,而且使用RedisTemplate这样就可以使用哨兵模式了,springboot会自动分配处理。

希望大家不要把集成redis和集成jedis看成一样的,如果有问题,请大家帮忙提出来留言,我会更改的!!!

 

转载于:https://my.oschina.net/mdxlcj/blog/1854513

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值