一、Redis 安装配置

1.下载redis安装包 wget http://download.redis.io/releases/redis-4.0.9.tar.gz

2.解压安装包 tar -zxvf redis-4.0.9.tar.gz -C /usr/local/

3.安装gcc依赖

先通过gcc -v是否有安装gcc,如果没有安装,执行命令sudo yum install -y gcc

4.cd到redis的解压目录下,并执行

cd /usr/local/redis-4.0.9/ 此处目录根据下载的redis版本及解压路径调整

5.编译安装

make MALLOC=libc

将/usr/local/redis-4.0.9/src目录下的文件加到/usr/local/bin目录

cd src && make install

6.测试是否安装成功

cd /usr/local/redis-4.0.9/src/

./redis-server

二、Redis主从复制配置

master的redis.conf文件(其余是默认设置)

port 6379
daemonize yes
# 这个文件夹要改成自己的目录
dir "/home/redis/redis_master_s"

slaver1和slaver2的redis.conf文件

port
# 主服务器端口为6379
slaveof 127.0.0.1
dir "/home/redis/redis_slaver1_s"
port 6377
# 主服务器端口为6379
slaveof 127.0.0.1 6379
dir "/home/redis/redis_slaver2_s"

这个主从服务器就配置好了。

启动服务

./redis-server redis.conf

此时Redis的主从复制配置完毕,主从复制的功能: 
1. 主服务器写入,从服务器可以读取到 
2. 从服务器不能写入

三、Redis的哨兵模式配置

sentinel是哨兵,用于监视主从服务器的运行状况,如果主服务器挂掉,会在从服务器中选举一个作为主服务器。 
配置文件如下 
master的sentinel.conf

port 26379
# 初次配置时的状态,这个sentinel会自动更新
sentinel monitor mymaster 127.0.0.1 6379 2
daemonize yes
logfile "./sentinel_log.log"
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 10000
sentinel config-epoch mymaster 2
bind 192.168.171.128 127.0.0.1
sentinel leader-epoch mymaster 2

slaver1和slaver2的sentinel.conf

port 26378
# 初次配置时的状态,这个sentinel会自动更新
sentinel monitor mymaster 127.0.0.1 6379 2
daemonize yes
logfile "./sentinel_log.log"
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 10000
sentinel config-epoch mymaster 2
bind 192.168.171.128 127.0.0.1
sentinel leader-epoch mymaster 2

再次启动redis所有的服务端

./redis-server redis.conf
./redis-server sentinel.conf --sentinel

分别开启redis的客户端

./redis-cli
./redis-cli -h 127.0.0.1 -p 6378
./redis-cli -h 127.0.0.1 -p 6377

使用一下命令查看三个redis服务的状态

info replication  

四、代码支持

RedisCacheConfig

package com.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import java.lang.reflect.Method; /**
* @author David
*/
@Configuration
@EnableAutoConfiguration
@EnableCaching //加上这个注解是的支持缓存注解
public class RedisCacheConfig extends CachingConfigurerSupport { @Value("${spring.redis.host}")
private String host; @Value("${spring.redis.port}")
private int port; @Value("${spring.redis.timeout}")
private int timeout; @Value("${spring.redis.database}")
private int database; @Value("${spring.redis.password}")
private String password; @Value("${spring.redis.sentinel.nodes}")
private String redisNodes; @Value("${spring.redis.sentinel.master}")
private String master; /**
* redis哨兵配置
* @return
*/
@Bean
public RedisSentinelConfiguration redisSentinelConfiguration(){
RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
String[] host = redisNodes.split(",");
for(String redisHost : host){
String[] item = redisHost.split(":");
String ip = item[0];
String port = item[1];
configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
}
configuration.setMaster(master);
return configuration;
} /**
* 连接redis的工厂类
*
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory(redisSentinelConfiguration());
factory.setHostName(host);
factory.setPort(port);
factory.setTimeout(timeout);
factory.setPassword(password);
factory.setDatabase(database);
return factory;
} /**
* 配置RedisTemplate
* 设置添加序列化器
* key 使用string序列化器
* value 使用Json序列化器
* 还有一种简答的设置方式,改变defaultSerializer对象的实现。
*
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate() {
//StringRedisTemplate的构造方法中默认设置了stringSerializer
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//设置开启事务
template.setEnableTransactionSupport(true);
//set key serializer
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer); template.setConnectionFactory(jedisConnectionFactory());
template.afterPropertiesSet();
return template;
} /**
* 设置RedisCacheManager
* 使用cache注解管理redis缓存
*
* @return
*/
@Override
@Bean
public RedisCacheManager cacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
return redisCacheManager;
} /**
* 自定义生成redis-key
*
* @return
*/
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName()).append(".");
sb.append(method.getName()).append(".");
for (Object obj : objects) {
sb.append(obj.toString());
}
System.out.println("keyGenerator=" + sb.toString());
return sb.toString();
}
};
}
}

application.properties

