简体   繁体   English

使用C#检查负载平衡服务器

[英]Check Load Balancing server using C#

I have four application server for my application.Application is working on all server using load balancing.If one of my server goes down I have to check it manually using my system hosts file.To avoid this manual process I have created one program using C#.I write server IP address one by one in host file and remove previous one. 我的应用程序有四个应用程序服务器。应用程序正在使用负载平衡的所有服务器上运行。如果我的服务器之一发生故障,则必须使用系统主机文件手动检查它。为避免此手动过程,我使用C#创建了一个程序我在主机文件中一一写入服务器IP地址,然后删除前一个。

 private void RunWithUAC()
    {
        List<string> lstIPAddress = new List<string>();
        lstIPAddress.Add("1.1.1.1 example.com");
        lstIPAddress.Add("1.1.1.1 example.com");
        lstIPAddress.Add("1.1.1.1 example.com");
        lstIPAddress.Add("1.1.1.1 example.com");
        var systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
        Console.WriteLine(systemPath);
        var path = @"C:\Windows\System32\drivers\etc\hosts";

        foreach (var item in lstIPAddress)
        {
            System.IO.File.WriteAllText(path, string.Empty);
            try
            {
                File.WriteAllText(path, item);

                WebRequest request = WebRequest.Create("https://example.com");
                request.Timeout = 10000;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception)
            {
                MessageBox.Show(item);
            }
            Thread.Sleep(1000);
        }
    }

But When second server goes down.It will give me timeout error for third server. 但是当第二台服务器关闭时,它将给我第三台服务器超时错误。 Please check the code and let me know what is wrong with this code. 请检查代码,让我知道此代码出了什么问题。

Probably some kind of connection pooling, HTTP pipelining or keep-alive. 可能是某种类型的连接池,HTTP流水线或保持活动状态。 This is the wrong approach in the first place. 首先,这是错误的方法。

Connect directly to the right IP ( WebRequest.Create("https://1.1.1.1") ). 直接连接到正确的IP( WebRequest.Create("https://1.1.1.1") )。 If you need to send a Host header add that manually to the request . 如果您需要发送Host标头, 请将其手动添加到请求中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM