Redis集群搭建(一主多从)

21 篇文章 0 订阅

本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本,Redis2.8及以上版本。

一、安装redis

安装方法,可以参考:http://blog.csdn.net/codetomylaw/article/details/40395905

目的:搭建一主二从环境,192.168.18.19(主)   192.168.18.178 (从) 192.168.18.179(从) 


部署redis完毕,所有端口都使用的默认端口6379

配置文件目录:/etc/redis 

备份目录: /opt/soft/redis/backup 

日志目录:  /opt/soft/redis/log 


192.168.18.178/179从服务器需要配置slaveof 192.168.18.19 6379


配置slave-priority,默认是100

178 配置为50,179配置为100

当Master挂掉的时候Sentinel会优先选择slave-priority值较小的作为新的Master。


验证:当往Master写入数据的时候,slave可以同步数据,说明环境搭建完成。


二、配置sentinel

参考:http://blog.csdn.net/codetomylaw/article/details/41011543


配置成守护进程

daemonize yes

配置日志输出目录

logfile "/opt/soft/redis/log/sentinel.log” 


配置monitor的Master redis

sentinel monitor mymaster 192.168.18.19 6379 2 


部署了3个sentinel进程,分别在192.168.18.19、 192.168.18.178、192.168.18.179上,配置文件sentinel.conf 相同。


你会发现:sentinel.conf 配置文件是动态的,文件多了如下内容

sentinel known-slave mymaster 192.168.18.179 6379

sentinel known-sentinel mymaster 192.168.18.179 26379 9a00533bb48c8c860b0f373d9594b5126d1a1db9

sentinel known-sentinel mymaster 192.168.18.19 26379 738264461625b563f367683b234d9b4c1d971972

sentinel current-epoch 0  


三、程序验证

	Set<String> sentinels = new HashSet<String>();
        sentinels.add(new HostAndPort("192.168.18.19", 26379).toString());
        sentinels.add(new HostAndPort("192.168.18.178", 26379).toString());
        sentinels.add(new HostAndPort("192.168.18.179", 26379).toString());
        JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster", sentinels);
        System.out.println("Current master: " + sentinelPool.getCurrentHostMaster().toString());
        
        Jedis master = sentinelPool.getResource();
        master.set("username","jack");
        sentinelPool.returnResource(master);
        
        Jedis master2 = sentinelPool.getResource();
        String value = master2.get("username");
        System.out.println("username: " + value);

能正常从redis获取信息说明配置完成。


四、内存升级方案

1)停掉一个从,升级内存,挂回

2)停掉另一个从,升级内存,挂回

3)停掉主,从两个从中选出一个主,升级内存,挂回  (以前的主变成了从)


五、多主多从集群搭建

待完善



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值