redis集群主从之读写分离

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

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

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

配置读写节点

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
  • 增加自动配置类

如下:

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

封装调用接口

@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绑定在该节点上。

redis集群主从之读写分离的更多相关文章

  1. (转)基于Redis Sentinel的Redis集群(主从&Sharding)高可用方案

    转载自:http://warm-breeze.iteye.com/blog/2020413 本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedi ...

  2. 基于Redis Sentinel的Redis集群(主从Sharding)高可用方案(转)

    本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...

  3. 基于Redis Sentinel的Redis集群(主从&Sharding)高可用方案

    本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...

  4. 查看Redis集群主从对应关系工具

    工具的作用: 1)比"cluster nodes"更为直观的显示结果 2)指出落在同一个IP上的master 3)指出落在同一个IP上的master和slave对 运行效果图: 源 ...

  5. Redis Sentinel的Redis集群(主从&Sharding)高可用方案

    在不使用redis3.0之后版本的情况下,对于redis服务端一般是采用Sentinel哨兵模式,也就是一主多备的方式. 这里,先抛出三个问题, 问题1:单节点宕机数据丢失?问题2:多节点(节点间没有 ...

  6. 基于Keepalived高可用集群的MariaDB读写分离机制实现

    一 MariaDB读写分离机制 在实现读写分离机制之前先理解一下三种主从复制方式:1.异步复制:MariaDB默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库 ...

  7. linux上使用amoeba实现MySql集群,以及读写分离,主从复制

    一.由于是MySql集群,所以就不可能只有一个MySql,需要多个MySql,具体安装步骤,可以参考http://www.cnblogs.com/ywzq/p/4882140.html这个地址进行安装 ...

  8. redis集群主从集群搭建、sentinel(哨兵集群)配置以及Jedis 哨兵模式简要配置

    前端时间项目上为了提高平台性能,为应用添加了redis缓存,为了提高服务的可靠性,redis部署了高可用的主从缓存,主从切换使用的redis自带的sentinel集群.现在权作记录.

  9. redis集群主从中断,报io过高 不错

    问题原因:1.由于这个集群redis操作非常频繁,1分钟操作数据达到1-2G,所有自动aof非常频繁,主从复制打包rdb也非常频繁,之前配置已经无法满足要求报异常如下6943:M 19 Jul 20: ...

  10. Redis集群-主从模式

    1.架构设计 集群在单台主机上模拟搭建6个节点(3主3从的集群): 2.配置 创建与端口相同的文件夹存储Redis配置文件和持久化文件. 目录如下: 每个节点配置文件如下: 节点1: bind 192 ...

随机推荐

  1. Built-in COM has been disabled via a feature switch.

    .net 6.0 开始默认关闭com组件 使用时会出现以下信息 Built-in COM has been disabled via a feature switch. See https://aka ...

  2. python UI自动化,怎么在控制台调试代码?

    Chrom 控制台调试脚本 http://testingpai.com/article/1606720137383 可以在任意网页按 F12 进入开发者工具,选择 console 输入 JS 代码: ...

  3. injectionIII iOS代码注入工具(下)

    injectionIII iOS代码注入工具(下) 本文将解决如何使用injectionIII对主页热重载,如果对injectionIII不了解的同学请回到上篇查看 Vaccine 简单地说Vacci ...

  4. 了解Microsoft Media Foundation

    关于Microsoft Media Foundation 是什么 Microsoft Media Foundation是用来处理(创建.修改.传输.合成)多媒体数据(音视频)的一个平台. 有什么用 M ...

  5. Android 自定义带动画的柱状图

    功能分析 假设要使用柱状图展示用户一周的数据,通用的做法是对接三方图表SDK或者自己通过代码绘制. 1.三方SDK通常包体较大,且定制性差,对特定的UI需求兼容性差; 2.自己绘制,比较复杂,而且要考 ...

  6. 七牛云 + PicGo

    下载PicGo https://github.com/Molunerfinn/PicGo/releases/tag/v2.3.1 七牛云配置 1.AccessKey和SecretKey:可以在七牛云控 ...

  7. 全志A40i+Logos FPGA开发板(4核ARM Cortex-A7)硬件说明书(下)

    前 言 本文档主要介绍板卡硬件接口资源以及设计注意事项等内容,测试板卡为创龙科技旗下的全志A40i+Logos FPGA开发板. 核心板的ARM端和FPGA端的IO电平标准一般为3.3V,上拉电源一般 ...

  8. yb课堂实战之LoginInterceptor注册和放行路径 《十二》

    LoginInterceptor 拦截器注册和路径校验配置 继承WebMvcConfigurer 配置拦截路径和放行路径 InterceptorConfig.java package net.ybcl ...

  9. 探究kubernetes 探针参数periodSeconds和timeoutSeconds

    探究kubernetes 探针参数 periodSeconds和timeoutSeconds 问题起源 kubernetes probes的配置中有两个容易混淆的参数,periodSeconds和ti ...

  10. P2984

    [USACO10FEB]Chocolate Giving S 题意描述 Farmer John有B头奶牛(1<=B<=25000),有N(2*B<=N<=50000)个农场,编 ...