public static int GetPort(string IPAndPort) { var endPoint = CacheConfigHelper.SplitString(IPAndPort, ":").ToList(); var ip = endPoint[0]; //IP var port = int.Parse(endPoint[1]); //端口 return(port); }
/// <summary> /// 创建链接池管理对象 /// </summary> public static RedisCacheManage Create(XuHos.Common.Config.Sections.Redis config, int DbNum = 0) { _KeyPrefix = config.KeyPrefix + ":"; if (_Locator == null) { lock (_syncCreateInstance) { if (_Locator == null) { if (string.IsNullOrEmpty(config.SentineList) || !_supportSentinal) { //Redis服务器相关配置 string writeServerList = config.WriteServerList; string readServerList = config.ReadServerList; var writeServerArray = CacheConfigHelper.SplitString(writeServerList, ",").ToList(); var readServerArray = CacheConfigHelper.SplitString(readServerList, ",").ToList(); var Nodes = new List <string>(); //只有一个写,多个读的情况 /* * Redis.ReadServerList 192.168.100.51:16378,192.168.100.51:26378 * Redis.WriteServerList 192.168.100.51:6378 */ if (writeServerArray.Count == 1) { var writeServer = writeServerArray[0]; var NodeName = writeServerArray[0]; if (!_clusterConfigOptions.ContainsKey(NodeName)) { StackExchange.Redis.ConfigurationOptions configOption = new StackExchange.Redis.ConfigurationOptions(); configOption.ServiceName = NodeName; configOption.Password = config.Password; configOption.AbortOnConnectFail = false; configOption.DefaultDatabase = string.IsNullOrEmpty(config.DBNum) ? 0 : int.Parse(config.DBNum); configOption.Ssl = config.Ssl != null && bool.TrueString.ToUpper() == config.Ssl.ToUpper() ? true : false; foreach (var ipAndPort in writeServerArray.Union(readServerArray)) { configOption.EndPoints.Add(ipAndPort); } _clusterConfigOptions.Add(writeServer, configOption); } Nodes.Add(NodeName); } /* * 多个写和多个读 * Redis.ReadServerList [email protected]:16378,[email protected]:16379,[email protected]:16380,[email protected]:16381,[email protected]:16382,[email protected]:26378,[email protected]:26379,[email protected]:26380,[email protected]:26381,[email protected]:26382 * Redis.WriteServerList [email protected]:6378,[email protected]:6379,[email protected]:6380,[email protected]:6381,[email protected]:6382 */ else { for (int i = 0; i < writeServerArray.Count; i++) { //存在多个Master服务器的时候 if (writeServerArray[i].IndexOf("@") > 0) { //集群名称() var NodeName = CacheConfigHelper.GetServerClusterName(writeServerArray[i]); //主服务器名称 var masterServer = CacheConfigHelper.GetServerHost(writeServerArray[i]); //主服务器列表 var masterServerIPAndPortArray = CacheConfigHelper.GetServerList(config.WriteServerList, NodeName); //从服务器列表 var slaveServerIPAndPortArray = CacheConfigHelper.GetServerList(config.ReadServerList, NodeName); //当前集群的配置不存在 if (!_clusterConfigOptions.ContainsKey(NodeName)) { StackExchange.Redis.ConfigurationOptions configOption = new StackExchange.Redis.ConfigurationOptions(); configOption.ServiceName = NodeName; configOption.Password = config.Password; configOption.AbortOnConnectFail = false; configOption.DefaultDatabase = string.IsNullOrEmpty(config.DBNum) ? int.Parse(config.DBNum) : 0; configOption.Ssl = config.Ssl != null && bool.TrueString.ToUpper() == config.Ssl.ToUpper() ? true : false; foreach (var ipAndPort in masterServerIPAndPortArray.Union(slaveServerIPAndPortArray).Distinct()) { configOption.EndPoints.Add(CacheConfigHelper.GetIP(ipAndPort), CacheConfigHelper.GetPort(ipAndPort)); } _clusterConfigOptions.Add(NodeName, configOption); } Nodes.Add(NodeName); } else { //192.168.10.100:6379 var NodeName = writeServerArray[i]; if (!_clusterConfigOptions.ContainsKey(NodeName)) { StackExchange.Redis.ConfigurationOptions configOption = new StackExchange.Redis.ConfigurationOptions(); configOption.ServiceName = NodeName; configOption.Password = config.Password; configOption.AbortOnConnectFail = false; configOption.DefaultDatabase = string.IsNullOrEmpty(config.DBNum) ? int.Parse(config.DBNum) : 0; configOption.Ssl = config.Ssl != null && bool.TrueString.ToUpper() == config.Ssl.ToUpper() ? true : false; configOption.EndPoints.Add(CacheConfigHelper.GetIP(NodeName), CacheConfigHelper.GetPort(NodeName)); _clusterConfigOptions.Add(NodeName, configOption); } Nodes.Add(NodeName); } } } _Locator = new XuHos.Common.KetamaHash.KetamaNodeLocator(Nodes, _VIRTUAL_NODE_COUNT); } else { List <string> sentinelMasterNameList = new List <string>(); List <string> sentinelServerHostList = new List <string>(); var SentineList = CacheConfigHelper.SplitString(config.SentineList, ",").ToList(); for (int i = 0; i < SentineList.Count; i++) { var args = CacheConfigHelper.SplitString(SentineList[i], "@").ToList(); var ServiceName = args[0]; var hostName = args[1]; var endPoint = CacheConfigHelper.SplitString(hostName, ":").ToList(); var ip = endPoint[0]; //IP var port = int.Parse(endPoint[1]); //端口 sentinelMasterNameList.Add(ServiceName); sentinelServerHostList.Add(hostName); if (!_clusterConfigOptions.ContainsKey(hostName)) { //连接sentinel服务器 StackExchange.Redis.ConfigurationOptions sentinelConfig = new StackExchange.Redis.ConfigurationOptions(); sentinelConfig.ServiceName = ServiceName; sentinelConfig.EndPoints.Add(ip, port); sentinelConfig.AbortOnConnectFail = false; sentinelConfig.DefaultDatabase = string.IsNullOrEmpty(config.DBNum) ? int.Parse(config.DBNum) : 0; sentinelConfig.TieBreaker = ""; //这行在sentinel模式必须加上 sentinelConfig.CommandMap = StackExchange.Redis.CommandMap.Sentinel; sentinelConfig.DefaultVersion = new Version(3, 0); _clusterConfigOptions[hostName] = sentinelConfig; } else { StackExchange.Redis.ConfigurationOptions sentinelConfig = _clusterConfigOptions[hostName] as StackExchange.Redis.ConfigurationOptions; sentinelConfig.EndPoints.Add(ip, port); _clusterConfigOptions[hostName] = sentinelConfig; } } //初始化Reds分片定位器 _Locator = new XuHos.Common.KetamaHash.KetamaNodeLocator(sentinelServerHostList, _VIRTUAL_NODE_COUNT); } } } } return(new RedisCacheManage(DbNum)); }