1.負(fù)載均衡(Load Balancer)Ocelot可以在每個(gè)路由的可用下游服務(wù)中實(shí)現(xiàn)負(fù)載均衡,這使我們更有效地選擇下游服務(wù)來處理請求。負(fù)載均衡類型: { "Routes": [ { //下游路由服務(wù)地址 "DownstreamPathTemplate": "/api/values", //下游服務(wù)地址訪問協(xié)議類型http或者h(yuǎn)ttps "DownstreamScheme": "http", //下游服務(wù)的主機(jī)和端口 "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9001 }, { "Host": "localhost", "Port": 9002 } ], //上游服務(wù)地址,即下游服務(wù)真實(shí)訪問地址 "UpstreamPathTemplate": "/", //負(fù)載均衡類型:輪詢 "LoadBalancerOptions": { "Type": "RoundRobin" }, //上游服務(wù)HTTP請求方式,例如Get、Post "UpstreamHttpMethod": [ "Get" ] } ] } 新請求通過上游訪問下游服務(wù)的時(shí)候,Ocelot會根據(jù)LoadBalancerOptions負(fù)載均衡選項(xiàng)類型來分發(fā)到具體下游服務(wù)。 2.服務(wù)發(fā)現(xiàn)下面展示如何使用服務(wù)發(fā)現(xiàn)來設(shè)置路由: { "DownstreamPathTemplate": "/api/posts/{postId}", "DownstreamScheme": "https", "UpstreamPathTemplate": "/posts/{postId}", "UpstreamHttpMethod": [ "Put" ], "ServiceName": "product", "LoadBalancerOptions": { "Type": "LeastConnection" } } 設(shè)置此選項(xiàng)后,Ocelot將從服務(wù)發(fā)現(xiàn)提供程序中查找下游主機(jī)和端口,并在所有可用服務(wù)中進(jìn)行負(fù)載平衡請求。如果您從服務(wù)發(fā)現(xiàn)提供者(領(lǐng)事)中添加和刪除服務(wù),Ocelot會停止調(diào)用已刪除的服務(wù),并開始調(diào)用已添加的服務(wù)。后續(xù)學(xué)習(xí)服務(wù)發(fā)現(xiàn)這塊知識點(diǎn)時(shí)候會重新再講解。 3.項(xiàng)目演示3.1APIGateway項(xiàng)目該項(xiàng)目通過LoadBalancerOptions配置選項(xiàng)定義服務(wù)負(fù)載均衡請求機(jī)制,事例項(xiàng)目使用的負(fù)載均衡類型是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服務(wù); s.AddOcelot(); }) .Configure(a => { //使用Ocelot a.UseOcelot().Wait(); }); 3.2APIServicesA和APIServicesB下游服務(wù)項(xiàng)目APIServicesA和APIServicesB項(xiàng)目分別新建兩個(gè)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"; } } 3.3項(xiàng)目運(yùn)行●通過dotnet run命令啟動(dòng)APIGateway項(xiàng)目 dotnet run --project APIGateway項(xiàng)目路徑\APIGateway.csproj
dotnet run --project APIServicesA項(xiàng)目路徑\APIServicesA.csproj
dotnet run --project APIServicesB項(xiàng)目路徑\APIServicesB.csproj
4.自定義負(fù)載均衡Ocelot支持自定義負(fù)載均衡的方法。自定義負(fù)載均衡的類需要繼承ILoadBalancer接口類,下面我們定義一個(gè)簡單的負(fù)載均衡循環(huán)輸出下游服務(wù)的示例: 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中注冊此類: Func<IServiceProvider, DownstreamRoute, IServiceDiscoveryProvider, CustomLoadBalancer> loadBalancerFactoryFunc = (serviceProvider, Route, serviceDiscoveryProvider) => new CustomLoadBalancer(serviceDiscoveryProvider.Get); s.AddOcelot().AddCustomLoadBalancer(loadBalancerFactoryFunc); 最后在路由的LoadBalancerOptions配置選項(xiàng)上修改為CustomLoadBalancer自定義負(fù)載均衡類名: "LoadBalancerOptions": { "Type": "CustomLoadBalancer" } 運(yùn)行項(xiàng)目調(diào)試查看結(jié)果: |
|