一、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文件(其余是默认设置)

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

slaver1和slaver2的redis.conf文件

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

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

启动服务

  1. ./redis-server redis.conf

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

三、Redis的哨兵模式配置

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

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

slaver1和slaver2的sentinel.conf

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

再次启动redis所有的服务端

  1. ./redis-server redis.conf
  2. ./redis-server sentinel.conf --sentinel

分别开启redis的客户端

  1. ./redis-cli
  2. ./redis-cli -h 127.0.0.1 -p 6378
  3. ./redis-cli -h 127.0.0.1 -p 6377

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

  1. info replication  

四、代码支持

RedisCacheConfig

  1. package com.config;
  2.  
  3. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  4. import com.fasterxml.jackson.annotation.PropertyAccessor;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  8. import org.springframework.cache.annotation.CachingConfigurerSupport;
  9. import org.springframework.cache.annotation.EnableCaching;
  10. import org.springframework.cache.interceptor.KeyGenerator;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.data.redis.cache.RedisCacheManager;
  14. import org.springframework.data.redis.connection.RedisNode;
  15. import org.springframework.data.redis.connection.RedisSentinelConfiguration;
  16. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  17. import org.springframework.data.redis.core.RedisTemplate;
  18. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  19. import org.springframework.data.redis.serializer.StringRedisSerializer;
  20.  
  21. import java.lang.reflect.Method;
  22.  
  23. /**
  24. * @author David
  25. */
  26. @Configuration
  27. @EnableAutoConfiguration
  28. @EnableCaching //加上这个注解是的支持缓存注解
  29. public class RedisCacheConfig extends CachingConfigurerSupport {
  30.  
  31. @Value("${spring.redis.host}")
  32. private String host;
  33.  
  34. @Value("${spring.redis.port}")
  35. private int port;
  36.  
  37. @Value("${spring.redis.timeout}")
  38. private int timeout;
  39.  
  40. @Value("${spring.redis.database}")
  41. private int database;
  42.  
  43. @Value("${spring.redis.password}")
  44. private String password;
  45.  
  46. @Value("${spring.redis.sentinel.nodes}")
  47. private String redisNodes;
  48.  
  49. @Value("${spring.redis.sentinel.master}")
  50. private String master;
  51.  
  52. /**
  53. * redis哨兵配置
  54. * @return
  55. */
  56. @Bean
  57. public RedisSentinelConfiguration redisSentinelConfiguration(){
  58. RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
  59. String[] host = redisNodes.split(",");
  60. for(String redisHost : host){
  61. String[] item = redisHost.split(":");
  62. String ip = item[0];
  63. String port = item[1];
  64. configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
  65. }
  66. configuration.setMaster(master);
  67. return configuration;
  68. }
  69.  
  70. /**
  71. * 连接redis的工厂类
  72. *
  73. * @return
  74. */
  75. @Bean
  76. public JedisConnectionFactory jedisConnectionFactory() {
  77. JedisConnectionFactory factory = new JedisConnectionFactory(redisSentinelConfiguration());
  78. factory.setHostName(host);
  79. factory.setPort(port);
  80. factory.setTimeout(timeout);
  81. factory.setPassword(password);
  82. factory.setDatabase(database);
  83. return factory;
  84. }
  85.  
  86. /**
  87. * 配置RedisTemplate
  88. * 设置添加序列化器
  89. * key 使用string序列化器
  90. * value 使用Json序列化器
  91. * 还有一种简答的设置方式,改变defaultSerializer对象的实现。
  92. *
  93. * @return
  94. */
  95. @Bean
  96. public RedisTemplate<Object, Object> redisTemplate() {
  97. //StringRedisTemplate的构造方法中默认设置了stringSerializer
  98. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  99. //设置开启事务
  100. template.setEnableTransactionSupport(true);
  101. //set key serializer
  102. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  103. template.setKeySerializer(stringRedisSerializer);
  104. template.setHashKeySerializer(stringRedisSerializer);
  105.  
  106. template.setConnectionFactory(jedisConnectionFactory());
  107. template.afterPropertiesSet();
  108. return template;
  109. }
  110.  
  111. /**
  112. * 设置RedisCacheManager
  113. * 使用cache注解管理redis缓存
  114. *
  115. * @return
  116. */
  117. @Override
  118. @Bean
  119. public RedisCacheManager cacheManager() {
  120. RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
  121. return redisCacheManager;
  122. }
  123.  
  124. /**
  125. * 自定义生成redis-key
  126. *
  127. * @return
  128. */
  129. @Override
  130. public KeyGenerator keyGenerator() {
  131. return new KeyGenerator() {
  132. @Override
  133. public Object generate(Object o, Method method, Object... objects) {
  134. StringBuilder sb = new StringBuilder();
  135. sb.append(o.getClass().getName()).append(".");
  136. sb.append(method.getName()).append(".");
  137. for (Object obj : objects) {
  138. sb.append(obj.toString());
  139. }
  140. System.out.println("keyGenerator=" + sb.toString());
  141. return sb.toString();
  142. }
  143. };
  144. }
  145. }

