////// 加权轮询算法 /// public static class WeightRoundRobin { private static object obj = new object(); private static int pos = 0; static Dictionarydic = new Dictionary { { "192.168.1.12", 1}, { "192.168.1.13", 1 }, { "192.168.1.14", 3}, { "192.168.1.15", 1}, { "192.168.1.16", 1}, { "192.168.1.17", 1 }, { "192.168.1.18", 1}, { "192.168.1.19", 1} }; public static string roundRobin() { //獲取ip列表list List it = dic.Keys.ToList(); List serverList = new List (); foreach (var item in it) { int weight = 0; dic.TryGetValue(item, out weight); for (int i = 0; i < weight; i++) { serverList.Add(item); } } string server = null; lock (obj) { if (pos >= serverList.Count) { pos = 0; } server = serverList[pos]; pos++; } return server; } }