|
ServiceStack.Redis 是一个C#访问Redis的客户端,可以说可以通过它实现所有需要Redis-Cli的功能。但是今天我在主Redis 实例设置了访问密码,而在slave 上没有设置,我通过一个缓存工厂来获取连接。在redisClient实例化可以直接设置密码。
1 ///
2 /// 缓存客户端管理器工厂
3 ///
4 public class PoolManagerFactory
5 {
6 private static PooledRedisClientManager Manager = null;
7 public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts, int initialDB = 0)
8 {
9 if (Manager == null)
10 {
11 Manager = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig()
12 {
13 MaxWritePoolSize = 5,
14 MaxReadPoolSize = 5,
15 AutoStart = true
16 }, initialDB, 50, 5);
17 }
18 return Manager;
19 }
20
21 }
我一直认为readWriteHosts数组中只能输入ip:port来代表一个redis 实例的连接,但是如何把密码加在里面呢?想不到如何实现,只能下载了源代码查看,原来这个实现是通过分隔字符串来实现,
1 ///
2 /// IP地址中可以加入auth验证 password@ip:port
3 ///
4 ///
5 ///
6 public static List ToRedisEndPoints(this IEnumerable hosts)
7 {
8 if (hosts == null) return new List();
9 //redis终结点的列表
10 var redisEndpoints = new List();
11 foreach (var host in hosts)
12 {
13 RedisEndpoint endpoint;
14 string[] hostParts;
15 if (host.Contains("@"))
16 {
17 hostParts = host.SplitOnLast('@');
18 var password = hostParts[0];
19 hostParts = hostParts[1].Split(':');
20 endpoint = GetRedisEndPoint(hostParts);
21 endpoint.Password = password;
22 }
23 else
24 {
25 hostParts = host.Split(':');
26 endpoint = GetRedisEndPoint(hostParts);
27 }
28 redisEndpoints.Add(endpoint);
29 }
30 return redisEndpoints;
31 }
在ip:port前面加上@用来表示密码,比如password@ip:port ,现在才知道能看到源码的程序是多么的幸福的一件事。开源伟大。
master:设置密码:config set requirepass password
slave 指定master密码:config set masterauth password 就可以实现在master设置密码,并且不需要重启redis实例 非常方便,但是这种方式重启后失效。
|
|
|