linux下配置redis4.0.2主从复制以及高可用
一、环境
三台服务器分别为:
172.28.18.75/172.28.18.103/172.28.18.104
在三台服务器上分别部署一个redis节点以及一个sentinel节点
二、主从复制配置
1、172.28.18.75为主redis配置在25700端口
vim /etc/redis/redis-25700.conf
主要配置项如下:
bind 172.28.18.75
port 25700
#从节点访问主节点的密码
masterauth "password"
#redis客户端访问redis的密码
requirepass "password"
#保护模式开启
protected-mode yes
daemonize yes
dir "/etc/redis"
logfile "25700.log"
2、172.28.18.103配置从redis在25701
bind 172.28.18.103
port 25701
protected-mode yes
daemonize yes
#从节点访问主节点的密码
masterauth "password"
#redis客户端访问redis的密码
requirepass "password"
dir "/etc/redis"
logfile "25707.log"
slaveof 172.28.18.75 25700
3、172.28.18.104配置从redis在25701
bind 172.28.18.104
port 25701
protected-mode yes
daemonize yes
#从节点访问主节点的密码
masterauth "password"
#redis客户端访问redis的密码
requirepass "password"
dir "/etc/redis"
logfile "25700.log"
slaveof 172.28.18.75 25700
三、启动redis
1、启动主节点redis
redis-server redis-25700.conf
查看日志
tail -f 25700.log

启动成功
2、启动从节点redis
在172.28.18.103上
redis-server redis-25701.conf
查看日志
tail -f 25701.log

链接主节点172.28.18.75:25700
同步开始
从主节点全盘复制
复制完成
同时查看主节点redis日志

从节点172.2818.103请求同步数据
开始后台同步数据
同步到从节点172.28.18.103成功
按照上述方法启动172.28.18.104的从节点redis


至此,主从节点启动完毕。
三、验证结果
在主节点172.28.18.75上,连接redis
redis-cli -h 172.28.18.75 -p 25700 -a password

插入一个key
在从节点172.28.18.103和172.28.18.104上 ,分别连接redis,并查看这个key

有数据,则同步成功。
四、高可用redis sentinel配置
1、分别在三台服务器上配置3个sentinel节点
172.28.18.75
vim sentinel-26379.conf
bind 172.28.18.75
port 26379
dir "/etc/redis"
logfile "sentinel-26379.log"
protected-mode yes
daemonize yes
#监控哪个主节点信息
sentinel monitor mymaster 172.28.18.75 25700 2
#5秒没有响应,则视为主观下线
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 80000
#连接主节点的密码,(在主节点redis配置文件里设置)
sentinel auth-pass mymaster password
172.28.18.103
vim sentinel-26379.conf
bind 172.28.18.103
port 26379
dir "/etc/redis"
logfile "sentinel-26379.log"
protected-mode yes
daemonize yes
#监控主节点信息
sentinel monitor mymaster 172.28.18.75 25700 2
#5秒没有响应,则视为主观下线
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 80000
#连接主节点的密码,(在主节点redis配置文件里设置)
sentinel auth-pass mymaster password
172.28.18.104
vim sentinel-26379.conf
bind 172.28.18.104
port 26379
dir "/etc/redis"
logfile "sentinel-26379.log"
protected-mode yes
daemonize yes
#监控主节点信息,因为是3个sentilnel节点,所以设置投票选举票数为2,一般sentinel节点设置为奇数个,选举票数为:(节点总数/2)+1
sentinel monitor mymaster 172.28.18.75 25700 2
#5秒没有响应,则视为主观下线
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 80000
#连接主节点的密码,(在主节点redis配置文件里设置)
sentinel auth-pass mymaster password
2、启动3个sentinel节点
172.28.18.75
redis-sentinel sentinel-26379.conf
查看日志
tail -f sentinel-26379.log

通过连接主节点redis,并执行info信息得到Relication段里的从节点信息,并添加到自己的sentinel配置文件中
此时打开sentinel-26379.conf,里面多了从节点信息配置

再同样启动172.28.18.103,172.28.18.104两个sentinel节点

监控主节点172.28.18.75
添加两个从节点redis信息到配置文件中
感知172.28.18.75的sentinel节点信息,并添加到配置文件中
此时的172.28.18.75上的sentinel节点也感知到了103节点的信息,并添加到配置文件中

103和75的sentinel-26379.conf,里面多了感知的sentilnel节点信息配置


再启动104的sentinel节点

也增加了2个从redis节点信息以及2个sentinel节点信息,并添加到配置文件中
此时,75和103的sentinel节点也感知到了104sentinel节点信息,并添加到配置文件中


至此,三个sentilnel节点启动完毕。
3、验证高可用
在172.28.18.75上停掉主redis节点

过5秒左右,查看3台服务器的sentinel日志如下:
172.28.18.75

1、172.28.18.75 25700 主观下线
2、172.28.18.75 25700 经过投票成为客观下线
3、此sentinel节点尝试成为领导者节点,发出投票
4、其余两个节点同意投票给它,它成为了领导者节点。
5、将104从redis节点选为新的主redis节点,并进行切换操作。
6、将103从redis节点设置为104新的主redis节点。并重新同步数据。
172.28.103和172.28.18.104日志:


