serviceStack.Redis 在PooledRedisClientManager 中设置密码
ServiceStack.Redis 是一个C#访问Redis的客户端,可以说可以通过它实现所有需要Redis-Cli的功能。但是今天我在主Redis 实例设置了访问密码,而在slave 上没有设置,我通过一个缓存工厂来获取连接。在redisClient实例化可以直接设置密码。
1 /// <summary> 2 /// 缓存客户端管理器工厂 3 /// </summary> 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 /// <summary> 2 /// IP地址中可以加入auth验证 password@ip:port 3 /// </summary> 4 /// <param name="hosts"></param> 5 /// <returns></returns> 6 public static List<RedisEndpoint> ToRedisEndPoints(this IEnumerable<string> hosts) 7 { 8 if (hosts == null) return new List<RedisEndpoint>(); 9 //redis终结点的列表 10 var redisEndpoints = new List<RedisEndpoint>(); 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实例 非常方便,但是这种方式重启后失效。
有人的地方就有政治,有政治就有斗争,但我讨厌政治,讨厌无休止的迎合。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库