15 changed files with 266 additions and 35 deletions
@ -0,0 +1,51 @@ |
|||||
|
using LINGYUN.ApiGateway.Utils; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Ocelot.LoadBalancer.LoadBalancers; |
||||
|
using Ocelot.Multiplexer; |
||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp; |
||||
|
using Volo.Abp.Application.Dtos; |
||||
|
|
||||
|
namespace LINGYUN.ApiGateway.Controllers |
||||
|
{ |
||||
|
[RemoteService(Name = ApiGatewayConsts.RemoteServiceName)] |
||||
|
[Area("ApiGateway")] |
||||
|
[Route("api/ApiGateway/Basic")] |
||||
|
public class ApiGatewayController : Controller |
||||
|
{ |
||||
|
protected IServiceProvider ServiceProvider { get; } |
||||
|
protected ILoadBalancerFinder LoadBalancerFinder { get; } |
||||
|
public ApiGatewayController( |
||||
|
IServiceProvider serviceProvider, |
||||
|
ILoadBalancerFinder loadBalancerFinder) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
LoadBalancerFinder = loadBalancerFinder; |
||||
|
} |
||||
|
|
||||
|
[HttpGet] |
||||
|
[Route("Aggregators")] |
||||
|
public Task<JsonResult> GetAggregatorsAsync() |
||||
|
{ |
||||
|
var aggregators = ServiceProvider.GetServices<IDefinedAggregator>(); |
||||
|
|
||||
|
var aggregatorDtos = new ListResultDto<string>(aggregators.Select(agg => agg.GetType().Name).ToList()); |
||||
|
|
||||
|
return Task.FromResult(Json(aggregatorDtos)); |
||||
|
} |
||||
|
|
||||
|
[HttpGet] |
||||
|
[Route("LoadBalancers")] |
||||
|
public async Task<JsonResult> GetLoadBalancersAsync() |
||||
|
{ |
||||
|
var loadBalancers = await LoadBalancerFinder.GetLoadBalancersAsync(); |
||||
|
|
||||
|
var loadBalancerDtos = new ListResultDto<LoadBalancerDescriptor>(loadBalancers); |
||||
|
|
||||
|
return Json(loadBalancerDtos); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
{ |
||||
|
"culture": "en", |
||||
|
"texts": { |
||||
|
"NoLoadBalancer": "No Load balancer", |
||||
|
"RoundRobin": "Round robin", |
||||
|
"LeastConnection": "Least connection", |
||||
|
"CookieStickySessions": "Cookie sticky sessions", |
||||
|
"CustomLoadBalancer": "Custom load balancer - {0}" |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
{ |
||||
|
"culture": "zh-Hans", |
||||
|
"texts": { |
||||
|
"NoLoadBalancer": "无负载均衡", |
||||
|
"RoundRobin": "服务器轮询", |
||||
|
"LeastConnection": "总是空闲服务器", |
||||
|
"CookieStickySessions": "粘性会话类型", |
||||
|
"CustomLoadBalancer": "自定义负载均衡器 - {0}" |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using Microsoft.Extensions.Localization; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace LINGYUN.ApiGateway.Utils |
||||
|
{ |
||||
|
public interface ILoadBalancerFinder |
||||
|
{ |
||||
|
Task<List<LoadBalancerDescriptor>> GetLoadBalancersAsync(); |
||||
|
} |
||||
|
|
||||
|
public class LoadBalancerDescriptor |
||||
|
{ |
||||
|
public string Type { get; } |
||||
|
public string DisplayName { get; } |
||||
|
public LoadBalancerDescriptor(string type, string displayName) |
||||
|
{ |
||||
|
Type = type; |
||||
|
DisplayName = displayName; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,58 @@ |
|||||
|
using LINGYUN.ApiGateway.Localization; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Localization; |
||||
|
using Ocelot.LoadBalancer.LoadBalancers; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace LINGYUN.ApiGateway.Utils |
||||
|
{ |
||||
|
public class LoadBalancerFinder : ILoadBalancerFinder, ISingletonDependency |
||||
|
{ |
||||
|
private Lazy<List<LoadBalancerDescriptor>> lazyLoadBalancers; |
||||
|
protected List<LoadBalancerDescriptor> LoadBalancers => lazyLoadBalancers.Value; |
||||
|
protected IServiceProvider ServiceProvider { get; } |
||||
|
protected IStringLocalizer Localizer { get; } |
||||
|
public LoadBalancerFinder( |
||||
|
IServiceProvider serviceProvider, |
||||
|
IStringLocalizer<ApiGatewayResource> localizer) |
||||
|
{ |
||||
|
Localizer = localizer; |
||||
|
ServiceProvider = serviceProvider; |
||||
|
|
||||
|
lazyLoadBalancers = new Lazy<List<LoadBalancerDescriptor>>(() => FindLocalLoadBalancers()); |
||||
|
} |
||||
|
|
||||
|
public Task<List<LoadBalancerDescriptor>> GetLoadBalancersAsync() |
||||
|
{ |
||||
|
return Task.FromResult(LoadBalancers); |
||||
|
} |
||||
|
|
||||
|
protected List<LoadBalancerDescriptor> FindLocalLoadBalancers() |
||||
|
{ |
||||
|
List<LoadBalancerDescriptor> loadBalancers = new List<LoadBalancerDescriptor> |
||||
|
{ |
||||
|
new LoadBalancerDescriptor(typeof(NoLoadBalancer).Name, Localizer["NoLoadBalancer"]), |
||||
|
new LoadBalancerDescriptor(typeof(RoundRobin).Name, Localizer["RoundRobin"]), |
||||
|
new LoadBalancerDescriptor(typeof(LeastConnection).Name, Localizer["LeastConnection"]), |
||||
|
new LoadBalancerDescriptor(typeof(CookieStickySessions).Name, Localizer["CookieStickySessions"]) |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
var loadBalancerCreators = ServiceProvider.GetServices<ILoadBalancerCreator>(); |
||||
|
loadBalancerCreators = loadBalancerCreators |
||||
|
.Where(lbc => !loadBalancers.Any(l => l.Type.Equals(lbc.Type))) |
||||
|
.ToArray(); |
||||
|
|
||||
|
foreach(var defintedLoadBalancerCreator in loadBalancerCreators) |
||||
|
{ |
||||
|
loadBalancers.Add(new LoadBalancerDescriptor(defintedLoadBalancerCreator.Type, Localizer["CustomLoadBalancer", defintedLoadBalancerCreator.Type])); |
||||
|
} |
||||
|
|
||||
|
return loadBalancers; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue