|
|
Redis 主从高可用配置
|
|
|
-----------------------
|
|
|
|
|
|
## 准备
|
|
|
|
|
|
需要3台主机(Linux):
|
|
|
|
|
|
如:
|
|
|
```
|
|
|
192.168.102.168(主)
|
|
|
192.168.102.162(从)
|
|
|
192.168.102.78(sentinel 监控)
|
|
|
```
|
|
|
|
|
|
## 第一步:安装Redis
|
|
|
|
|
|
在以上三台安装,[redis3.0.5](http://redis.io/download),安装步骤如下:
|
|
|
|
|
|
```
|
|
|
$ wget http://download.redis.io/releases/redis-3.0.5.tar.gz
|
|
|
$ tar xzf redis-3.0.5.tar.gz
|
|
|
$ cd redis-3.0.5
|
|
|
$ make
|
|
|
|
|
|
```
|
|
|
测试是否安装成功
|
|
|
|
|
|
```
|
|
|
$ src/redis-server
|
|
|
$ src/redis-cli
|
|
|
redis> set foo bar
|
|
|
OK
|
|
|
redis> get foo
|
|
|
"bar
|
|
|
```
|
|
|
一切OK,就可以进行下一步
|
|
|
|
|
|
## 第二步:配置参数
|
|
|
|
|
|
* `192.168.102.168`(主)的`redis.conf`,配置如下:
|
|
|
|
|
|
```
|
|
|
#主要是该主机已启动一个redis实例,占用了6379端口
|
|
|
port 6377
|
|
|
```
|
|
|
|
|
|
* `192.168.102.168`(从)的`redis.conf`,配置如下:
|
|
|
|
|
|
```
|
|
|
#配置使端口一致
|
|
|
port 6377
|
|
|
#配置如下
|
|
|
slaveof 192.168.102.168 6377
|
|
|
```
|
|
|
|
|
|
* `192.168.102.78`(监控)的`sentinel.conf`,配置如下:
|
|
|
|
|
|
```
|
|
|
##redis-0
|
|
|
##sentinel实例之间的通讯端口
|
|
|
port 26379
|
|
|
#mymaster
|
|
|
sentinel monitor mymaster 192.168.102.168 6377 1
|
|
|
sentinel down-after-milliseconds mymaster 5000
|
|
|
sentinel failover-timeout mymaster 900000
|
|
|
sentinel parallel-syncs mymaster 2
|
|
|
```
|
|
|
|
|
|
## 第三步:启动服务
|
|
|
|
|
|
在主和从分别执行:`nohup ./src/redis-server ./redis.conf &`。
|
|
|
|
|
|
在监控的主机上执行:`nohup ./src/redis-sentinel ./sentinel.conf --sentinel &`。
|
|
|
|
|
|
查看是否已生效
|
|
|
|
|
|
主的信息
|
|
|
```
|
|
|
redis-cli -h 192.168.102.168 -p 6377 info Replication
|
|
|
```
|
|
|
|
|
|
输出
|
|
|
|
|
|
```
|
|
|
# Replication
|
|
|
role:master
|
|
|
connected_slaves:1
|
|
|
slave0:ip=192.168.102.162,port=6377,state=online,offset=3256260,lag=1
|
|
|
master_repl_offset:3256260
|
|
|
repl_backlog_active:1
|
|
|
repl_backlog_size:1048576
|
|
|
repl_backlog_first_byte_offset:2207685
|
|
|
repl_backlog_histlen:1048576
|
|
|
|
|
|
```
|
|
|
|
|
|
从的信息
|
|
|
```
|
|
|
redis-cli -h 192.168.102.162 -p 6377 info Replication
|
|
|
```
|
|
|
|
|
|
输出
|
|
|
```
|
|
|
# Replication
|
|
|
role:slave
|
|
|
master_host:192.168.102.168
|
|
|
master_port:6377
|
|
|
master_link_status:up
|
|
|
master_last_io_seconds_ago:0
|
|
|
master_sync_in_progress:0
|
|
|
slave_repl_offset:3355550
|
|
|
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
|
|
|
```
|
|
|
|
|
|
|
|
|
查看监控
|
|
|
```
|
|
|
redis-cli -h 192.168.102.78 -p 26379 info Sentinel
|
|
|
```
|
|
|
|
|
|
输出
|
|
|
|
|
|
```
|
|
|
# Sentinel
|
|
|
sentinel_masters:1
|
|
|
sentinel_tilt:0
|
|
|
sentinel_running_scripts:0
|
|
|
sentinel_scripts_queue_length:0
|
|
|
master0:name=mymaster,status=ok,address=192.168.102.168:6377,slaves=1,sentinels=1
|
|
|
```
|
|
|
|
|
|
|
|
|
表示主从配置成功!
|
|
|
|
|
|
## 第四步:故障演练,关闭主redis主机
|
|
|
|
|
|
```
|
|
|
redis-cli -h 192.168.102.168 -p 6377 shutdown #表示把192.168.102.168 这台redis 关闭
|
|
|
```
|
|
|
|
|
|
这时可以看到
|
|
|
162这台从变成了主,当再次重启168,168变成从。
|
|
|
|
|
|
## 第五步:jedis调用实例
|
|
|
|
|
|
maven配置
|
|
|
|
|
|
```xml
|
|
|
<dependency>
|
|
|
<groupId>redis.clients</groupId>
|
|
|
<artifactId>jedis</artifactId>
|
|
|
<version>2.7.2</version>
|
|
|
<type>jar</type>
|
|
|
<scope>compile</scope>
|
|
|
</dependency>
|
|
|
```
|
|
|
|
|
|
```java
|
|
|
|
|
|
Set<String> sentinels = new HashSet<String>(16);
|
|
|
sentinels.add("192.168.102.168:6377");
|
|
|
sentinels.add("192.168.102.162:6377");
|
|
|
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
|
|
config.setMaxTotal(32);
|
|
|
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("def_master",sentinels,config,"012_345^678-90");
|
|
|
Jedis jedis = jedisSentinelPool.getResource();
|
|
|
try{
|
|
|
jedis.set("key","value");
|
|
|
} finally {
|
|
|
jedisSentinelPool.returnResource(jedis);
|
|
|
}
|
|
|
jedisSentinelPool.close();
|
|
|
```
|
|
|
|
|
|
参考文档:
|
|
|
|
|
|
* [Redis Sentinel机制与用法(一)](http://segmentfault.com/a/1190000002680804)
|
|
|
* [Redis Sentinel机制与用法(二)](http://segmentfault.com/a/1190000002685515)
|
|
|
* [redis sentinel 主从切换(failover)解决方案,详细配置](http://blog.csdn.net/pi9nc/article/details/17735653)
|
|
|
Redis 主从高可用配置
|
|
|
-----------------------
|
|
|
|
|
|
## 准备
|
|
|
|
|
|
需要3台主机(Linux):
|
|
|
|
|
|
如:
|
|
|
```
|
|
|
192.168.102.162(主)
|
|
|
192.168.102.168(从)
|
|
|
192.168.102.78(sentinel 监控)
|
|
|
```
|
|
|
|
|
|
## 第一步:安装Redis
|
|
|
|
|
|
在以上三台安装,[redis3.0.5](http://redis.io/download),安装步骤如下:
|
|
|
|
|
|
```
|
|
|
$ wget http://download.redis.io/releases/redis-3.0.5.tar.gz
|
|
|
$ tar xzf redis-3.0.5.tar.gz
|
|
|
$ cd redis-3.0.5
|
|
|
$ make
|
|
|
|
|
|
```
|
|
|
测试是否安装成功
|
|
|
|
|
|
```
|
|
|
$ src/redis-server
|
|
|
$ src/redis-cli
|
|
|
redis> set foo bar
|
|
|
OK
|
|
|
redis> get foo
|
|
|
"bar
|
|
|
```
|
|
|
一切OK,就可以进行下一步
|
|
|
|
|
|
## 第二步:配置参数
|
|
|
|
|
|
* `192.168.102.162`(主)的`redis.conf`,配置如下:
|
|
|
|
|
|
```
|
|
|
#主要是该主机已启动一个redis实例,占用了6379端口
|
|
|
port 6377
|
|
|
```
|
|
|
|
|
|
* `192.168.102.168`(从)的`redis.conf`,配置如下:
|
|
|
|
|
|
```
|
|
|
#配置使端口一致
|
|
|
port 6377
|
|
|
#配置如下
|
|
|
slaveof 192.168.102.162 6377
|
|
|
```
|
|
|
|
|
|
* `192.168.102.78`(监控)的`sentinel.conf`,配置如下:
|
|
|
|
|
|
```
|
|
|
##redis-0
|
|
|
##sentinel实例之间的通讯端口
|
|
|
port 26379
|
|
|
#mymaster
|
|
|
sentinel monitor mymaster 192.168.102.162 6377 1
|
|
|
sentinel down-after-milliseconds mymaster 5000
|
|
|
sentinel failover-timeout mymaster 900000
|
|
|
sentinel parallel-syncs mymaster 2
|
|
|
```
|
|
|
|
|
|
## 第三步:启动服务
|
|
|
|
|
|
在主和从分别执行:`nohup ./src/redis-server ./redis.conf &`。
|
|
|
|
|
|
在监控的主机上执行:`nohup ./src/redis-sentinel ./sentinel.conf --sentinel &`。
|
|
|
|
|
|
查看是否已生效
|
|
|
|
|
|
主的信息
|
|
|
```
|
|
|
redis-cli -h 192.168.102.162 -p 6377 info Replication
|
|
|
```
|
|
|
|
|
|
输出
|
|
|
|
|
|
```
|
|
|
# Replication
|
|
|
role:master
|
|
|
connected_slaves:1
|
|
|
slave0:ip=192.168.102.168,port=6377,state=online,offset=3256260,lag=1
|
|
|
master_repl_offset:3256260
|
|
|
repl_backlog_active:1
|
|
|
repl_backlog_size:1048576
|
|
|
repl_backlog_first_byte_offset:2207685
|
|
|
repl_backlog_histlen:1048576
|
|
|
|
|
|
```
|
|
|
|
|
|
从的信息
|
|
|
```
|
|
|
redis-cli -h 192.168.102.168 -p 6377 info Replication
|
|
|
```
|
|
|
|
|
|
输出
|
|
|
```
|
|
|
# Replication
|
|
|
role:slave
|
|
|
master_host:192.168.102.162
|
|
|
master_port:6377
|
|
|
master_link_status:up
|
|
|
master_last_io_seconds_ago:0
|
|
|
master_sync_in_progress:0
|
|
|
slave_repl_offset:3355550
|
|
|
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
|
|
|
```
|
|
|
|
|
|
|
|
|
查看监控
|
|
|
```
|
|
|
redis-cli -h 192.168.102.78 -p 26379 info Sentinel
|
|
|
```
|
|
|
|
|
|
输出
|
|
|
|
|
|
```
|
|
|
# Sentinel
|
|
|
sentinel_masters:1
|
|
|
sentinel_tilt:0
|
|
|
sentinel_running_scripts:0
|
|
|
sentinel_scripts_queue_length:0
|
|
|
master0:name=mymaster,status=ok,address=192.168.102.162:6377,slaves=1,sentinels=1
|
|
|
```
|
|
|
|
|
|
|
|
|
表示主从配置成功!
|
|
|
|
|
|
## 第四步:故障演练,关闭主redis主机
|
|
|
|
|
|
```
|
|
|
redis-cli -h 192.168.102.162 -p 6377 shutdown #表示把192.168.102.162 这台redis 关闭
|
|
|
```
|
|
|
|
|
|
这时可以看到
|
|
|
162这台从变成了主,当再次重启168,168变成从。
|
|
|
|
|
|
## 第五步:jedis调用实例
|
|
|
|
|
|
maven配置
|
|
|
|
|
|
```xml
|
|
|
<dependency>
|
|
|
<groupId>redis.clients</groupId>
|
|
|
<artifactId>jedis</artifactId>
|
|
|
<version>2.7.2</version>
|
|
|
<type>jar</type>
|
|
|
<scope>compile</scope>
|
|
|
</dependency>
|
|
|
```
|
|
|
|
|
|
```java
|
|
|
|
|
|
Set<String> sentinels = new HashSet<String>(16);
|
|
|
sentinels.add("192.168.102.162:6377");
|
|
|
sentinels.add("192.168.102.166:6377");
|
|
|
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
|
|
config.setMaxTotal(32);
|
|
|
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("def_master",sentinels,config,"012_345^678-90");
|
|
|
Jedis jedis = jedisSentinelPool.getResource();
|
|
|
try{
|
|
|
jedis.set("key","value");
|
|
|
} finally {
|
|
|
jedisSentinelPool.returnResource(jedis);
|
|
|
}
|
|
|
jedisSentinelPool.close();
|
|
|
```
|
|
|
|
|
|
参考文档:
|
|
|
|
|
|
* [Redis Sentinel机制与用法(一)](http://segmentfault.com/a/1190000002680804)
|
|
|
* [Redis Sentinel机制与用法(二)](http://segmentfault.com/a/1190000002685515)
|
|
|
* [redis sentinel 主从切换(failover)解决方案,详细配置](http://blog.csdn.net/pi9nc/article/details/17735653)
|
|
|
* [Redis Sentinel:集群Failover解决方案](http://shift-alt-ctrl.iteye.com/blog/1884370) |
|
|
\ No newline at end of file |
...
|
...
|
|