ASP.NET Core3.1 Ocelot负载均衡的实现
副问题[/!--empirenews.page--]
Ocelot可以在每个路由的可用下流处事中实现负载平衡,这使我们更有用地选择下流处事来处理赏罚哀求。负载平衡范例: LeastConnection:按照处事正在处理赏罚哀求量的环境来抉择哪个处事来处理赏罚新哀求,即将新哀求发送到具有起码现有哀求的处事行止理赏罚。算法状态没有漫衍在Ocelot集群中。 RoundRobin:遍历可用处事并发送哀求。算法状态没有漫衍在Ocelot集群中。 NoLoadBalancer:从设置或处事发明中获取第一个可用处事来处理赏罚新哀求。 CookieStickySessions:通过行使Cookie,确保特定的哀求可以或许被分派到特定的处事长举办处理赏罚。 在Ocelot负载平衡项目示例中,通过网关项目标路由LoadBalancerOptions选项可以设置负载平衡范例: { "Routes": [ { //下流路由处事地点 "DownstreamPathTemplate": "/api/values", //下流处事地点会见协议范例http可能https "DownstreamScheme": "http", //下流处事的主机和端口 "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9001 }, { "Host": "localhost", "Port": 9002 } ], //上游处事地点,即下流处事真实会见解点 "UpstreamPathTemplate": "http://www.jb51.net/", //负载平衡范例:轮询 "LoadBalancerOptions": { "Type": "RoundRobin" }, //上游处事HTTP哀求方法,譬喻Get、Post "UpstreamHttpMethod": [ "Get" ] } ] } 新哀求通过上游会见下流处事的时辰,Ocelot会按照LoadBalancerOptions负载平衡选项范例来分发到详细下流处事。 2.处事发明 下面展示怎样行使处事发明来配置路由: { "DownstreamPathTemplate": "/api/posts/{postId}", "DownstreamScheme": "https", "UpstreamPathTemplate": "/posts/{postId}", "UpstreamHttpMethod": [ "Put" ], "ServiceName": "product", "LoadBalancerOptions": { "Type": "LeastConnection" } } 配置此选项后,Ocelot将从处事发明提供措施中查找下流主机和端口,并在全部可用处事中举办负载均衡哀求。假如您从处事发明提供者(领事)中添加和删除处事,Ocelot会遏制挪用已删除的处事,并开始挪用已添加的处事。后续进修处事发明这块常识点时辰会从头再讲授。 3.项目演示 3.1APIGateway项目 该项目通过LoadBalancerOptions设置选项界说处事负载平衡哀求机制,事例项目行使的负载平衡范例是RoundRobin,在Program添加Ocelot支持代码如下: public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) //.UseStartup<Startup>() .UseUrls("http://*:9000") .ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) //添加Ocelot设置文件 .AddJsonFile("configuration.json") .AddEnvironmentVariables(); }) .ConfigureServices(s => { //添加Ocelot处事; s.AddOcelot(); }) .Configure(a => { //行使Ocelot a.UseOcelot().Wait(); }); 3.2APIServicesA和APIServicesB下流处事项目 APIServicesA和APIServicesB项目别离新建两个GET哀求要领,代码别离如下: //APIServicesA [Route("api/[controller]")] public class ValuesController : Controller { // GET api/values [HttpGet] public string Get() { return "From APIServiceA"; } } //APIServicesB [Route("api/[controller]")] public class ValuesController : Controller { // GET api/values [HttpGet] public string Get() { return "From APIServiceB"; } } 通过dotnet run呼吁启动APIGateway项目(网关层) dotnet run --project APIGateway项目路径APIGateway.csproj
通过dotnet run呼吁启动APIServicesA项目 dotnet run --project APIGateway项目路径APIGateway.csproj
通过dotnet run呼吁启动APIServicesB项目 dotnet run --project APIServicesB项目路径APIServicesB.csproj
通过赏识器查察轮询分发给下流处事返回的功效:
负载平衡轮询分发下流处事乐成。 4.自界说负载平衡 Ocelot支持自界说负载平衡的要领。自界说负载平衡的类必要担任ILoadBalancer接口类,下面我们界说一个简朴的负载平衡轮回输出下流处事的示例: public class CustomLoadBalancer : ILoadBalancer { private readonly Func<Task<List<Service>>> _services; private readonly object _lock = new object(); private int _last; public CustomLoadBalancer(Func<Task<List<Service>>> services) { _services = services; } public async Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext) { var services = await _services(); lock (_lock) { if (_last >= services.Count) { _last = 0; } var next = services[_last]; _last++; return new OkResponse<ServiceHostAndPort>(next.HostAndPort); } } public void Release(ServiceHostAndPort hostAndPort) { } } 在Ocelot中注册此类: (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |