140 lines
6.0 KiB
C#
Executable File
140 lines
6.0 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared.Data
|
|
{
|
|
public class ConfigurationEndpointProvider : DictionaryEndpointProvider
|
|
{
|
|
private static string dataString;
|
|
|
|
private static DataAccessOptions oldAccessOptions;
|
|
|
|
private static IDictionary<string, DbEndpoint> _endpoints = new Dictionary<string, DbEndpoint>();
|
|
|
|
private static IDictionary<string, DbEndpoint> _endpointsSet = new Dictionary<string, DbEndpoint>();
|
|
|
|
private static IDictionary<string, DbEndpoint> _endpointsUnSet = new Dictionary<string, DbEndpoint>();
|
|
|
|
public ConfigurationEndpointProvider(IConfiguration configuration)
|
|
: base(Convert(configuration))
|
|
{
|
|
ConfigurationEndpointProvider configurationEndpointProvider = this;
|
|
ChangeToken.OnChange(() => configuration.GetReloadToken(), delegate
|
|
{
|
|
configurationEndpointProvider.ReloadOptions(configuration);
|
|
configurationEndpointProvider.TriggerEvent(new EndpointChangedEventArgs
|
|
{
|
|
ChangedEndpoints = _endpointsSet,
|
|
DeleteEndpoints = _endpointsUnSet
|
|
});
|
|
});
|
|
}
|
|
|
|
private bool ReloadOptions(IConfiguration configuration)
|
|
{
|
|
string text = configuration["OPBase.Data"];
|
|
if (string.Equals(text, dataString))
|
|
{
|
|
return false;
|
|
}
|
|
//Log.Info("重新加载当前项目" + configuration["APP_UK"] + "OPBase.Cache项:" + text);
|
|
dataString = text;
|
|
try
|
|
{
|
|
_endpointsSet.Clear();
|
|
_endpointsUnSet.Clear();
|
|
DataAccessOptions oldDataAccessConfig = new DataAccessOptions();
|
|
DataAccessOptions dataAccessOptions = ConfigConvert.XmlToConfig(text, oldDataAccessConfig);
|
|
if (string.IsNullOrWhiteSpace(text) || dataAccessOptions == null || !dataAccessOptions.Databases.Any())
|
|
{
|
|
_endpointsUnSet = _endpoints;
|
|
_endpoints.Clear();
|
|
return true;
|
|
}
|
|
Dictionary<string, DbEndpoint> dictionary = new Dictionary<string, DbEndpoint>();
|
|
foreach (KeyValuePair<string, DatabaseOptions> database in dataAccessOptions.Databases)
|
|
{
|
|
DbEndpoint dbEndpoint = new DbEndpoint();
|
|
dbEndpoint.DatabaseOptions = database.Value;
|
|
dictionary.Add(database.Key, dbEndpoint);
|
|
}
|
|
_endpointsSet = dictionary;
|
|
_endpoints = dictionary;
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//Log.Error(ex, "OPBase.Data配置项解析失败,请联系DBA");
|
|
throw new InvalidOperationException("OPBase.Data配置项解析失败,请联系DBA", ex);
|
|
}
|
|
}
|
|
|
|
private static IDictionary<string, DbEndpoint> Convert(IConfiguration configuration)
|
|
{
|
|
string text = (dataString = configuration["OPBase.Data"]);
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
{
|
|
//Log.Error("当前项目" + configuration["APP_UK"] + "OPBase.Data项,请联系DBA配置数据库连接");
|
|
throw new InvalidOperationException("当前项目" + configuration["APP_UK"] + "OPBase.Data项,请联系DBA配置数据库连接");
|
|
}
|
|
//Log.Info("获取到当前项目" + configuration["APP_UK"] + "统一配置OPBase.Data项配置:" + text);
|
|
try
|
|
{
|
|
DataAccessOptions oldDataAccessConfig = oldAccessOptions ?? new DataAccessOptions();
|
|
foreach (KeyValuePair<string, DatabaseOptions> database in (oldAccessOptions = ConfigConvert.XmlToConfig(text, oldDataAccessConfig)).Databases)
|
|
{
|
|
DbEndpoint dbEndpoint = new DbEndpoint();
|
|
dbEndpoint.DatabaseOptions = database.Value;
|
|
_endpoints.Add(database.Key, dbEndpoint);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//Log.Error(ex, "OPBase.Data配置项解析失败,请联系DBA");
|
|
throw new InvalidOperationException("OPBase.Data配置项解析失败,请联系DBA", ex);
|
|
}
|
|
return _endpoints;
|
|
}
|
|
|
|
private static ConnectionOptions GetReadConnectionConfig(DatabaseOptions databaseConfig)
|
|
{
|
|
if (databaseConfig.ReadConnections.Count < 2)
|
|
{
|
|
return databaseConfig.ReadConnections.FirstOrDefault();
|
|
}
|
|
int num = Interlocked.Increment(ref databaseConfig.ReadSeed);
|
|
int num2 = RoundRobin(databaseConfig.ReadConnections, num);
|
|
if (num2 < num)
|
|
{
|
|
Interlocked.CompareExchange(ref databaseConfig.ReadSeed, num2, num);
|
|
}
|
|
return databaseConfig.ReadConnections[num2];
|
|
}
|
|
|
|
private static ConnectionOptions GetWriteConnectionConfig(DatabaseOptions databaseConfig)
|
|
{
|
|
if (databaseConfig.WriteConnections.Count < 2)
|
|
{
|
|
return databaseConfig.WriteConnections.FirstOrDefault();
|
|
}
|
|
int num = Interlocked.Increment(ref databaseConfig.WriteSeed);
|
|
int num2 = RoundRobin(databaseConfig.WriteConnections, num);
|
|
if (num2 < num)
|
|
{
|
|
Interlocked.CompareExchange(ref databaseConfig.WriteSeed, num2, num);
|
|
}
|
|
return databaseConfig.WriteConnections[num2];
|
|
}
|
|
|
|
private static int RoundRobin(List<ConnectionOptions> connectionConfigs, int seed)
|
|
{
|
|
return seed % connectionConfigs.Count;
|
|
}
|
|
}
|
|
|
|
}
|