Exemple #1
0
        /// <summary>
        /// 执行调度
        /// </summary>
        /// <returns></returns>
        public ServiceRegModel DoDispatch()
        {
            regservic.CurrentContext = this.CurrentContext;
            //从注册的服务列表里面,根据服务中的长连接数量决定选取那个服务
            ServiceRegModel returnValue = null;
            ICacheProvider  cache       = CacheProviderFactory.GetGlobalCacheProvider();
            var             list        = regservic.GetActiveServerList();//已经确保GetActiveServerList 不为空
            //if (list != null)
            //{
            //    var regModel = list.Select(o => new { Count = cache.Get<int>(o.GetUri() + "_ListenerCount"), RegModel = o })
            //        .OrderBy(o => o.Count).FirstOrDefault();
            //    if (regModel != null)
            //        returnValue = regModel.RegModel;

            //}
            int minCount = int.MaxValue;

            foreach (ServiceRegModel item in list)
            {
                ServiceHostInfo host = item as ServiceHostInfo;
                if (host != null && minCount > host.ListenerCount)
                {
                    minCount    = host.ListenerCount;
                    returnValue = item;
                }
            }
            if (returnValue == null)
            {
                returnValue = this.CurrentContext.Host;
            }
            return(returnValue);
        }
Exemple #2
0
        /// <summary>
        /// 监听器统计,如果服务挂机,则统计信息在1分钟后过期
        /// </summary>
        private static void ListenersCountTimer()
        {
            CountTimer = new System.Threading.Timer(new System.Threading.TimerCallback(o =>
            {
                DateTime dt    = DateTime.Now;
                int[] arrCount = MessageCenter.Instance.CheckListeners();
                int currCount  = arrCount[0];
                //将监听器数量写入全局缓存,供集群调度服务使用
                ICacheProvider cache = CacheProviderFactory.GetGlobalCacheProvider();
                string key           = Program.Host.GetUri() + "_HostInfo";

                ServiceHostInfo serviceHostInfo = cache.Get <ServiceHostInfo>(key, () =>
                {
                    ServiceHostInfo info     = new ServiceHostInfo();
                    info.RegServerDesc       = Program.Host.RegServerDesc;
                    info.RegServerIP         = Program.Host.RegServerIP;
                    info.RegServerPort       = Program.Host.RegServerPort;
                    info.IsActive            = Program.Host.IsActive;
                    info.ServerMappingIP     = Program.Host.ServerMappingIP;
                    info.LogDirectory        = Program.Host.LogDirectory;
                    info.ListenerCount       = currCount;
                    info.ListenerMaxCount    = currCount;
                    info.ListenerMaxDateTime = DateTime.Now;
                    info.ActiveConnectCount  = arrCount[1];
                    return(info);
                },
                                                                              new System.Runtime.Caching.CacheItemPolicy()
                {
                    SlidingExpiration = new TimeSpan(0, 10, 0)
                }
                                                                              );

                bool changed = false;
                int maxCount = serviceHostInfo.ListenerMaxCount;

                if (currCount > maxCount)
                {
                    changed  = true;
                    maxCount = currCount;
                    serviceHostInfo.ListenerCount       = currCount;
                    serviceHostInfo.ListenerMaxCount    = maxCount;
                    serviceHostInfo.ListenerMaxDateTime = DateTime.Now;
                }
                else if (currCount != serviceHostInfo.ListenerCount)
                {
                    changed = true;
                    serviceHostInfo.ListenerCount = currCount;
                }

                if (changed)
                {
                    serviceHostInfo.ActiveConnectCount = arrCount[1];

                    Host.ListenerCount       = serviceHostInfo.ListenerCount;
                    Host.ListenerMaxCount    = serviceHostInfo.ListenerMaxCount;
                    Host.ListenerMaxDateTime = serviceHostInfo.ListenerMaxDateTime;
                    Host.ActiveConnectCount  = serviceHostInfo.ActiveConnectCount;

                    cache.Insert <ServiceHostInfo>(
                        key,
                        serviceHostInfo,
                        new System.Runtime.Caching.CacheItemPolicy()
                    {
                        SlidingExpiration = new TimeSpan(0, 1, 0)
                    }
                        );
                }
                Console.WriteLine("=========监听器数量统计:当前{0}个,最大{1}个,用时{2} ms ============", currCount, maxCount, DateTime.Now.Subtract(dt).TotalMilliseconds);
            }), null, 1000, 10000);
        }