c#-反向代理负载均衡-最简实践

概要介绍
什么是反向代理负载均衡
原理:反向代理处于web服务器这边,反向代理服务器提供负载均衡的功能,同时管理一组web服务器,它根据负载均衡算法将请求的浏览器访问转发到不同的web服务器处理,处理结果经过反向服务器返回给浏览器。

在这里插入图片描述

例如:浏览器访问请求的地址是反向代理服务器的地址114.100.80.10,反向代理服务器收到请求,经过负载均衡算法后得到一个真实物理地址10.0.03,并将请求结果发给真实无服务,真实服务器处理完后通过反向代理服务器返回给请求用户。
优点:部署简单,处于http协议层面。
缺点:使用了反向代理服务器后,web 服务器地址不能直接暴露在外,因此web服务器不需要使用外部IP地址,而反向代理服务作为沟通桥梁就需要配置双网卡、外部内部两套IP地址。
一 运行效果
1.服务启动(真正处理数据的服务器)

在这里插入图片描述

2 负载均衡服务器启动

在这里插入图片描述

3 客户端发送请求

在这里插入图片描述

4.服务器响应(负载均衡服务器响应略)

在这里插入图片描述

5 客户端响应
 在这里插入图片描述

二 代码分析
1.客户端

using System;
using System.Net.Http;
using System.Net;
using System.IO;
using System.Threading.Tasks;

namespace httpClient
{
    class Program
    {
        HttpClient client = new HttpClient();
        static void Main(string[] args)
        {
            Console.WriteLine("httpClient");
            Program p = new Program();
            p.Main();
            Console.ReadLine();


        }
        private async Task Main() {
            try
            {
                HttpResponseMessage response = await client.GetAsync("http://localhost:8080/web/?name=ddddd");
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                // Above three lines can be replaced with new helper method below
                // string responseBody = await client.GetStringAsync(uri);

                Console.WriteLine(responseBody);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", e.Message);
            }
        }
    }
}

2 负载均衡服务器

using System;
using System.Net;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace 反向代理负载均衡
{
    class Program
    {
        static public HttpClient client = new HttpClient();
        static void Main(string[] args)
        {
            using (HttpListener listerner = new HttpListener())
            {
                listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
                listerner.Prefixes.Add("http://localhost:8080/web/");

                // listerner.Prefixes.Add("http://localhost/web/");
                listerner.Start();
                Console.WriteLine("服务已经启动");
                while (true)
                {
                    //等待请求连接
                    //没有请求则GetContext处于阻塞状态
                    HttpListenerContext ctx = listerner.GetContext();
                    ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
                    string name = ctx.Request.QueryString["name"];
                    string url = "http://localhost:8081/web/?name=" + name;
                    Main(ctx,url);
                }
                listerner.Stop();
            }
        }

        public static void NewMethod(HttpListenerContext ctx, string name)
        {
            //使用Writer输出http响应代码
            using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
            {
                writer.WriteLine(name);
                writer.Close();
                ctx.Response.Close();
            }
        }

        public static async Task Main(HttpListenerContext ctx,string url)
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync(url);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                // Above three lines can be replaced with new helper method below
                // string responseBody = await client.GetStringAsync(uri);

                //Console.WriteLine(responseBody);
                NewMethod(ctx, responseBody);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", e.Message);
            }
        }
    }
}

3 服务器

using System;
using System.Net;
using System.IO;

namespace 反向代理负载均衡2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (HttpListener listerner = new HttpListener())
            {
                listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
                listerner.Prefixes.Add("http://localhost:8081/web/");

                // listerner.Prefixes.Add("http://localhost/web/");
                listerner.Start();
                Console.WriteLine("服务已经启动");
                while (true)
                {
                    //等待请求连接
                    //没有请求则GetContext处于阻塞状态
                    HttpListenerContext ctx = listerner.GetContext();
                    ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
                    string name = ctx.Request.QueryString["name"];

                    if (name != null)
                    {
                        Console.WriteLine(name);
                    }
                    NewMethod(ctx, name);

                }
                listerner.Stop();
            }
        }

        private static void NewMethod(HttpListenerContext ctx, string name)
        {
            //使用Writer输出http响应代码
            using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
            {
                writer.WriteLine("代理服务:"+name);
                writer.Close();
                ctx.Response.Close();
            }
        }
    }
}

3 代码分析


在这里插入图片描述