application.properties

  1. #========================redis \u914D\u7F6E=============================
  2. # 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
  3. spring.redis.database=0
  4. # Redis\u670D\u52A1\u5668\u5730\u5740
  5. spring.redis.host=localhost
  6. # Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3
  7. spring.redis.port=6379
  8. # Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
  9. spring.redis.password=
  10. #\u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
  11. spring.redis.pool.max-active=8
  12. # \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
  13. spring.redis.pool.max-wait=-1
  14. # \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5
  15. spring.redis.pool.max-idle=8
  16. # \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5
  17. spring.redis.pool.min-idle=0
  18. # \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09
  19. spring.redis.timeout=0
  20. ### \u4E3B\u4ECE\u914D\u7F6E
  21. # name of Redis server \u54E8\u5175\u76D1\u542C\u7684Redis server\u7684\u540D\u79F0
  22. spring.redis.sentinel.master=mymaster
  23. # comma-separated list of host:port pairs \u54E8\u5175\u7684\u914D\u7F6E\u5217\u8868
  24. 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. 51Nod大数加法(两个数正负都可)

    很多大数的问题都运用模拟的思想,但是这个说一样也一样,但是难度较大,很麻烦,我自己谢写了100多行的代码,感觉很对,但就是WA.其实个人感觉C和C++没有大数类,是对人思想和算法的考验,但是有时候做不 ...

  2. decltype关键字

    decltype用于编译时类型推导,是以一个普通表达式作为参数,返回该表达式的类型,而且decltype并不会对表达式进行求值. decltype的用法: //推导出表达式类型 ; decltype( ...

  3. [WesternCTF2018]shrine

    0x00 知识点 SSTI模板注入: 模板注入涉及的是服务端Web应用使用模板引擎渲染用户请求的过程 服务端把用户输入的内容渲染成模板就可能造成SSTI(Server-Side Template In ...

  4. php对象:__autoload()函数及单入口文件,__set(), __get(), get_class_methods(),get_class_vars()

    __autoload():当类中找不到相关类的时候,会自动执行__autoload()函数,可以自动加载相关文件 __set() : 当对类的私有变量进行调用赋值时,自动调用该方法.  __get() ...

  5. HTML5 SVG应用(1)——loading效果

    先看一下效果: 链接 代码: <svg version="1.1" id="loader-1" xmlns="http://www.w3.org ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习: DOM 事件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. PHP的一个小tips (关于=和==或者===的使用)

    由于我在项目中,很多场景判断等式成立的时候 都习惯把值放在==前面(例如 1 == $nStatus), 今天有个同事揪着我问为啥总这样写,回答之后今天也稍作记录下吧. 如果正常些 $nStatus ...

  8. 简单模拟B1011

    #include<iostream> using namespace std; int main() { int n; long long a,b,c; cin >> n; ; ...

  9. Linux系统提示无法获得锁

    这种情况出现主要是因为软件更新或者安装时出现错误. 删除掉两个临时文件即可 sudo rm /var/lib/dpkg/lock sudo rm /var/cache/apt/archive/lock ...

  10. php的执行流程

    源代码(人认识)->字节码(解释器认识)->机器码(硬件认识)来看下PHP的执行流程,假设有个a.php文件,不启用opacache的流程如下:a.php->经过zend编译-> ...