惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

博客园 - 飘渺峰

一致性Hash算法 .net Parallel并行使用注意事项 析构函数和Dispose方法的区别 查看SQLServer的最大连接数 Hash算法-CityHash算法 Hash算法 Sunday算法--C#版 KMP算法--C#版 BoyerMoore(BM)算法--C# HostFileChangeMonitor [转]软件项目管理总体流程设计 全排列和组合算法 生活 结束进程的方法forceStopPackage 【转】OAUTH协议简介 SQL Server FOR XML PATH 语句的应用 nlog轻量级日志组件 反射加载程序集的几个方法的区别 windows 下TCP最大连接数
负载均衡算法--C#版
飘渺峰 · 2011-04-17 · via 博客园 - 飘渺峰
public class LoadBalance
    {
        /// <summary>
        /// 锁对象
        /// </summary>
        private static readonly object locker = new object();

        /// <summary>
        /// 服务器权重列表
        /// </summary>
        private static List<int> weightList= new List<int>();

        /// <summary>
        /// 当前索引
        /// </summary>
        private static int currentIndex;

        /// <summary>
        /// 当前权重
        /// </summary>
        private static int currentWeight;

        private static int maxWeight;

        /// <summary>
        /// 最大公约数
        /// </summary>
        private static int gcd;

        static LoadBalance()
        {
            currentIndex = -1;
            currentWeight = 0;

            //获取服务器权重列表,从配置文件
            weightList = GetWeightList();
            maxWeight = GetMaxWeight(weightList);
            gcd = GetMaxGCD(weightList);
        }
        private static List<int> GetWeightList()
        {
            List<int> list= new List<int>();
            list.Add(3);
            list.Add(1);
            list.Add(1);
            list.Add(4);
            list.Add(1);
            list.Add(7);
            
            return list;
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public static int Start()
        {
            lock (locker)
            {
                int? iWeight = RoundRobin();
                if (iWeight != null)
                {
                    return (int)iWeight;
                }
                return weightList[0];
            }
        }


        /// <summary>
        /// 获取最大公约数
        /// </summary>
        /// <param name="list">要查找的int集合</param>
        /// <returns>返回集合中所有数的最大公约数</returns>
        private static int GetMaxGCD(List<int> list)
        {
            list.Sort(new WeightCompare());

            int iMinWeight = weightList[0];

            int gcd = 1;
            
            for (int i = 1; i < iMinWeight; i++)
            {
                bool isFound = true;
                foreach (int iWeight in list)
                {
                    if(iWeight %  i != 0)
                    {
                        isFound = false;
                        break;
                    }
                }
                if (isFound) gcd = i;
            }
            return gcd;
        }


        /// <summary>
        /// 获取服务器权重集合中的最大权重
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static int GetMaxWeight(List<int> list)
        {
            int iMaxWeight = 0;
            foreach (int i in list)
            {
                if (iMaxWeight < i) iMaxWeight = i;
            }
            return iMaxWeight;
        }

        private static int? RoundRobin()
        {
            while (true)
            {
                currentIndex = (currentIndex + 1)%weightList.Count;
                if(0 == currentIndex)
                {
                    currentWeight = currentWeight - gcd;
                    if(0 >= currentWeight)
                    {
                        currentWeight = maxWeight;
                        if (currentWeight == 0) return null;
                    }
                }
            
                if(weightList[currentIndex] >= currentWeight)
                {
                    return weightList[currentIndex];
                }
            }
        }

    }

    public class WeightCompare : IComparer<int>
    {
        public int Compare(int x, int y)
        {
            return x - y;
        }
    }
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Console.WriteLine("开始......");
            int iweight ;
            for (int i = 0; i < 17; i++)
            {
                iweight = LoadBalance.Start();
                Console.WriteLine(iweight);
            }
            Console.ReadLine();
        }
    }

posted @ 2011-04-17 19:06  飘渺峰  阅读(1132)  评论()    收藏  举报