配置Redis的Sentinel模式

需要使用Sentinel,至少用到3个类型的Reids服务器

1、Master服务器

  无需特殊需求的情况下,只要设置IP和端口即可,默认127.0.0.1 6379

2、Slave服务器

  除了设置自己的IP和端口,还要设置slaveof后面的值为Master的IP和端口,用逗号分割

  格式:slaveof 127.0.0.1 6379

3、Sentinel服务器

 1 #TwRedis(同一个Sentinel可以同时监听多个Master服务器)
 2 #设置要监听的服务器名称 IP和端口,最后一个2表示至少要两个 Sentinel服务器判定 Master失效 才会执行自动故障迁移
 3 sentinel monitor TwRedis 127.0.0.1 6001 2 
 4 #指定了Sentinel认为Redis实例已经失效所需的毫秒数
 5 sentinel down-after-milliseconds TwRedis 10000
 7 #指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,
8 #在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
8 sentinel config-epoch TwRedis 1 9 #指定Sentinel运行的端口 10 port 5001 

 

以上这三种类型的Redis都启动后,在Master被判断失效后,会在所有的slave服务器中竞选一个服务器提升为Master

 

c#连接代码,需要StackExchange.Redis支持

 1  public class RedisConnectionManager
 2     {
 3         /// <summary>
 4         /// redis配置文件信息
 5         /// </summary>
 6         private static RedisConfig redisConfig = RedisConfig.GetConfig();
 7 
 8         private static readonly object Locker = new object();
 9         private static ConnectionMultiplexer _instance;
10 
11         /// <summary>
12         /// 单例获取
13         /// </summary>
14         public static ConnectionMultiplexer Instance
15         {
16             get
17             {
18                 if (_instance == null)
19                 {
20                     lock (Locker)
21                     {
22                         if (_instance == null || !_instance.IsConnected)
23                         {
24                             _instance = GetManager();
25                         }
26                     }
27                 }
28                 return _instance;
29             }
30         }
31 
32         private static ConnectionMultiplexer GetManager()
33         {
34             #region 设置Redis集群
35             ConfigurationOptions option = new ConfigurationOptions
36             {
37                 ServiceName = redisConfig.ServerName,
38                 Proxy = Proxy.Twemproxy,
39                 AbortOnConnectFail = true,
40                 AllowAdmin = true,
41             };
42             redisConfig.MasterHosts.Split(',').ToList().ForEach(item =>
43             {
44                 option.EndPoints.Add(item);
45             });
46             var connect = ConnectionMultiplexer.Connect(option);
47             #endregion
48 
49 
50             #region 设置Sentinel集群
51             if (!string.IsNullOrEmpty(redisConfig.SentinelHosts))
52             {
53                 ConfigurationOptions sentinelConfig = new ConfigurationOptions();
54                 sentinelConfig.ServiceName = redisConfig.ServerName;
55                 redisConfig.SentinelHosts.Split(',').ToList().ForEach(item =>
56                 {
57                     sentinelConfig.EndPoints.Add(item);
58                 });
59                 sentinelConfig.TieBreaker = "";//这行在sentinel模式必须加上
60                 sentinelConfig.CommandMap = CommandMap.Sentinel;
61                 sentinelConfig.DefaultVersion = new Version(3, 0);
62                 ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(sentinelConfig);
63                 conn.GetSubscriber().Subscribe("+switch-master", (channel, message) =>
64                 {
65                     Console.WriteLine((string)message);
66                 });
67             } 
68             #endregion
69 
70             return connect;
71         }
72 
73     }

 按照以上设置后,在主从切换后,会自动调整连接服务器,调用者无需关注此时是那个Redis服务器在生效

posted @ 2017-01-26 13:17  fengylm  阅读(771)  评论(0编辑  收藏  举报