public static int GetPort(string IPAndPort)
        {
            var endPoint = RedisCacheConfigHelper.SplitString(IPAndPort, ":").ToList();
            var ip       = endPoint[0];            //IP
            var port     = int.Parse(endPoint[1]); //端口

            return(port);
        }
Exemple #2
0
        /// <summary>
        /// 创建链接池管理对象
        /// </summary>
        public static ICacheManager Create(StackExchange.RedisCacheConfig config)
        {
            ThreadPool.SetMinThreads(200, 200);

            if (string.IsNullOrEmpty(config.KeyPrefix))
            {
                _KeyPrefix = string.Empty;
            }
            else
            {
                _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 = RedisCacheConfigHelper.SplitString(writeServerList, ",").ToList();
                            var    readServerArray  = RedisCacheConfigHelper.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))
                                {
                                    ConfigurationOptions configOption = new ConfigurationOptions();
                                    configOption.ServiceName        = NodeName;
                                    configOption.Password           = config.Password;
                                    configOption.AbortOnConnectFail = false;
                                    configOption.DefaultDatabase    = config.DBNum;
                                    configOption.Ssl = config.Ssl;

                                    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 = RedisCacheConfigHelper.GetServerClusterName(writeServerArray[i]);
                                        //主服务器名称
                                        var masterServer = RedisCacheConfigHelper.GetServerHost(writeServerArray[i]);

                                        //主服务器列表
                                        var masterServerIPAndPortArray = RedisCacheConfigHelper.GetServerList(config.WriteServerList, NodeName);
                                        //从服务器列表
                                        var slaveServerIPAndPortArray = RedisCacheConfigHelper.GetServerList(config.ReadServerList, NodeName);

                                        //当前集群的配置不存在
                                        if (!_clusterConfigOptions.ContainsKey(NodeName))
                                        {
                                            ConfigurationOptions configOption = new ConfigurationOptions();
                                            configOption.ServiceName        = NodeName;
                                            configOption.Password           = config.Password;
                                            configOption.AbortOnConnectFail = false;
                                            configOption.DefaultDatabase    = config.DBNum;
                                            configOption.Ssl             = config.Ssl;
                                            configOption.ConnectTimeout  = 15000;
                                            configOption.SyncTimeout     = 5000;
                                            configOption.ResponseTimeout = 15000;

                                            foreach (var ipAndPort in masterServerIPAndPortArray.Union(slaveServerIPAndPortArray).Distinct())
                                            {
                                                configOption.EndPoints.Add(RedisCacheConfigHelper.GetIP(ipAndPort), RedisCacheConfigHelper.GetPort(ipAndPort));
                                            }

                                            _clusterConfigOptions.Add(NodeName, configOption);
                                        }

                                        Nodes.Add(NodeName);
                                    }
                                    else
                                    {
                                        //192.168.10.100:6379
                                        var NodeName = writeServerArray[i];

                                        if (!_clusterConfigOptions.ContainsKey(NodeName))
                                        {
                                            ConfigurationOptions configOption = new ConfigurationOptions();
                                            configOption.ServiceName        = NodeName;
                                            configOption.Password           = config.Password;
                                            configOption.AbortOnConnectFail = false;
                                            configOption.DefaultDatabase    = config.DBNum;
                                            configOption.Ssl             = config.Ssl;
                                            configOption.ConnectTimeout  = 15000;
                                            configOption.SyncTimeout     = 5000;
                                            configOption.ResponseTimeout = 15000;


                                            configOption.EndPoints.Add(RedisCacheConfigHelper.GetIP(NodeName), RedisCacheConfigHelper.GetPort(NodeName));
                                            _clusterConfigOptions.Add(NodeName, configOption);
                                        }

                                        Nodes.Add(NodeName);
                                    }
                                }
                            }

                            _Locator = new KetamaHash.KetamaNodeLocator(Nodes, _VIRTUAL_NODE_COUNT);
                        }
                        else
                        {
                            List <string> sentinelMasterNameList = new List <string>();
                            List <string> sentinelServerHostList = new List <string>();
                            var           SentineList            = RedisCacheConfigHelper.SplitString(config.SentineList, ",").ToList();
                            for (int i = 0; i < SentineList.Count; i++)
                            {
                                var args = RedisCacheConfigHelper.SplitString(SentineList[i], "@").ToList();

                                var ServiceName = args[0];
                                var hostName    = args[1];
                                var endPoint    = RedisCacheConfigHelper.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服务器
                                    ConfigurationOptions sentinelConfig = new ConfigurationOptions();
                                    sentinelConfig.ServiceName = ServiceName;
                                    sentinelConfig.EndPoints.Add(ip, port);
                                    sentinelConfig.AbortOnConnectFail = false;
                                    sentinelConfig.DefaultDatabase    = config.DBNum;
                                    sentinelConfig.TieBreaker         = ""; //这行在sentinel模式必须加上
                                    sentinelConfig.CommandMap         = CommandMap.Sentinel;
                                    sentinelConfig.DefaultVersion     = new Version(3, 0);
                                    _clusterConfigOptions[hostName]   = sentinelConfig;
                                }
                                else
                                {
                                    ConfigurationOptions sentinelConfig = _clusterConfigOptions[hostName] as ConfigurationOptions;
                                    sentinelConfig.EndPoints.Add(ip, port);
                                    _clusterConfigOptions[hostName] = sentinelConfig;
                                }
                            }

                            //初始化Reds分片定位器
                            _Locator = new KetamaHash.KetamaNodeLocator(sentinelServerHostList, _VIRTUAL_NODE_COUNT);
                        }
                    }
                }
            }


            return(new RedisCacheManage(config.DBNum, config.NumberOfConnections));
        }