1 搭建本机redis集群
创建三个redis集群(方便起见,都是在本机,只是修改了端口号)
2 修改redis.windows.conf 配置各自的端口号
在从服务器 redis.windows.conf 增加slaveof ip port (主服务器的ip和端口)
3 在每个redis服务器文件夹下创建 sentinel.conf文件内容为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1# 这个是Redis6379配置内容,其他文件同理新增然后改一下端口即可,26380,和 26381。
2
3#当前Sentinel服务运行的端口
4port 26379
5# 哨兵监听的主服务器
6sentinel monitor mymaster 192.168.11.239 6379 2
7# 3s内mymaster无响应,则认为mymaster宕机了
8sentinel down-after-milliseconds mymaster 3000
9#如果10秒后,mysater仍没启动过来,则启动failover
10sentinel failover-timeout mymaster 10000
11# 执行故障转移时, 最多有1个从服务器同时对新的主服务器进行同步
12sentinel parallel-syncs mymaster 1
13#哨兵的Ip地址
14bind 192.168.11.239
15
16
4 创建启动redis的bat:
内容为:@echo off cd redis6379 startRedisServer.bat
在各自的redis文件下创建startRedisServer.bat 内容为:@echo off title 6379 redis-server.exe redis.windows.conf @pause
创建启动哨兵的bat:
内容为:@echo off cd redis6379 startRedisSentinel.bat at
在各自的redis文件下创建startRedisSentinel.bat 内容为:@echo off title S6381 redis-server.exe sentinel.conf –sentinel @pause
5 点击启动redis集群,在启动哨兵,本机搭建哨兵模式已完成
6 springboot集成 redis 哨兵模式
添加pom.xml相关的jar包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1 <dependency>
2 <groupId>redis.clients</groupId>
3 <artifactId>jedis</artifactId>
4 </dependency>
5 <dependency>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-data-redis</artifactId>
8 </dependency>
9 <dependency>
10 <dependency>
11 <groupId>org.apache.commons</groupId>
12 <artifactId>commons-pool2</artifactId>
13 <version>2.5.0</version>
14 </dependency>
15
16
7 添加属性文件
1
2
3
4
5
6
7
8
9
10
11
12
13 1spring.redis.host=192.168.11.239
2spring.redis.port=6379
3spring.redis.password=
4spring.redis.jedis.pool.max-active=1024
5spring.redis.jedis.pool.max-wait=20000
6spring.redis.jedis.pool.max-idle=200
7spring.redis.jedis.pool.min-idle=10
8spring.redis.sentinel.master=mymaster
9spring.redis.sentinel.nodes=192.168.11.239:26379,192.168.11.239:26380,192.168.11.239:26381
10
11
12
13
8 配置redisconfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 1package com.rao.tao.sentinel.config;
2
3import org.slf4j.Logger;
4import org.slf4j.LoggerFactory;
5import org.springframework.beans.factory.annotation.Value;
6import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7import org.springframework.boot.context.properties.ConfigurationProperties;
8import org.springframework.context.annotation.Bean;
9import org.springframework.context.annotation.Configuration;
10import org.springframework.data.redis.connection.RedisConnectionFactory;
11import org.springframework.data.redis.connection.RedisNode;
12import org.springframework.data.redis.connection.RedisSentinelConfiguration;
13import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
14import org.springframework.data.redis.core.RedisTemplate;
15import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
16import org.springframework.data.redis.serializer.StringRedisSerializer;
17import redis.clients.jedis.JedisPoolConfig;
18
19import java.util.HashSet;
20import java.util.List;
21import java.util.Set;
22
23@Configuration
24@EnableAutoConfiguration
25public class RedisConfig {
26 private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);
27 @Value("#{'${spring.redis.sentinel.nodes}'.split(',')}")
28 private List<String> nodes;
29
30 @Bean
31 @ConfigurationProperties(prefix="spring.redis")
32 public JedisPoolConfig getRedisConfig(){
33 JedisPoolConfig config = new JedisPoolConfig();
34 return config;
35 }
36 @Bean
37 public RedisSentinelConfiguration sentinelConfiguration(){
38 RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
39 //配置matser的名称
40 redisSentinelConfiguration.master("mymaster");
41 //配置redis的哨兵sentinel
42 Set<RedisNode> redisNodeSet = new HashSet<>();
43 nodes.forEach(x->{
44 redisNodeSet.add(new RedisNode(x.split(":")[0],Integer.parseInt(x.split(":")[1])));
45 });
46 logger.info("redisNodeSet -->"+redisNodeSet);
47 redisSentinelConfiguration.setSentinels(redisNodeSet);
48 return redisSentinelConfiguration;
49 }
50
51 @Bean
52 public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig, RedisSentinelConfiguration sentinelConfig) {
53 JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig,jedisPoolConfig);
54 return jedisConnectionFactory;
55 }
56
57 @Bean
58 public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
59 RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
60 redisTemplate.setConnectionFactory(jedisConnectionFactory);
61 GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
62 // 设置值(value)的序列化采用FastJsonRedisSerializer。
63 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
64// redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
65 // 设置键(key)的序列化采用StringRedisSerializer。
66 redisTemplate.setKeySerializer(new StringRedisSerializer());
67 redisTemplate.setHashKeySerializer(new StringRedisSerializer());
68 redisTemplate.afterPropertiesSet();
69 return redisTemplate;
70 }
71
72
73}
74
75
76