负载均衡代码

Posted 天藐水瓶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了负载均衡代码相关的知识,希望对你有一定的参考价值。

    string[] ips = new string[] { "192.168.1.98", "192.168.1.99" };
            for (var i = 0; i < 50; i++)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + ips[i % 2] + "/server.html");
                request.Host = "serveriphost.demo.com";

                using (WebResponse response = request.GetResponse())
                {
                    Console.Write($"{i.ToString().PadLeft(2, ‘ ‘)}:");

                    using (TextReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        var result = reader.ReadToEnd();

                        if (result != ips[i % 2])
                            Console.WriteLine($"Host:{ips[i % 2]},Real:{result}");
                        else
                            Console.WriteLine("same.");
                    }
                }
            }
            Console.WriteLine("End.");
            Console.ReadKey();

以上是关于负载均衡代码的主要内容,如果未能解决你的问题,请参考以下文章

CFS任务的负载均衡(框架篇)

用 Java 代码实现负载均衡的五种常见算法

几种简单的负载均衡算法及其Java代码实现

Nginx反向代理实现负载均衡配置图解

分布式部署与NGINX负载均衡

Nginx负载均衡