redis主从复制部署策略+jedis设置主从
生活随笔
收集整理的這篇文章主要介紹了
redis主从复制部署策略+jedis设置主从
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【README】
redis 有3種集群模式,包括 主從, 哨兵, cluster;
本文主要po出 主從;
master?? 192.168.163.201 6382
slave? 192.168.163.202:6382
?
【1】從機(jī) 202:6382 的 redis.conf 配置
只需要編寫? slaveof 192.168.163.201 6382 即可
################################# REPLICATION ################################## Master-Slave replication. Use slaveof to make a Redis instance a copy of # another Redis server. A few things to understand ASAP about Redis replication. # # 1) Redis replication is asynchronous, but you can configure a master to # stop accepting writes if it appears to be not connected with at least # a given number of slaves. # 2) Redis slaves are able to perform a partial resynchronization with the # master if the replication link is lost for a relatively small amount of # time. You may want to configure the replication backlog size (see the next # sections of this file) with a sensible value depending on your needs. # 3) Replication is automatic and does not need user intervention. After a # network partition slaves automatically try to reconnect to masters # and resynchronize with them. #slaveof 192.168.163.201 6382【2】 命令行連接到從庫(kù)
// 連接到從庫(kù) [root@centos202 6382]# /usr/local/redis-cluster/bin/redis-cli -h 192.168.163.202 -p 6382 192.168.163.202:6382> // 查看從庫(kù)key 192.168.163.202:6382> keys * 1) "balance" 2) "debt" 3) "k1" // master節(jié)點(diǎn) 新增k2后,在從庫(kù)再次查看key 192.168.163.202:6382> keys * 1) "balance" 2) "debt" 3) "k2" 4) "k1" 192.168.163.202:6382> get k2 "v2"// 獲取 副本信息,很明顯, 202:6282是從庫(kù);201:6282是主庫(kù); 192.168.163.202:6382> info replication # Replication role:slave master_host:192.168.163.201 master_port:6382 master_link_status:up master_last_io_seconds_ago:9 master_sync_in_progress:0 slave_repl_offset:487 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0【3】把從庫(kù)變?yōu)楠?dú)立的主庫(kù)
在客戶端執(zhí)行 slaveof no one
192.168.163.202:6382> slaveof no one OK 192.168.163.202:6382> info replication # Replication role:master connected_slaves:0 master_repl_offset:543 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 192.168.163.202:6382> 192.168.163.202:6382> keys * 1) "balance" 2) "debt" 3) "k2" 4) "k1" 192.168.163.202:6382> exit【4】jedis 設(shè)置主從
/*** redis 主從庫(kù)測(cè)試* @author tr*/ public class MasterSlaveTest {public static void main(String[] args) {Jedis master = new Jedis("192.168.163.201", 6382); // 主庫(kù)Jedis slave = new Jedis("192.168.163.202", 6382); // 從庫(kù) slave.slaveof("192.168.163.201", 6382); // 設(shè)置202 是 201的從庫(kù) /* 寫入主庫(kù) */master.set("master1", "v1");/* 從從庫(kù)讀取 */String result = slave.get("master1");System.out.println("從庫(kù)讀取的值=" + result); } }?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的redis主从复制部署策略+jedis设置主从的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 图片论坛怎么优化(图片论坛怎么优化画质)
- 下一篇: redis连接池