diff --git a/nuget-publish.bat b/nuget-publish.bat index 8bf49b1e..b5368997 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,9 +1,9 @@ :start ::定义版本 -set EFCORE2=2.4.3.1 -set EFCORE3=3.4.3.1 -set EFCORE5=5.4.3.1 -set EFCORE6=6.4.3.1 +set EFCORE2=2.4.3.2 +set EFCORE3=3.4.3.2 +set EFCORE5=5.4.3.2 +set EFCORE6=6.4.3.2 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.MultiConfig/Controllers/CommonController.cs b/samples/Sample.MultiConfig/Controllers/CommonController.cs index 8299ae55..1f485e91 100644 --- a/samples/Sample.MultiConfig/Controllers/CommonController.cs +++ b/samples/Sample.MultiConfig/Controllers/CommonController.cs @@ -33,7 +33,7 @@ namespace Sample.MultiConfig.Controllers public override int Priority { get; } public override string DefaultDataSourceName { get; } public override string DefaultConnectionString { get; } - public override IDictionary> ReadWriteSeparationConfigs { get; } + public override IDictionary ReadWriteNodeSeparationConfigs { get; } public override ReadStrategyEnum? ReadStrategy { get; } public override bool? ReadWriteDefaultEnable { get; } public override int? ReadWriteDefaultPriority { get; } diff --git a/samples/Sample.SqlServer/Controllers/ValuesController.cs b/samples/Sample.SqlServer/Controllers/ValuesController.cs index dec7b4fc..9e021e24 100644 --- a/samples/Sample.SqlServer/Controllers/ValuesController.cs +++ b/samples/Sample.SqlServer/Controllers/ValuesController.cs @@ -20,6 +20,7 @@ using ShardingCore.Extensions.ShardingQueryableExtensions; using ShardingCore.Sharding.Abstractions; using Microsoft.EntityFrameworkCore.Query.Internal; using ShardingCore.EFCores; +using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; namespace Sample.SqlServer.Controllers { @@ -34,12 +35,14 @@ namespace Sample.SqlServer.Controllers private readonly DefaultShardingDbContext _defaultTableDbContext; private readonly IShardingRouteManager _shardingRouteManager; + private readonly IShardingReadWriteManager _readWriteManager; - public ValuesController(DefaultShardingDbContext defaultTableDbContext, IShardingRouteManager shardingRouteManager) + public ValuesController(DefaultShardingDbContext defaultTableDbContext, IShardingRouteManager shardingRouteManager,IShardingReadWriteManager readWriteManager) { _defaultTableDbContext = defaultTableDbContext; _ = defaultTableDbContext.Model; _shardingRouteManager = shardingRouteManager; + _readWriteManager = readWriteManager; } [HttpGet] @@ -364,5 +367,19 @@ namespace Sample.SqlServer.Controllers return Ok(new { xxx, xxx1 }); } + [HttpGet] + public async Task Get5(string readNodeName) + { + using (_readWriteManager.CreateScope()) + { + _readWriteManager.GetCurrent().SetReadWriteSeparation(100,true); + + _readWriteManager.GetCurrent().AddDataSourceReadNode("A", readNodeName); + var xxxaaa = await _defaultTableDbContext.Set().FirstOrDefaultAsync(); + + } + return Ok(); + } + } } diff --git a/samples/Sample.SqlServer/Startup.cs b/samples/Sample.SqlServer/Startup.cs index a17d81c1..6ec251d4 100644 --- a/samples/Sample.SqlServer/Startup.cs +++ b/samples/Sample.SqlServer/Startup.cs @@ -10,9 +10,11 @@ using Sample.SqlServer.UnionAllMerge; using ShardingCore; using ShardingCore.TableExists; using System; +using System.Collections.Generic; using System.Diagnostics; using Microsoft.Extensions.DependencyInjection.Extensions; using ShardingCore.Core.DbContextCreator; +using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ShardingComparision; using ShardingCore.Sharding.ShardingComparision.Abstractions; @@ -55,6 +57,24 @@ namespace Sample.SqlServer op.AddDefaultDataSource("A", "Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;" ); + op.AddReadWriteNodeSeparation(sp =>new Dictionary>() + { + {"A",new List() + { + new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A2","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A3","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A4","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A5","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A6","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"), + new ReadNode("X","Data Source=localhost;Initial Catalog=ShardingCoreDBXA123;Integrated Security=True;"), + }} + },ReadStrategyEnum.Loop); }).EnsureConfig(); //services.AddShardingDbContext( // (conn, o) => diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs index deb2ad0f..97d7057e 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs @@ -85,6 +85,20 @@ namespace ShardingCore.Core.ShardingConfigurations ShardingReadWriteSeparationOptions.DefaultPriority= defaultPriority; ShardingReadWriteSeparationOptions.ReadConnStringGetStrategy= readConnStringGetStrategy; } + public void AddReadWriteNodeSeparation( + Func>> readWriteNodeSeparationConfigure, + ReadStrategyEnum readStrategyEnum, + bool defaultEnable = false, + int defaultPriority = 10, + ReadConnStringGetStrategyEnum readConnStringGetStrategy = ReadConnStringGetStrategyEnum.LatestFirstTime) + { + ShardingReadWriteSeparationOptions = new ShardingReadWriteSeparationOptions(); + ShardingReadWriteSeparationOptions.ReadWriteNodeSeparationConfigure= readWriteNodeSeparationConfigure ?? throw new ArgumentNullException(nameof(readWriteNodeSeparationConfigure)); + ShardingReadWriteSeparationOptions.ReadStrategy = readStrategyEnum; + ShardingReadWriteSeparationOptions.DefaultEnable=defaultEnable; + ShardingReadWriteSeparationOptions.DefaultPriority= defaultPriority; + ShardingReadWriteSeparationOptions.ReadConnStringGetStrategy= readConnStringGetStrategy; + } /// /// 多个DbContext事务传播委托 diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingReadWriteSeparationOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingReadWriteSeparationOptions.cs index e824ea7e..1aab8567 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingReadWriteSeparationOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingReadWriteSeparationOptions.cs @@ -7,6 +7,7 @@ namespace ShardingCore.Core.ShardingConfigurations public class ShardingReadWriteSeparationOptions { public Func>> ReadWriteSeparationConfigure { get; set; } + public Func>> ReadWriteNodeSeparationConfigure { get; set; } public ReadStrategyEnum ReadStrategy { get; set; } = ReadStrategyEnum.Loop; public bool DefaultEnable { get; set; } = false; diff --git a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/AbstractVirtualDataSourceConfigurationParams.cs b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/AbstractVirtualDataSourceConfigurationParams.cs index 3dfbac10..eff83fac 100644 --- a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/AbstractVirtualDataSourceConfigurationParams.cs +++ b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/AbstractVirtualDataSourceConfigurationParams.cs @@ -28,7 +28,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions public abstract string DefaultDataSourceName { get; } public abstract string DefaultConnectionString { get; } public virtual IDictionary ExtraDataSources { get; }=new ConcurrentDictionary(); - public virtual IDictionary> ReadWriteSeparationConfigs { get; } + public virtual IDictionary ReadWriteNodeSeparationConfigs { get; } public virtual ReadStrategyEnum? ReadStrategy { get; } public virtual bool? ReadWriteDefaultEnable { get; } public virtual int? ReadWriteDefaultPriority { get; } @@ -52,7 +52,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions public virtual bool UseReadWriteSeparation() { - return ReadWriteSeparationConfigs!=null; + return ReadWriteNodeSeparationConfigs!=null; } } diff --git a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/IVirtualDataSourceConfigurationParams.cs b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/IVirtualDataSourceConfigurationParams.cs index 077d1e62..b059a8cc 100644 --- a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/IVirtualDataSourceConfigurationParams.cs +++ b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/IVirtualDataSourceConfigurationParams.cs @@ -42,7 +42,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions /// /// null表示不启用读写分离,if null mean not enable read write /// - IDictionary> ReadWriteSeparationConfigs { get; } + IDictionary ReadWriteNodeSeparationConfigs { get; } ReadStrategyEnum? ReadStrategy { get; } bool? ReadWriteDefaultEnable { get; } diff --git a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs index 1db66476..2bb640aa 100644 --- a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs +++ b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Data.Common; +using System.Linq; using Microsoft.Extensions.DependencyInjection; using ShardingCore.Core.ShardingConfigurations.Abstractions; @@ -28,7 +29,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources public override string DefaultDataSourceName { get; } public override string DefaultConnectionString { get; } public override IDictionary ExtraDataSources { get; } - public override IDictionary> ReadWriteSeparationConfigs { get; } + public override IDictionary ReadWriteNodeSeparationConfigs { get; } public override ReadStrategyEnum? ReadStrategy { get; } public override bool? ReadWriteDefaultEnable { get; } public override int? ReadWriteDefaultPriority { get; } @@ -53,7 +54,23 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources new EmptyTableEnsureManager(); if (options.ShardingReadWriteSeparationOptions != null) { - ReadWriteSeparationConfigs = options.ShardingReadWriteSeparationOptions.ReadWriteSeparationConfigure?.Invoke(serviceProvider); + if (options.ShardingReadWriteSeparationOptions.ReadWriteNodeSeparationConfigure != null) + { + var readConfig = options.ShardingReadWriteSeparationOptions.ReadWriteNodeSeparationConfigure?.Invoke(serviceProvider); + if (readConfig != null) + { + ReadWriteNodeSeparationConfigs = readConfig.ToDictionary(kv=>kv.Key,kv=>kv.Value.ToArray()); + } + } + else + { + var nodeConfig = options.ShardingReadWriteSeparationOptions.ReadWriteSeparationConfigure?.Invoke(serviceProvider); + if (nodeConfig != null) + { + ReadWriteNodeSeparationConfigs = nodeConfig.ToDictionary(kv => kv.Key, + kv => kv.Value.Select(o => new ReadNode(Guid.NewGuid().ToString("n"), o)).ToArray()); + } + } ReadStrategy = options.ShardingReadWriteSeparationOptions.ReadStrategy; ReadWriteDefaultEnable = options.ShardingReadWriteSeparationOptions.DefaultEnable; ReadWriteDefaultPriority = options.ShardingReadWriteSeparationOptions.DefaultPriority; diff --git a/src/ShardingCore/DynamicDataSources/DynamicDataSourceHelper.cs b/src/ShardingCore/DynamicDataSources/DynamicDataSourceHelper.cs index 73e3b370..d29d9e4b 100644 --- a/src/ShardingCore/DynamicDataSources/DynamicDataSourceHelper.cs +++ b/src/ShardingCore/DynamicDataSources/DynamicDataSourceHelper.cs @@ -61,14 +61,15 @@ namespace ShardingCore.DynamicDataSources /// /// /// + /// /// public static void DynamicAppendReadWriteConnectionString(IVirtualDataSource virtualDataSource, string dataSourceName, - string connectionString) where TShardingDbContext : DbContext, IShardingDbContext + string connectionString,string readNodeName = null) where TShardingDbContext : DbContext, IShardingDbContext { - if (virtualDataSource.ConnectionStringManager is IReadWriteAppendConnectionString + if (virtualDataSource.ConnectionStringManager is IReadWriteConnectionStringManager readWriteAppendConnectionString) { - readWriteAppendConnectionString.AddReadConnectionString(dataSourceName, connectionString); + readWriteAppendConnectionString.AddReadConnectionString(dataSourceName, connectionString, readNodeName); return; } @@ -82,12 +83,13 @@ namespace ShardingCore.DynamicDataSources /// /// /// + /// public static void DynamicAppendReadWriteConnectionString(string configId, string dataSourceName, - string connectionString) where TShardingDbContext : DbContext, IShardingDbContext + string connectionString, string readNodeName=null) where TShardingDbContext : DbContext, IShardingDbContext { var virtualDataSourceManager = ShardingContainer.GetRequiredVirtualDataSourceManager(); var virtualDataSource = virtualDataSourceManager.GetVirtualDataSource(configId); - DynamicAppendReadWriteConnectionString(virtualDataSource, dataSourceName, connectionString); + DynamicAppendReadWriteConnectionString(virtualDataSource, dataSourceName, connectionString,readNodeName); } } } diff --git a/src/ShardingCore/Helpers/DynamicShardingHelper.cs b/src/ShardingCore/Helpers/DynamicShardingHelper.cs index bb708919..6adac062 100644 --- a/src/ShardingCore/Helpers/DynamicShardingHelper.cs +++ b/src/ShardingCore/Helpers/DynamicShardingHelper.cs @@ -82,14 +82,15 @@ namespace ShardingCore.Helpers /// /// /// + /// /// public static void DynamicAppendReadWriteConnectionString(IVirtualDataSource virtualDataSource, string dataSourceName, - string connectionString) where TShardingDbContext : DbContext, IShardingDbContext + string connectionString, string readNodeName=null) where TShardingDbContext : DbContext, IShardingDbContext { - if (virtualDataSource.ConnectionStringManager is IReadWriteAppendConnectionString + if (virtualDataSource.ConnectionStringManager is IReadWriteConnectionStringManager readWriteAppendConnectionString) { - readWriteAppendConnectionString.AddReadConnectionString(dataSourceName, connectionString); + readWriteAppendConnectionString.AddReadConnectionString(dataSourceName, connectionString, readNodeName); return; } @@ -103,12 +104,13 @@ namespace ShardingCore.Helpers /// /// /// + /// public static void DynamicAppendReadWriteConnectionString(string configId, string dataSourceName, - string connectionString) where TShardingDbContext : DbContext, IShardingDbContext + string connectionString, string readNodeName = null) where TShardingDbContext : DbContext, IShardingDbContext { var virtualDataSourceManager = ShardingContainer.GetRequiredVirtualDataSourceManager(); var virtualDataSource = virtualDataSourceManager.GetVirtualDataSource(configId); - DynamicAppendReadWriteConnectionString(virtualDataSource, dataSourceName, connectionString); + DynamicAppendReadWriteConnectionString(virtualDataSource, dataSourceName, connectionString, readNodeName); } } } diff --git a/src/ShardingCore/Sharding/ActualConnectionStringManager.cs b/src/ShardingCore/Sharding/ActualConnectionStringManager.cs index 7db0352b..52caefcd 100644 --- a/src/ShardingCore/Sharding/ActualConnectionStringManager.cs +++ b/src/ShardingCore/Sharding/ActualConnectionStringManager.cs @@ -65,35 +65,50 @@ namespace ShardingCore.Sharding private string GetReadWriteSeparationConnectString(string dataSourceName) { var support = ReadWriteSeparation; + string readNodeName = null; + var hasConfig = false; var shardingReadWriteContext = _shardingReadWriteManager.GetCurrent(); if (shardingReadWriteContext != null) { - support = (ReadWriteSeparationPriority >= shardingReadWriteContext.DefaultPriority) + var dbFirst = ReadWriteSeparationPriority >= shardingReadWriteContext.DefaultPriority; + support = dbFirst ? ReadWriteSeparation : shardingReadWriteContext.DefaultReadEnable; + if (!dbFirst&& support) + { + hasConfig = shardingReadWriteContext.TryGetDataSourceReadNode(dataSourceName, out readNodeName); + } } if (support) { - return GetReadWriteSeparationConnectString0(dataSourceName); + return GetReadWriteSeparationConnectString0(dataSourceName, hasConfig?readNodeName:null); } return GetWriteConnectionString(dataSourceName); } - private string GetReadWriteSeparationConnectString0(string dataSourceName) + private string GetReadWriteSeparationConnectString0(string dataSourceName,string readNodeName) { - if (ReadConnStringGetStrategy == ReadConnStringGetStrategyEnum.LatestFirstTime) + if (_virtualDataSource.ConnectionStringManager is IReadWriteConnectionStringManager + readWriteConnectionStringManager) { - if (_cacheConnectionString == null) - _cacheConnectionString = _virtualDataSource.ConnectionStringManager.GetConnectionString(dataSourceName); - return _cacheConnectionString; - } - else if (ReadConnStringGetStrategy == ReadConnStringGetStrategyEnum.LatestEveryTime) - { - return _virtualDataSource.ConnectionStringManager.GetConnectionString(dataSourceName); + if (ReadConnStringGetStrategy == ReadConnStringGetStrategyEnum.LatestFirstTime) + { + if (_cacheConnectionString == null) + _cacheConnectionString = readWriteConnectionStringManager.GetReadNodeConnectionString(dataSourceName, readNodeName); + return _cacheConnectionString; + } + else if (ReadConnStringGetStrategy == ReadConnStringGetStrategyEnum.LatestEveryTime) + { + return readWriteConnectionStringManager.GetReadNodeConnectionString(dataSourceName,readNodeName); + } + else + { + throw new ShardingCoreInvalidOperationException($"ReadWriteConnectionStringManager ReadConnStringGetStrategy:{ReadConnStringGetStrategy}"); + } } else { - throw new ShardingCoreInvalidOperationException($"ReadWriteConnectionStringManager ReadConnStringGetStrategy:{ReadConnStringGetStrategy}"); + throw new ShardingCoreInvalidOperationException($"virtual data source connection string manager is not [{nameof(IReadWriteConnectionStringManager)}]"); } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteAppendConnectionString.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnectionStringManager.cs similarity index 59% rename from src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteAppendConnectionString.cs rename to src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnectionStringManager.cs index f7486ae6..e15fa4c2 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteAppendConnectionString.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnectionStringManager.cs @@ -6,8 +6,9 @@ using System.Threading.Tasks; namespace ShardingCore.Sharding.ReadWriteConfigurations.Abstractions { - public interface IReadWriteAppendConnectionString + public interface IReadWriteConnectionStringManager { - bool AddReadConnectionString(string dataSourceName,string connectionString); + string GetReadNodeConnectionString(string dataSourceName,string readNodeName); + bool AddReadConnectionString(string dataSourceName,string connectionString, string readNodeName); } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnector.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnector.cs index 9e0275e9..f02b506d 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnector.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnector.cs @@ -15,13 +15,15 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations.Abstractions /// /// 获取链接字符串 /// + /// 可为null /// - public string GetConnectionString(); + public string GetConnectionString(string readNodeName); /// /// 添加链接字符串 /// /// + /// /// - public bool AddConnectionString(string connectionString); + public bool AddConnectionString(string connectionString, string readNodeName); } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnectorFactory.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnectorFactory.cs index 3caae2a9..3b39c1c8 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnectorFactory.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IReadWriteConnectorFactory.cs @@ -11,6 +11,6 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations.Abstractions public interface IReadWriteConnectorFactory { IReadWriteConnector CreateConnector(ReadStrategyEnum strategy, string dataSourceName, - IEnumerable connectionStrings); + ReadNode[] readNodes); } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IShardingConnectionStringResolver.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IShardingConnectionStringResolver.cs index 6eae1521..9d1f9fdd 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IShardingConnectionStringResolver.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/Abstractions/IShardingConnectionStringResolver.cs @@ -19,13 +19,20 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations.Abstractions public interface IShardingConnectionStringResolver { bool ContainsReadWriteDataSourceName(string dataSourceName); - string GetConnectionString(string dataSourceName); + /// + /// 获取指定数据源的读连接名称节点 + /// + /// + /// 名称不存在报错,如果为null那么就随机获取 + /// + string GetConnectionString(string dataSourceName,string readNodeName); /// /// 添加数据源从库读字符串 /// /// /// + /// /// - bool AddConnectionString(string dataSourceName, string connectionString); + bool AddConnectionString(string dataSourceName, string connectionString, string readNodeName); } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/Abstractions/AbstractionReadWriteConnector.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/Abstractions/AbstractionReadWriteConnector.cs index 813fde09..61105925 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/Abstractions/AbstractionReadWriteConnector.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/Abstractions/AbstractionReadWriteConnector.cs @@ -12,43 +12,45 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations.Connectors.Abstractions { public abstract class AbstractionReadWriteConnector:IReadWriteConnector { - protected List ConnectionStrings { get;} + protected List ReadNodes { get;} protected int Length { get; private set; } private object slock = new object(); //private readonly string _tempConnectionString; //private readonly OneByOneChecker _oneByOneChecker = new OneByOneChecker(); - public AbstractionReadWriteConnector(string dataSourceName,IEnumerable connectionStrings) + public AbstractionReadWriteConnector(string dataSourceName,ReadNode[] readNodes) { DataSourceName = dataSourceName; - ConnectionStrings = connectionStrings.ToList(); - Length = ConnectionStrings.Count; + ReadNodes = readNodes.ToList(); + Length = ReadNodes.Count; //_tempConnectionString = ConnectionStrings[0]; } public string DataSourceName { get; } - public string GetConnectionString() + + public string GetConnectionString(string readNodeName) { - return DoGetConnectionString(); + return DoGetConnectionString(readNodeName); } - public abstract string DoGetConnectionString(); + public abstract string DoGetConnectionString(string readNodeName); /// /// 动态添加数据源 /// /// + /// /// - public bool AddConnectionString(string connectionString) + public bool AddConnectionString(string connectionString, string readNodeName) { - var acquired = Monitor.TryEnter(slock,TimeSpan.FromSeconds(3)); + var acquired = Monitor.TryEnter(slock, TimeSpan.FromSeconds(3)); if (!acquired) throw new ShardingCoreInvalidOperationException($"{nameof(AddConnectionString)} is busy"); try { - ConnectionStrings.Add(connectionString); - Length = ConnectionStrings.Count; + ReadNodes.Add(new ReadNode(readNodeName?? Guid.NewGuid().ToString("n"), connectionString)); + Length = ReadNodes.Count; return true; } finally diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/ReadWriteLoopConnector.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/ReadWriteLoopConnector.cs index fa4105ff..c088e90a 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/ReadWriteLoopConnector.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/ReadWriteLoopConnector.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; +using ShardingCore.Exceptions; using ShardingCore.Sharding.ReadWriteConfigurations.Connectors.Abstractions; namespace ShardingCore.Sharding.ReadWriteConfigurations @@ -17,19 +18,32 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations public class ReadWriteLoopConnector: AbstractionReadWriteConnector { private long _seed = 0; - public ReadWriteLoopConnector(string dataSourceName, IEnumerable connectionStrings):base(dataSourceName,connectionStrings) + public ReadWriteLoopConnector(string dataSourceName, ReadNode[] readNodes) :base(dataSourceName, readNodes) { } - public override string DoGetConnectionString() + private string DoGetNoReadNameConnectionString() { if (Length == 1) - return ConnectionStrings[0]; + return ReadNodes[0].ConnectionString; var newValue = Interlocked.Increment(ref _seed); var next = (int)(newValue % Length); if (next < 0) - return ConnectionStrings[Math.Abs(next)]; - return ConnectionStrings[next]; + return ReadNodes[Math.Abs(next)].ConnectionString; + return ReadNodes[next].ConnectionString; + } + + public override string DoGetConnectionString(string readNodeName) + { + if (readNodeName == null) + { + return DoGetNoReadNameConnectionString(); + } + else + { + return ReadNodes.FirstOrDefault(o => o.Name == readNodeName)?.ConnectionString ?? + throw new ShardingCoreInvalidOperationException($"read node name :[{readNodeName}] not found"); + } } } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/ReadWriteRandomConnector.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/ReadWriteRandomConnector.cs index f6f9c965..68020924 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/ReadWriteRandomConnector.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/Connectors/ReadWriteRandomConnector.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using ShardingCore.Exceptions; using ShardingCore.Helpers; using ShardingCore.Sharding.ReadWriteConfigurations.Connectors.Abstractions; @@ -16,17 +17,28 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations */ public class ReadWriteRandomConnector:AbstractionReadWriteConnector { - public ReadWriteRandomConnector(string dataSourceName,IEnumerable connectionStrings):base(dataSourceName,connectionStrings) + public ReadWriteRandomConnector(string dataSourceName,ReadNode[] readNodes):base(dataSourceName, readNodes) { } - public override string DoGetConnectionString() + private string DoGetNoReadNameConnectionString() { if (Length == 1) - return ConnectionStrings[0]; + return ReadNodes[0].ConnectionString; var next = RandomHelper.Next(0, Length); - return ConnectionStrings[next]; + return ReadNodes[next].ConnectionString; } + public override string DoGetConnectionString(string readNodeName) + { + if (readNodeName == null) + { + return DoGetNoReadNameConnectionString(); + }else + { + return ReadNodes.FirstOrDefault(o => o.Name == readNodeName)?.ConnectionString ?? + throw new ShardingCoreInvalidOperationException($"read node name :[{readNodeName}] not found"); + } + } } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadNode.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadNode.cs new file mode 100644 index 00000000..59f2c71e --- /dev/null +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadNode.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShardingCore.Sharding.ReadWriteConfigurations +{ + /// + /// + /// + /// Author: xjm + /// Created: 2022/5/11 8:31:38 + /// Email: 326308290@qq.com + public class ReadNode + { + /// + /// 当前读库节点名称 + /// + public string Name { get; } + /// + /// 当前读库链接的连接字符串 + /// + public string ConnectionString { get; } + + public ReadNode(string name,string connectionString) + { + Name = name??throw new ArgumentNullException("read node name is null"); + ConnectionString = connectionString; + } + } +} diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteConnectionStringManager.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteConnectionStringManager.cs index 47aa02e4..e525d536 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteConnectionStringManager.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteConnectionStringManager.cs @@ -16,7 +16,7 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ReadWriteConnectionStringManager: IConnectionStringManager, IReadWriteAppendConnectionString + public class ReadWriteConnectionStringManager: IConnectionStringManager, IReadWriteConnectionStringManager { private IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IVirtualDataSource _virtualDataSource; @@ -26,20 +26,25 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations { _virtualDataSource = virtualDataSource; var readWriteConnectorFactory = ShardingContainer.GetService(); - var readWriteConnectors = virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o=> readWriteConnectorFactory.CreateConnector(virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key,o.Value)); + var readWriteConnectors = virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o=> readWriteConnectorFactory.CreateConnector(virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key,o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); } public string GetConnectionString(string dataSourceName) { - if (!_shardingConnectionStringResolver.ContainsReadWriteDataSourceName(dataSourceName)) - return _virtualDataSource.GetConnectionString(dataSourceName); - return _shardingConnectionStringResolver.GetConnectionString(dataSourceName); + return GetReadNodeConnectionString(dataSourceName,null); } - public bool AddReadConnectionString(string dataSourceName, string connectionString) + public string GetReadNodeConnectionString(string dataSourceName, string readNodeName) { - return _shardingConnectionStringResolver.AddConnectionString(dataSourceName, connectionString); + if (!_shardingConnectionStringResolver.ContainsReadWriteDataSourceName(dataSourceName)) + return _virtualDataSource.GetConnectionString(dataSourceName); + return _shardingConnectionStringResolver.GetConnectionString(dataSourceName, readNodeName); + } + + public bool AddReadConnectionString(string dataSourceName, string connectionString, string readNodeName) + { + return _shardingConnectionStringResolver.AddConnectionString(dataSourceName, connectionString, readNodeName); } } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteConnectorFactory.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteConnectorFactory.cs index 38fd96a4..8f68345b 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteConnectorFactory.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteConnectorFactory.cs @@ -12,16 +12,16 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations { public class ReadWriteConnectorFactory: IReadWriteConnectorFactory { - public IReadWriteConnector CreateConnector(ReadStrategyEnum strategy,string dataSourceName, IEnumerable connectionStrings) + public IReadWriteConnector CreateConnector(ReadStrategyEnum strategy,string dataSourceName, ReadNode[] readNodes) { if (strategy == ReadStrategyEnum.Loop) { - return new ReadWriteLoopConnector(dataSourceName, connectionStrings); + return new ReadWriteLoopConnector(dataSourceName, readNodes); } else if (strategy == ReadStrategyEnum.Random) { - return new ReadWriteRandomConnector(dataSourceName, connectionStrings); + return new ReadWriteRandomConnector(dataSourceName, readNodes); } else { diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteShardingConnectionStringResolver.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteShardingConnectionStringResolver.cs index 37c0e7cf..a6367ace 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteShardingConnectionStringResolver.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteShardingConnectionStringResolver.cs @@ -35,27 +35,26 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations return _connectors.ContainsKey(dataSourceName); } - public string GetConnectionString(string dataSourceName) + public string GetConnectionString(string dataSourceName, string readNodeName) { if (!_connectors.TryGetValue(dataSourceName, out var connector)) throw new ShardingCoreInvalidOperationException($"read write connector not found, data source name:[{dataSourceName}]"); - return connector.GetConnectionString(); + return connector.GetConnectionString(readNodeName); } - - public bool AddConnectionString(string dataSourceName, string connectionString) + public bool AddConnectionString(string dataSourceName, string connectionString, string readNodeName) { if (!_connectors.TryGetValue(dataSourceName, out var connector)) { connector = _readWriteConnectorFactory.CreateConnector(_readStrategy, - dataSourceName, new List() + dataSourceName, new ReadNode[] { - connectionString + new ReadNode(readNodeName??Guid.NewGuid().ToString("n"),connectionString) }); return _connectors.TryAdd(dataSourceName, connector); } else { - return connector.AddConnectionString(connectionString); + return connector.AddConnectionString(connectionString, readNodeName); } } } diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/ShardingReadWriteContext.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/ShardingReadWriteContext.cs index 2b7b29f0..32170aff 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/ShardingReadWriteContext.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/ShardingReadWriteContext.cs @@ -15,16 +15,43 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations { public bool DefaultReadEnable { get; set; } public int DefaultPriority { get; set; } + private readonly Dictionary _dataSourceReadNode; private ShardingReadWriteContext() { DefaultReadEnable = false; DefaultPriority = 0; + _dataSourceReadNode = new Dictionary(); } public static ShardingReadWriteContext Create() { return new ShardingReadWriteContext(); } + /// + /// 添加数据源对应读节点获取名称 + /// + /// + /// + /// + public bool AddDataSourceReadNode(string dataSource, string readNodeName) + { + if (_dataSourceReadNode.ContainsKey(dataSource)) + { + return false; + } + _dataSourceReadNode.Add(dataSource, readNodeName); + return true; + } + /// + /// 尝试获取对应数据源的读节点名称 + /// + /// + /// + /// + public bool TryGetDataSourceReadNode(string dataSource,out string readNodeName) + { + return _dataSourceReadNode.TryGetValue(dataSource, out readNodeName); + } } } diff --git a/test/ShardingCore.Test/ShardingTest.cs b/test/ShardingCore.Test/ShardingTest.cs index 6ad9b3a1..2630805c 100644 --- a/test/ShardingCore.Test/ShardingTest.cs +++ b/test/ShardingCore.Test/ShardingTest.cs @@ -79,7 +79,7 @@ namespace ShardingCore.Test _routeTailFactory = routeTailFactory; _readWriteConnectorFactory = readWriteConnectorFactory; _tableRouteRuleEngineFactory = tableRouteRuleEngineFactory; - var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); + var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); } [Fact] @@ -229,9 +229,9 @@ namespace ShardingCore.Test { new ParallelTableComparerType(typeof(SysUserSalary)),new ParallelTableComparerType(typeof(SysUserMod)), }); Assert.Equal(x1x1, x2x2); Assert.Equal(x1x1.GetHashCode(), x2x2.GetHashCode()); - var succeedAddConnectionString = _shardingConnectionStringResolver.AddConnectionString("X", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;"); + var succeedAddConnectionString = _shardingConnectionStringResolver.AddConnectionString("X", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;",null); Assert.True(succeedAddConnectionString); - var connectionString = _shardingConnectionStringResolver.GetConnectionString("X"); + var connectionString = _shardingConnectionStringResolver.GetConnectionString("X", null); Assert.Equal("Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;", connectionString); } diff --git a/test/ShardingCore.Test/ShardingTestSync.cs b/test/ShardingCore.Test/ShardingTestSync.cs index e8d110f1..e6f8ee22 100644 --- a/test/ShardingCore.Test/ShardingTestSync.cs +++ b/test/ShardingCore.Test/ShardingTestSync.cs @@ -66,7 +66,7 @@ namespace ShardingCore.Test _shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = routeTailFactory; _readWriteConnectorFactory = readWriteConnectorFactory; - var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); + var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); } [Fact] diff --git a/test/ShardingCore.Test2x/ShardingTest.cs b/test/ShardingCore.Test2x/ShardingTest.cs index ddddddbf..2f626e9c 100644 --- a/test/ShardingCore.Test2x/ShardingTest.cs +++ b/test/ShardingCore.Test2x/ShardingTest.cs @@ -73,7 +73,7 @@ namespace ShardingCore.Test2x _shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = routeTailFactory; _readWriteConnectorFactory = readWriteConnectorFactory; - var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); + var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); } [Fact] diff --git a/test/ShardingCore.Test2x/ShardingTestSync.cs b/test/ShardingCore.Test2x/ShardingTestSync.cs index 68290c39..578ea84a 100644 --- a/test/ShardingCore.Test2x/ShardingTestSync.cs +++ b/test/ShardingCore.Test2x/ShardingTestSync.cs @@ -66,7 +66,7 @@ namespace ShardingCore.Test2x _shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = routeTailFactory; _readWriteConnectorFactory = readWriteConnectorFactory; - var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); + var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); } [Fact] diff --git a/test/ShardingCore.Test3x/ShardingTest.cs b/test/ShardingCore.Test3x/ShardingTest.cs index 351466f0..0cee91a5 100644 --- a/test/ShardingCore.Test3x/ShardingTest.cs +++ b/test/ShardingCore.Test3x/ShardingTest.cs @@ -73,7 +73,7 @@ namespace ShardingCore.Test3x _shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = routeTailFactory; _readWriteConnectorFactory = readWriteConnectorFactory; - var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); + var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); } [Fact] diff --git a/test/ShardingCore.Test3x/ShardingTestSync.cs b/test/ShardingCore.Test3x/ShardingTestSync.cs index b640a385..b680054c 100644 --- a/test/ShardingCore.Test3x/ShardingTestSync.cs +++ b/test/ShardingCore.Test3x/ShardingTestSync.cs @@ -67,7 +67,7 @@ namespace ShardingCore.Test3x _shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = routeTailFactory; _readWriteConnectorFactory = readWriteConnectorFactory; - var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); + var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); } [Fact] diff --git a/test/ShardingCore.Test5x/ShardingTest.cs b/test/ShardingCore.Test5x/ShardingTest.cs index fb84b50f..8d30541d 100644 --- a/test/ShardingCore.Test5x/ShardingTest.cs +++ b/test/ShardingCore.Test5x/ShardingTest.cs @@ -73,7 +73,7 @@ namespace ShardingCore.Test5x _shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = routeTailFactory; _readWriteConnectorFactory = readWriteConnectorFactory; - var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); + var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); } [Fact] diff --git a/test/ShardingCore.Test5x/ShardingTestSync.cs b/test/ShardingCore.Test5x/ShardingTestSync.cs index 937054d4..0d189c35 100644 --- a/test/ShardingCore.Test5x/ShardingTestSync.cs +++ b/test/ShardingCore.Test5x/ShardingTestSync.cs @@ -67,7 +67,7 @@ namespace ShardingCore.Test5x _shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = routeTailFactory; _readWriteConnectorFactory = readWriteConnectorFactory; - var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); + var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); }