Пример #1
0
        private MqClientConfigInfo FillModel(ConfigCenterMqTable table, ConfigCenterClusterComputer data, bool useLanIpAddress)
        {
            bool flag = !string.IsNullOrEmpty(data.LanIpAddress) && useLanIpAddress;

            return(new MqClientConfigInfo()
            {
                ClientId = table.StoreId,
                AccessPassword = data.AccessPassword,
                AccessUsername = data.AccessUsername,
                IpAddress = flag ? data.LanIpAddress : data.IpAddress,
                MqttPort = data.MqttPort,
                Port = data.Port,
                MqInstanceId = data.Id,
                MqListenerId = table.MqListenerId ?? 0,
                ExpiredTime = DateTime.Now.AddMinutes(table.Expired)
            });
        }
Пример #2
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);
        }