.Net支持Redis哨兵模式
csredis 博客
csRedisgit地址
csRedis3.2.1 Nuget地址 (在使用csredis3.2.1获取sentinel时产生运行时异常,调查问题最后发现是获取sentinel的s-down-time配置参数存在问题。在sentinel集群中并非每个sentinel都能获取到这个参数,获取不到就抛出异常了。)
https://yq.aliyun.com/articles/503938
修复csredis获取redis sentinel的问题
http://blog.bossma.cn/csharp/fix-csredis-get-redis-sentinel-problem/
/// <summary> /// RedisSentinelManagerContext /// </summary> public class RedisSentinelManagerContext { /// <summary> /// 哨兵默认端口 /// </summary> private const int DefaultSentinelPort = 26379; /// <summary> /// Redis哨兵配置地址 /// </summary> private static readonly string RedisSentinelAddresses = ConfigurationHelper.AppSetting("redis.sentinelAddresses"); /// <summary> /// Redis哨兵Master地址 /// </summary> private static readonly string RedisMasterName = ConfigurationHelper.AppSetting("redis.masterName"); /// <summary> /// Redis密码 /// </summary> private static readonly string RedisPassword = ConfigurationHelper.AppSetting("redis.password"); /// <summary> /// RedisSentinelManager实例 /// </summary> private static RedisSentinelManager _instance; /// <summary> /// RedisSentinelManager实例 /// </summary> public static RedisSentinelManager Instance() { if (_instance == null) { if (string.IsNullOrEmpty(RedisSentinelAddresses) || string.IsNullOrEmpty(RedisMasterName)) { throw new Exception("没有配置Redis的哨兵地址或Master名称"); } var param = new string[0]; _instance = new RedisSentinelManager(param); RedisSentinelAddresses.Split(',').ForEach(p => { var arr = p.Split(':'); _instance.Add(arr[0], arr.Length >= 2 ? int.Parse(arr[1]) : DefaultSentinelPort); }); if(!string.IsNullOrEmpty(RedisPassword)) _instance.Connected += (s, e) => _instance.Call(x => x.Auth(RedisPassword)); _instance.Connect(RedisMasterName); } return _instance; } public static string StringSet(string key, string value, int seconds) { return _instance.Call(r => r.Set(key, value, seconds)); } public static string StringSet(string key, string value, TimeSpan ts) { return _instance.Call(r => r.Set(key, value, ts)); } public static string StringGet(string key) { return _instance.Call(r => r.Get(key)); } public static bool KeyExpire(string key, int seconds) { return _instance.Call(r => r.Expire(key, seconds)); } public static bool KeyExpire(string key, TimeSpan expiration) { return _instance.Call(r => r.Expire(key, expiration)); } public static bool KeyExist(string key) { return _instance.Call(r => r.Exists(key)); } public static bool HSet(string key, string field, object value) { return _instance.Call(r => r.HSet(key, field, value)); } public static string HMSet(string key, Dictionary<string, string> dict) { return _instance.Call(r => r.HMSet(key, dict)); } public static Dictionary<string, string> HGetAll(string key) { return _instance.Call(r => r.HGetAll(key)); } public static string HGet(string key, string field) { return _instance.Call(r => r.HGet(key, field)); } public static bool HExists(string key, string field) { return _instance.Call(r => r.HExists(key, field)); } public static long HDel(string key, params string[] fields) { return _instance.Call(r => r.HDel(key, fields)); } }