#========================redis \u914D\u7F6E=============================
# Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u4E3A0\uFF09,\u5982\u679C\u8BBE\u7F6E\u4E3A1\uFF0C\u90A3\u4E48\u5B58\u5165\u7684key-value\u90FD\u5B58\u653E\u5728select 1\u4E2D
spring.redis.database=0
# Redis\u670D\u52A1\u5668\u5730\u5740
spring.redis.host=localhost
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3
spring.redis.port=6379
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
spring.redis.password=
#\u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
spring.redis.pool.max-active=8
# \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
spring.redis.pool.max-wait=-1
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5
spring.redis.pool.max-idle=8
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5
spring.redis.pool.min-idle=0
# \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09
spring.redis.timeout=0
### \u4E3B\u4ECE\u914D\u7F6E
# name of Redis server \u54E8\u5175\u76D1\u542C\u7684Redis server\u7684\u540D\u79F0
spring.redis.sentinel.master=mymaster
# comma-separated list of host:port pairs \u54E8\u5175\u7684\u914D\u7F6E\u5217\u8868
spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26378,127.0.0.1:26377

  

Redis哨兵模式高可用部署和配置的更多相关文章

  1. Redis哨兵模式高可用解决方案

    一.序言 Redis高可用有两种模式:哨兵模式和集群模式,本文基于哨兵模式搭建一主两从三哨兵Redis高可用服务. 1.目标与收获 一主两从三哨兵Redis服务,基本能够满足中小型项目的高可用要求,使 ...

  2. Spark Standalone模式 高可用部署

      本文使用Spark的版本为:spark-2.4.0-bin-hadoop2.7.tgz. spark的集群采用3台机器进行搭建,机器分别是server01,server02,server03. 其 ...

  3. MooseFS及其高可用部署

    MooseFS的工作原理分析 MooseFS(下面统一称为MFS)由波兰公司Gemius SA于2008年5月30日正式推出的一款Linux下的开源存储系统,是OpenStack开源云计算项目的子项目 ...

  4. Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】

    No cross,no crown . 不经历风雨,怎么见彩虹. Redis哨兵模式,用现在流行的话可以说就是一个"哨兵机器人",给"哨兵机器人"进行相应的配置 ...

  5. Redis Sentinel实现高可用配置

    一般情况下yum安装redis的启动目录在:”/usr/sbin” :配置目录在”/etc/redis/”在其目录下会有默认的redis.conf和redis-sentinel.conf redis高 ...

  6. Redis高可用部署及监控

    Redis高可用部署及监控 目录                        一.Redis Sentinel简介 二.硬件需求 三.拓扑结构 .单M-S结构 .双M-S结构 .优劣对比 四.配置部 ...

  7. [Redis] Redis哨兵模式部署 - zz胖的博客

    1. 部署Redis集群 redis的安装及配置参考[redis部署] 本文以创建一主二从的集群为例. 1.1 部署与配置 先创建sentinel目录,在该目录下创建8000,8001,8002三个以 ...

  8. Redis主从配置及通过Keepalived实现Redis自动切换高可用

    Redis主从配置及通过Keepalived实现Redis自动切换高可用 [日期:2014-07-23] 来源:Linux社区  作者:fuquanjun [字体:大 中 小]   一:环境介绍: M ...

  9. Redis Sentinel 高可用部署实践集群

    一.Redis Sentinel 介绍    1.Sentinel     数据库环境搭建,从单机版到主备.再到多数据库集群,我们需要一个高可用的监控:比如Mysql中,我们可能会采用MHA来搭建我们 ...

随机推荐

  1. DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

    客户端当发送空的json字符串时,请求RestController时,报错: DefaultHandlerExceptionResolver : Failed to read HTTP message ...

  2. JavaScript之HTML DOM Event

    当鼠标在button上点击时,会在button上触发一个click事件.但是button是div的一个子元素, 在button里点击相当于在div里点击,是否click事件也会触发在div上?如果cl ...

  3. Java自学-集合框架 聚合操作

    聚合操作 步骤 1 : 聚合操作 JDK8之后,引入了对集合的聚合操作,可以非常容易的遍历,筛选,比较集合中的元素. 像这样: String name =heros .stream() .sorted ...

  4. Java时间格式化年-月-日-时间

    Date d = new Date(); System.out.println(d); //Sat Mar 16 20:58:56 CST 2019 System.out.println(d.toLo ...

  5. 干货 | 京东云托管Kubernetes集成镜像仓库并部署原生DashBoard

    在上一篇"Cloud Native 实操系列"文章中,我们为大家介绍了如何通过京东云原生容器实现Eureka的部署(

  6. Git 报错:Updates were rejected because the tip of your current branch is behind

    刚开始学习 git 命令,发现会出现很多的错误,所以就总结下出现的错误,以此来加深理解和掌握吧! 环境:在本地库操作了一系列的 add 和 commit 操作后,想把本地仓库推送到远端,但是发生以下错 ...

  7. 和我一起从0学算法(C语言版)(三)

    第二章 暴力求解(枚举法) 第一节 小学奥数题-程序求解 观察下面的加法算式:       祥 瑞 生 辉   +   三 羊 献 瑞 -------------------    三 羊 生 瑞 气 ...

  8. java类的实例化顺序

    1. 父类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行 2. 子类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行 3. 父类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行 4 ...

  9. TF分布式问题

    碰到一个没解决的问题. 用tensorflow 分布式异步更新模式训练模型, 模型中带正则项, 每个batch的损失函数为 \[\lambda \|W\|_1 + \frac 1 {N_j} \sum ...

  10. 吴裕雄--天生自然ShellX学习笔记:Shell 输入/输出重定向

    大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准输出,默 ...