1、172.28.18.75 25700 主观下线
2、172.28.18.75 25700 经过投票成为客观下线
3、收到75发来的请求成为领导者节点投票请求,并同意投票给它,75成为了领导者节点。
4、75完成切换操作后,重新同步配置数据
此时104的redis晋升为主redis节点,连接redis验证

103节点还是从redis节点,只不过主节点变为104节点,连接redis验证

此时,再把75上被停掉的redis重新启动起来,将成为104的从redis节点
104上的sentinel日志如下

104上连接redis验证

此时,75成了从redis节点
至此,redis高可用成功设置完毕。
4、JAVA连接sentinel实例
新建一个maven项目,在pom里加入jedis依赖和logback依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
新建一个class如下
public class RedisSentinelFailoverTest {
private static Logger logger=LoggerFactory.getLogger(RedisSentinelFailoverTest.class);
public static void main(String[] args) throws InterruptedException{
String masterName="mymaster";
Set<String> sentinels=new HashSet<String>();
sentinels.add("172.28.18.75:26379");
sentinels.add("172.28.18.103:26379");
sentinels.add("172.28.18.104:26379");
JedisSentinelPool jedisSentinelPool=new JedisSentinelPool(masterName,sentinels,"password");
while(true){
Jedis jedis=null;
try{
jedis=jedisSentinelPool.getResource();
int index=new Random().nextInt(100000);
String key="k-"+index;
String value="v-"+index;
jedis.set(key, value);
logger.info("{} values is {}",key,value,jedis.get(key));
TimeUnit.SECONDS.sleep(1);
}
catch(Exception e){
logger.error(e.getMessage(),e);
TimeUnit.SECONDS.sleep(1);
}
finally{
if(jedis!=null){
jedis.close();
}
}
}
}
}
运行测试

从连接池里获取到主redis节点104
linux下配置redis4.0.2主从复制以及高可用的更多相关文章
- Linux下安装redis-4.0.10
1.下载redis-4.0.10 在redis官网(https://redis.io/download)下载redis-4.0.10 2.将安装包上传至Linux服务器 在Linux服务器根目录下创建 ...
- Linux下配置OpenCV1.0环境
自己一直嚷嚷着打算学学图像识别,识别个简单的,车牌号,验证码之类的,之前查过资料,OpenCV可以实现.昨天花了一个下午终于配置好环境了,今天写下总结. OpenCV这一名称包含了Open和Compu ...
- Linux下安装Redis4.0版本(简便方法)
Redis介绍: Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...
- Linux下配置Squid基础教程
Linux下配置Squid基础教程 本视频高清下载地址:http://down.51cto.com/data/437529 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处 ...
- linux下mysql基于mycat做主从复制和读写分离之基础篇
Linux下mysql基于mycat实现主从复制和读写分离1.基础设施 两台虚拟机:172.20.79.232(主) 172.20.79.233(从) 1.1软件设施 mysql5.6.39 , my ...
- linux下配置mysql默认编码utf8
linux下配置mysql默认编码utf8 下面是需要在对应地方加入的配置 [client] default-character-set=utf8 [mysqld] character-set-ser ...
- Linux下配置Lamp
linux下配置lamp步骤: 一.快速安装Apache+PHP5+MySql 先更新: # yum update 然后安装LAMP环境:(163的yum源上只有php5.1.6 mysql 5.0. ...
- Linux下配置PHP开发环境
转载于: http://www.uxtribe.com/php/405.html 该站下有系列PHP文章. 在Linux下搭建PHP环境比Windows下要复杂得多.除了安装Apache,PHP等软件 ...
- 一步一步教你如何在linux下配置apache+tomcat(转)
一步一步教你如何在linux下配置apache+tomcat 一.安装前准备. 1. 所有组件都安装到/usr/local/e789目录下 2. 解压缩命令:tar —vxzf 文件名(. ...
随机推荐
- RPi 3B Aduio 3.5mm output
/********************************************************************** * RPi 3B Aduio 3.5mm output ...
- webpack的入门教程
webpack是模块打包工具/前端资源加载.它是根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. webpack可以将css.less.js转换为一个静态文件,减少了页 ...
- Redis之在Linux上安装和简单的使用
我只是一个搬运工 Redis之在Linux上安装和简单的使用https://blog.csdn.net/qq_20989105/article/details/76390367 一.安装gcc 1.R ...
- Linux搭建禅道
1.开源版安装包下载(64位的下载64位,32位的选中32位) [root@iZbp~]# wget http://dl.cnezsoft.com/zentao/9.0.1/ZenTaoPMS.9.0 ...
- best practices for designing web api
restful why: meaningful This will be improve efficiency , less documents , just read the code auto g ...
- mysql 分组排序前n + 长表转宽表
MySQL数据库优化的八种方式(经典必看) 建表 CREATE TABLE if not EXISTS `bb` ( `id` int not null primary key auto_increm ...
- Web Service入门简介(一个简单的WebService示例)
Web Service入门简介 一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从I ...
- Java8 新特性学习
摘自:https://blog.csdn.net/shuaicihai/article/details/72615495 Lambda 表达式 Lambda 是一个匿名函数,我们可以把 Lambda ...
- Hello ThreadPoolExecutor
ThreadPoolExecutor创建: public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliv ...
- Excel--按内容分页打印
当我们有这样一张表,需要按不同城市分页打印,每页带标题行,可按以下步骤:1.点击城市一列任一单元格,点击“开始”——>“排序和筛选”(升序): 2.点击“数据”-->“分类汇总”: 分类字 ...