前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >redis集群主从之读写分离

redis集群主从之读写分离

作者头像
一笠风雨任生平
发布2019-08-02 11:07:26
1.9K0
发布2019-08-02 11:07:26
举报
文章被收录于专栏:服务化进程服务化进程

redis集群主从之读写分离

1、集群部署 这里就不详细赘述如何部署主从集群了,一般都是使用slaveOf配置来进行初始化配置。

2、与springboot集成实现读写分离 通过注解实现调用层读写分离,然后根据取模运算来确定访问哪个读库

  • 在springcloud配置中心增加redis配置:

配置读写节点

代码语言:javascript
复制
spring:
  redis:
    database: 0
    pool:
      max-active: 8
      max-idle: 9
      max-wait: -1
      min-idle: 0
    redis-master:
      host: 192.168.1.1
      prot: 6379
      password: 
      testOnBorrow: false
    redis-slave1:
      host: 192.168.1.2
      prot: 6379
      password:
      testOnBorrow: false
    redis-slave2:
      host: 192.168.1.3
      prot: 6379
      password:
      testOnBorrow: false
  • 增加自动配置类

如下:

代码语言:javascript
复制
@Configuration
public class RedisConfiguration {

    @Value("${spring.redis.pool.max-idle}")
    private int maxIdle;
    @Value("${spring.redis.pool.max-active}")
    private int maxTotal;
    @Value("${spring.redis.database}")
    private int index;
    @Value("${spring.redis.pool.max-wait}")
    private long maxWaitMillis;
    

    @Bean(name = "redisMasterTemplate")
    public StringRedisTemplate redisMasterTemplate(@Value("${spring.redis-master.host}") String hostName,
            @Value("${spring.redis-master.port}") int port, @Value("${spring.redis-master.password}") String password, @Value("${spring.redis-master.testOnBorrow}") boolean testOnBorrow) {
        StringRedisTemplate temple = new StringRedisTemplate();
        temple.setConnectionFactory(
                connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));

        return temple;
    }

    @Bean(name = "redisSlave1Template")
    public StringRedisTemplate redisSlave1Template(@Value("${spring.redis-slave1.host}") String hostName,
            @Value("${spring.redis-slave1.port}") int port, @Value("${spring.redis-slave1.password}") String password, @Value("${spring.redis-slave1.testOnBorrow}") boolean testOnBorrow) {
        StringRedisTemplate temple = new StringRedisTemplate();
        temple.setConnectionFactory(
                connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));

        return temple;
    }

    @Bean(name = "redisSlave1Template")
    public List<StringRedisTemplate> redisSlaveTemplate() {
        List<StringRedisTemplate> list = new ArrayList<StringRedisTemplate>();
        list.add(redisSlave1Template());
        list.add(redisSlave2Template());
        return list;
    }

    @Bean(name = "redisSlave2Template")
    public StringRedisTemplate redisSlave1Template(@Value("${spring.redis-slave2.host}") String hostName,
            @Value("${spring.redis-slave2.port}") int port, @Value("${spring.redis-slave2.password}") String password, @Value("${spring.redis-slave2.testOnBorrow}") boolean testOnBorrow) {
        StringRedisTemplate temple = new StringRedisTemplate();
        temple.setConnectionFactory(
                connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));
        return temple;
    }

    public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
            int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow) {
        JedisConnectionFactory jedis = new JedisConnectionFactory();
        jedis.setHostName(hostName);
        jedis.setPort(port);
        if (StringUtils.isNotEmpty(password)) {
            jedis.setPassword(password);
        }
        if (index != 0) {
            jedis.setDatabase(index);
        }
        jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow));
        // 初始化连接pool
        jedis.afterPropertiesSet();
        RedisConnectionFactory factory = jedis;

        return factory;
    }

    public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
        JedisPoolConfig poolCofig = new JedisPoolConfig();
        poolCofig.setMaxIdle(maxIdle);
        poolCofig.setMaxTotal(maxTotal);
        poolCofig.setMaxWaitMillis(maxWaitMillis);
        poolCofig.setTestOnBorrow(testOnBorrow);
        return poolCofig;
    }
}

封装调用接口

代码语言:javascript
复制
@Component
public class StringRedisTemplateProxy {

	@Autowired
	private StringRedisTemplate redisMasterTemplate; 
	
	@Autowired
	private List<StringRedisTemplate> redisSlaveList; 
	
	private int index=0;
	
	public void setValue(String key,String value) {
		redisMasterTemplate.opsForValue().set(key, value);
	}
	
	public String getValue(String key) {
		index++;
		return redisSlaveList.get(index).opsForValue().get(key);
	}
}

这里缺点就是无法做到故障转移,这里就需要用到虚拟ip和脚本定时监控。 1、使用一个虚拟ip指向redis master 2、如果从节点故障,可以采用重试机制来进行一次调用读节点,如果是主节点故障,则需要通过脚本自动将一个从节点切换成主节点,并将虚拟IP绑定在该节点上。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • redis集群主从之读写分离
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档