框架分层

This commit is contained in:
橙子
2021-10-10 17:30:31 +08:00
parent 945437a2eb
commit cf062cadcd
59 changed files with 2908 additions and 14 deletions

View File

@@ -0,0 +1,71 @@
using Consul;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CC.ElectronicCommerce.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,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Consul;
using Microsoft.Extensions.Options;
namespace CC.ElectronicCommerce.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,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Consul;
using Microsoft.Extensions.Options;
namespace CC.ElectronicCommerce.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,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Consul;
using Microsoft.Extensions.Options;
namespace CC.ElectronicCommerce.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();
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CC.ElectronicCommerce.Core.ConsulExtend
{
/// <summary>
/// 使用Consul时需要配置
/// </summary>
public class ConsulClientOption
{
public string IP { get; set; }
public int Port { get; set; }
public string Datacenter { get; set; }
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CC.ElectronicCommerce.Core.ConsulExtend
{
public class ConsulRegisterOption
{
/// <summary>
/// 服务自身IP
/// </summary>
public string IP { get; set; }
/// <summary>
/// 服务自身Port
/// </summary>
public int Port { get; set; }
/// <summary>
/// 组名称
/// </summary>
public string GroupName { get; set; }
/// <summary>
/// 心跳检查地址
/// </summary>
public string HealthCheckUrl { get; set; }
/// <summary>
/// 心跳频率
/// </summary>
public int Interval { get; set; }
/// <summary>
/// 心跳超时
/// </summary>
public int Timeout { get; set; }
/// <summary>
/// 移除延迟时间
/// </summary>
public int DeregisterCriticalServiceAfter { get; set; }
/// <summary>
/// 标签,额外信息,用于权重
/// </summary>
public string Tag { get; set; }
}
}

View File

@@ -0,0 +1,69 @@
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CC.ElectronicCommerce.Core.ConsulExtend
{
/// <summary>
/// HTTP模式
/// </summary>
public static class ConsulRegiterExtend
{
/// <summary>
/// 自动读取配置文件完成注册
/// </summary>
/// <param name="app"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public static async Task UseConsulConfiguration(this IApplicationBuilder app, IConfiguration configuration)
{
ConsulRegisterOption consulRegisterOption = new ConsulRegisterOption();
configuration.Bind("ConsulRegisterOption", consulRegisterOption);
ConsulClientOption consulClientOption = new ConsulClientOption();
configuration.Bind("ConsulClientOption", consulClientOption);
await UseConsul(app, consulClientOption, consulRegisterOption);
}
/// <summary>
/// 基于提供信息完成注册
/// </summary>
/// <param name="app"></param>
/// <param name="healthService"></param>
/// <returns></returns>
public static async Task UseConsul(this IApplicationBuilder app, ConsulClientOption consulClientOption, ConsulRegisterOption consulRegisterOption)
{
using (ConsulClient client = new ConsulClient(c =>
{
c.Address = new Uri($"http://{consulClientOption.IP}:{consulClientOption.Port}/");
c.Datacenter = consulClientOption.Datacenter;
}))
{
await client.Agent.ServiceRegister(new AgentServiceRegistration()
{
ID = $"{consulRegisterOption.IP}-{consulRegisterOption.Port}-{Guid.NewGuid()}",//唯一Id
Name = consulRegisterOption.GroupName,//组名称-Group
Address = consulRegisterOption.IP,
Port = consulRegisterOption.Port,
Tags = new string[] { consulRegisterOption.Tag },
Check = new AgentServiceCheck()
{
Interval = TimeSpan.FromSeconds(consulRegisterOption.Interval),
HTTP = $"http://{consulRegisterOption.IP}:{consulRegisterOption.Port}{consulRegisterOption.HealthCheckUrl}",
Timeout = TimeSpan.FromSeconds(consulRegisterOption.Timeout),
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(consulRegisterOption.DeregisterCriticalServiceAfter)
}
});
Console.WriteLine($"{JsonConvert.SerializeObject(consulRegisterOption)} 完成注册");
}
}
}
}

View File

@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace CC.ElectronicCommerce.Core.ConsulExtend
{
public static class HealthCheckMiddleware
{
/// <summary>
/// 设置心跳响应
/// </summary>
/// <param name="app"></param>
/// <param name="checkPath">默认是/Health</param>
/// <returns></returns>
public static void UseHealthCheckMiddleware(this IApplicationBuilder app, string checkPath = "/Health")
{
app.Map(checkPath, applicationBuilder => applicationBuilder.Run(async context =>
{
Console.WriteLine($"This is Health Check");
context.Response.StatusCode = (int)HttpStatusCode.OK;
await context.Response.WriteAsync("OK");
}));
}
}
}