提交.Net6版本

This commit is contained in:
橙子
2021-12-25 14:50:54 +08:00
parent aebf12a7ca
commit 6503ad905b
443 changed files with 17839 additions and 712 deletions

View File

@@ -0,0 +1,72 @@
using Consul;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Common.IOCOptions;
namespace Yi.Framework.Core.ConsulExtend
{
public abstract class AbstractConsulDispatcher
{
protected ConsulClientOption _ConsulClientOption = null;
protected KeyValuePair<string, AgentService>[] _CurrentAgentServiceDictionary = null;
public AbstractConsulDispatcher(IOptionsMonitor<ConsulClientOption> consulClientOption)
{
this._ConsulClientOption = consulClientOption.CurrentValue;
}
/// <summary>
/// 负载均衡获取地址
/// </summary>
/// <param name="mappingUrl">Consul映射后的地址</param>
/// <returns></returns>
public string GetAddress(string mappingUrl)
{
Uri uri = new Uri(mappingUrl);
string serviceName = uri.Host;
string addressPort = this.ChooseAddress(serviceName);
return $"{uri.Scheme}://{addressPort}{uri.PathAndQuery}";
}
protected virtual string ChooseAddress(string serviceName)
{
ConsulClient client = new ConsulClient(c =>
{
c.Address = new Uri($"http://{this._ConsulClientOption.IP}:{this._ConsulClientOption.Port}/");
c.Datacenter = this._ConsulClientOption.Datacenter;
});
AgentService agentService = null;
//var response = client.Agent.Services().Result.Response;
////foreach (var item in response)
////{
//// Console.WriteLine("***************************************");
//// Console.WriteLine(item.Key);
//// var service = item.Value;
//// Console.WriteLine($"{service.Address}--{service.Port}--{service.Service}");
//// Console.WriteLine("***************************************");
////}
//this._CurrentAgentServiceDictionary = response.Where(s => s.Value.Service.Equals(serviceName, StringComparison.OrdinalIgnoreCase)).ToArray();
//升级consul实例获取
var entrys = client.Health.Service(serviceName).Result.Response;
List<KeyValuePair<string, AgentService>> serviceList = new List<KeyValuePair<string, AgentService>>();
for (int i = 0; i < entrys.Length; i++)
{
serviceList.Add(new KeyValuePair<string, AgentService>(i.ToString(), entrys[i].Service));
}
this._CurrentAgentServiceDictionary = serviceList.ToArray();
int index = this.GetIndex();
agentService = this._CurrentAgentServiceDictionary[index].Value;
return $"{agentService.Address}:{agentService.Port}";
}
protected abstract int GetIndex();
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Consul;
using Microsoft.Extensions.Options;
using Yi.Framework.Common.IOCOptions;
namespace Yi.Framework.Core.ConsulExtend
{
/// <summary>
/// 平均
/// </summary>
public class AverageDispatcher : AbstractConsulDispatcher
{
#region Identity
private static int _iTotalCount = 0;
private static int iTotalCount
{
get
{
return _iTotalCount;
}
set
{
_iTotalCount = value >= Int32.MaxValue ? 0 : value;
}
}
public AverageDispatcher(IOptionsMonitor<ConsulClientOption> consulClientOption) : base(consulClientOption)
{
}
#endregion
/// <summary>
/// 平均
/// </summary>
/// <returns></returns>
protected override int GetIndex()
{
return new Random(iTotalCount++).Next(0, base._CurrentAgentServiceDictionary.Length);
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Consul;
using Microsoft.Extensions.Options;
using Yi.Framework.Common.IOCOptions;
namespace Yi.Framework.Core.ConsulExtend
{
/// <summary>
/// 轮询
/// </summary>
public class PollingDispatcher : AbstractConsulDispatcher
{
#region Identity
private static int _iTotalCount = 0;
private static int iTotalCount
{
get
{
return _iTotalCount;
}
set
{
_iTotalCount = value >= Int32.MaxValue ? 0 : value;
}
}
public PollingDispatcher(IOptionsMonitor<ConsulClientOption> consulClientOption) : base(consulClientOption)
{
}
#endregion
/// <summary>
/// 轮询
/// </summary>
/// <param name="serviceCount"></param>
/// <returns></returns>
protected override int GetIndex()
{
return iTotalCount++ % base._CurrentAgentServiceDictionary.Length;
}
}
}

View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Consul;
using Microsoft.Extensions.Options;
using Yi.Framework.Common.IOCOptions;
namespace Yi.Framework.Core.ConsulExtend
{
/// <summary>
/// 权重
/// </summary>
public class WeightDispatcher : AbstractConsulDispatcher
{
#region Identity
private static int _iTotalCount = 0;
private static int iTotalCount
{
get
{
return _iTotalCount;
}
set
{
_iTotalCount = value >= Int32.MaxValue ? 0 : value;
}
}
public WeightDispatcher(IOptionsMonitor<ConsulClientOption> consulClientOption) : base(consulClientOption)
{
}
#endregion
protected override string ChooseAddress(string serviceName)
{
ConsulClient client = new ConsulClient(c =>
{
c.Address = new Uri($"http://{base._ConsulClientOption.IP}:{base._ConsulClientOption.Port}/");
c.Datacenter = base._ConsulClientOption.Datacenter;
});
AgentService agentService = null;
var response = client.Agent.Services().Result.Response;
this._CurrentAgentServiceDictionary = response.Where(s => s.Value.Service.Equals(serviceName, StringComparison.OrdinalIgnoreCase)).ToArray();
var serviceDictionaryNew = new List<AgentService>();
foreach (var service in base._CurrentAgentServiceDictionary)
{
serviceDictionaryNew.AddRange(Enumerable.Repeat(service.Value, int.TryParse(service.Value.Tags?[0], out int iWeight) ? 1 : iWeight));
}
int index = new Random(DateTime.Now.Millisecond).Next(0, int.MaxValue) % serviceDictionaryNew.Count;
agentService = serviceDictionaryNew[index];
return $"{agentService.Address}:{agentService.Port}";
}
/// <summary>
/// 不需要了
/// </summary>
/// <returns></returns>
protected override int GetIndex()
{
throw new NotImplementedException();
}
}
}