Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="useLanIpAddress"></param>
        /// <returns></returns>
        public MqClientConfigInfo GetMqConfig(Guid storeId, bool useLanIpAddress)
        {
            int minutes = 1440;// 24*60分钟=1440分钟

            ConfigCenterClusterComputer configComputer = null;
            var item = GetMqTable(storeId);

            if (item != null && item.MqInstanceId.HasValue)
            {
                configComputer = _computerAccess.Get(item.MqInstanceId.Value);
            }
            if (configComputer != null && configComputer.DelFlag == false)
            {
                //如果当前配置有效,那么刷新配置
                item.CreateTime = DateTime.Now;
                item.Expired    = minutes;
                _searchService.ExcuteSql(string.Format("UPDATE ConfigCenter_MqTable SET CreateTime=GETDATE(),Expired={0} where StoreId='{1}'", minutes, storeId));
                return(FillModel(item, configComputer, useLanIpAddress));
            }
            var count = _computerAccess.Count(ClusterComputerType.MessageQueue);

            if (count == 0)
            {
                return(null);
            }
            int index = storeId.GetHashCode() % count;

            if (index >= count - 1)
            {
                index = count - 1;
            }
            if (index < 0)
            {
                index = 0;
            }
            configComputer = _computerAccess.GetComputerList(ClusterComputerType.MessageQueue, index).LastOrDefault();
            if (configComputer != null)
            {
                if (item != null)
                {
                    //如果当前配置有效,那么刷新配置
                    item.CreateTime   = DateTime.Now;
                    item.Expired      = minutes;
                    item.MqInstanceId = configComputer.Id;
                    _searchService.ExcuteSql(
                        string.Format("UPDATE ConfigCenter_MqTable SET CreateTime=GETDATE(),Expired={0},MqInstanceId={2} where StoreId='{1}'",
                                      minutes, storeId, configComputer.Id));
                }
                else
                {
                    //如果当前配置有效,那么刷新配置
                    item = new ConfigCenterMqTable()
                    {
                        CreateTime   = DateTime.Now,
                        StoreId      = storeId,
                        MqInstanceId = configComputer.Id
                    };
                    item.CreateTime = DateTime.Now;
                    item.Expired    = minutes;
                    _searchService.Create("ConfigCenter_MqTable", "StoreId", item);
                }
                return(FillModel(item, configComputer, useLanIpAddress));
            }

            return(null);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisConfig GetRedisConfig(string key)
        {
            ConfigCenterClusterComputer configComputer = null;
            var item = _cacheKeyAccess.GetByCacheKey(key);

            if (item != null && item.RedisInstanceId != 0)
            {
                configComputer = _computerAccess.Get(item.RedisInstanceId);
            }
            string ipAddress;

            if (configComputer != null)
            {
                ipAddress = configComputer.IpAddress;
                if (!string.IsNullOrEmpty(configComputer.AccessPassword))
                {
                    ipAddress = configComputer.AccessPassword + "@" + ipAddress;
                }
                if (configComputer.Port != 0)
                {
                    ipAddress += ":" + configComputer.Port;
                }
                return(new RedisConfig()
                {
                    ReadOnlyHosts = ipAddress,
                    ReadWriteHosts = ipAddress,
                    ConfigId = configComputer.Id
                });
            }
            var list = _computerAccess.GetComputerList(ClusterComputerType.Redis, short.MaxValue)
                       .ToList();

            if (list.Count == 0)
            {
                return(null);
            }
            List <ConfigCenterClusterComputer> renewList = new List <ConfigCenterClusterComputer>();

            for (int i = 0; i < list.Count(); i++)
            {
                var computer = list[i];
                if (computer.Weight <= 1)
                {
                    renewList.Add(computer);
                }
                else
                {
                    for (int j = 0; j < computer.Weight; j++)
                    {
                        renewList.Add(computer);
                    }
                }
            }
            //保证相同的机器编码计算出来的结果一致
            var hashcode = GetMyHashCode(key);
            int index    = hashcode % renewList.Count;

            if (index < renewList.Count)
            {
                configComputer = renewList[index];
            }
            else
            {
                configComputer = renewList[0];
            }
            ipAddress = configComputer.IpAddress;
            if (!string.IsNullOrEmpty(configComputer.AccessPassword))
            {
                ipAddress = configComputer.AccessPassword + "@" + ipAddress;
            }
            if (configComputer.Port != 0)
            {
                ipAddress += ":" + configComputer.Port;
            }
            if (item != null)
            {
                item.RedisInstanceId = configComputer.Id;
                item.CreateTime      = DateTime.Now;
                item.Expired         = 1440;
                _cacheKeyAccess.Update(item);
            }
            else
            {
                item = new ConfigCenterRedisCacheKey()
                {
                    CacheKey        = key,
                    RedisInstanceId = configComputer.Id,
                    CreateTime      = DateTime.Now,
                    Expired         = 1440
                };
                _cacheKeyAccess.Create(item);
            }
            return(new RedisConfig()
            {
                ReadOnlyHosts = ipAddress,
                ReadWriteHosts = ipAddress,
                ConfigId = configComputer.Id,
                ExpiredTime = DateTime.Now.AddMinutes(item.Expired == 0 ? 1440 : item.Expired)
            });
        }