Skip to content

Commit

Permalink
[#146]完成指定读写分离读库,有助于读写分离用户的读库设置链接发布x.4.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
xuejmnet committed May 11, 2022
1 parent 98f570c commit 58ee2a7
Show file tree
Hide file tree
Showing 32 changed files with 275 additions and 86 deletions.
8 changes: 4 additions & 4 deletions nuget-publish.bat
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion samples/Sample.MultiConfig/Controllers/CommonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class MySqlConfigurationParam : AbstractVirtualDataSourceConfigurationPar
public override int Priority { get; }
public override string DefaultDataSourceName { get; }
public override string DefaultConnectionString { get; }
public override IDictionary<string, IEnumerable<string>> ReadWriteSeparationConfigs { get; }
public override IDictionary<string, ReadNode[]> ReadWriteNodeSeparationConfigs { get; }
public override ReadStrategyEnum? ReadStrategy { get; }
public override bool? ReadWriteDefaultEnable { get; }
public override int? ReadWriteDefaultPriority { get; }
Expand Down
19 changes: 18 additions & 1 deletion samples/Sample.SqlServer/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using ShardingCore.Sharding.Abstractions;
using Microsoft.EntityFrameworkCore.Query.Internal;
using ShardingCore.EFCores;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;

namespace Sample.SqlServer.Controllers
{
Expand All @@ -34,12 +35,14 @@ public class ValuesController : ControllerBase

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]
Expand Down Expand Up @@ -364,5 +367,19 @@ public async Task<IActionResult> Get4()
return Ok(new { xxx, xxx1 });
}

[HttpGet]
public async Task<IActionResult> Get5(string readNodeName)
{
using (_readWriteManager.CreateScope<DefaultShardingDbContext>())
{
_readWriteManager.GetCurrent<DefaultShardingDbContext>().SetReadWriteSeparation(100,true);

_readWriteManager.GetCurrent<DefaultShardingDbContext>().AddDataSourceReadNode("A", readNodeName);
var xxxaaa = await _defaultTableDbContext.Set<SysUserSalary>().FirstOrDefaultAsync();

}
return Ok();
}

}
}
20 changes: 20 additions & 0 deletions samples/Sample.SqlServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
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;

Expand Down Expand Up @@ -55,6 +57,24 @@ public void ConfigureServices(IServiceCollection services)
op.AddDefaultDataSource("A",
"Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"
);
op.AddReadWriteNodeSeparation(sp =>new Dictionary<string, IEnumerable<ReadNode>>()
{
{"A",new List<ReadNode>()
{
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<DefaultShardingDbContext1>(
// (conn, o) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ public void AddReadWriteSeparation(
ShardingReadWriteSeparationOptions.DefaultPriority= defaultPriority;
ShardingReadWriteSeparationOptions.ReadConnStringGetStrategy= readConnStringGetStrategy;
}
public void AddReadWriteNodeSeparation(
Func<IServiceProvider, IDictionary<string, IEnumerable<ReadNode>>> 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;
}

/// <summary>
/// 多个DbContext事务传播委托
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace ShardingCore.Core.ShardingConfigurations
public class ShardingReadWriteSeparationOptions
{
public Func<IServiceProvider, IDictionary<string, IEnumerable<string>>> ReadWriteSeparationConfigure { get; set; }
public Func<IServiceProvider, IDictionary<string, IEnumerable<ReadNode>>> ReadWriteNodeSeparationConfigure { get; set; }

public ReadStrategyEnum ReadStrategy { get; set; } = ReadStrategyEnum.Loop;
public bool DefaultEnable { get; set; } = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class AbstractVirtualDataSourceConfigurationParams<TShardingDbCo
public abstract string DefaultDataSourceName { get; }
public abstract string DefaultConnectionString { get; }
public virtual IDictionary<string, string> ExtraDataSources { get; }=new ConcurrentDictionary<string, string>();
public virtual IDictionary<string, IEnumerable<string>> ReadWriteSeparationConfigs { get; }
public virtual IDictionary<string, ReadNode[]> ReadWriteNodeSeparationConfigs { get; }
public virtual ReadStrategyEnum? ReadStrategy { get; }
public virtual bool? ReadWriteDefaultEnable { get; }
public virtual int? ReadWriteDefaultPriority { get; }
Expand All @@ -52,7 +52,7 @@ public abstract DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection

public virtual bool UseReadWriteSeparation()
{
return ReadWriteSeparationConfigs!=null;
return ReadWriteNodeSeparationConfigs!=null;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface IVirtualDataSourceConfigurationParams
/// <summary>
/// null表示不启用读写分离,if null mean not enable read write
/// </summary>
IDictionary<string, IEnumerable<string>> ReadWriteSeparationConfigs { get; }
IDictionary<string, ReadNode[]> ReadWriteNodeSeparationConfigs { get; }

ReadStrategyEnum? ReadStrategy { get; }
bool? ReadWriteDefaultEnable { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using ShardingCore.Core.ShardingConfigurations.Abstractions;

Expand All @@ -28,7 +29,7 @@ public class SimpleVirtualDataSourceConfigurationParams<TShardingDbContext>: Abs
public override string DefaultDataSourceName { get; }
public override string DefaultConnectionString { get; }
public override IDictionary<string, string> ExtraDataSources { get; }
public override IDictionary<string, IEnumerable<string>> ReadWriteSeparationConfigs { get; }
public override IDictionary<string, ReadNode[]> ReadWriteNodeSeparationConfigs { get; }
public override ReadStrategyEnum? ReadStrategy { get; }
public override bool? ReadWriteDefaultEnable { get; }
public override int? ReadWriteDefaultPriority { get; }
Expand All @@ -53,7 +54,23 @@ public SimpleVirtualDataSourceConfigurationParams(IServiceProvider serviceProvid
new EmptyTableEnsureManager<TShardingDbContext>();
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;
Expand Down
12 changes: 7 additions & 5 deletions src/ShardingCore/DynamicDataSources/DynamicDataSourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ public static bool DynamicAppendVirtualDataSource<TShardingDbContext>(
/// <param name="virtualDataSource"></param>
/// <param name="dataSourceName"></param>
/// <param name="connectionString"></param>
/// <param name="readNodeName"></param>
/// <exception cref="ShardingCoreInvalidOperationException"></exception>
public static void DynamicAppendReadWriteConnectionString<TShardingDbContext>(IVirtualDataSource<TShardingDbContext> 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;
}

Expand All @@ -82,12 +83,13 @@ public static void DynamicAppendReadWriteConnectionString<TShardingDbContext>(IV
/// <param name="configId"></param>
/// <param name="dataSourceName"></param>
/// <param name="connectionString"></param>
/// <param name="readNodeName"></param>
public static void DynamicAppendReadWriteConnectionString<TShardingDbContext>(string configId, string dataSourceName,
string connectionString) where TShardingDbContext : DbContext, IShardingDbContext
string connectionString, string readNodeName=null) where TShardingDbContext : DbContext, IShardingDbContext
{
var virtualDataSourceManager = ShardingContainer.GetRequiredVirtualDataSourceManager<TShardingDbContext>();
var virtualDataSource = virtualDataSourceManager.GetVirtualDataSource(configId);
DynamicAppendReadWriteConnectionString(virtualDataSource, dataSourceName, connectionString);
DynamicAppendReadWriteConnectionString(virtualDataSource, dataSourceName, connectionString,readNodeName);
}
}
}
12 changes: 7 additions & 5 deletions src/ShardingCore/Helpers/DynamicShardingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ public static void DynamicAppendDataSource<TShardingDbContext>(string configId,
/// <param name="virtualDataSource"></param>
/// <param name="dataSourceName"></param>
/// <param name="connectionString"></param>
/// <param name="readNodeName"></param>
/// <exception cref="ShardingCoreInvalidOperationException"></exception>
public static void DynamicAppendReadWriteConnectionString<TShardingDbContext>(IVirtualDataSource<TShardingDbContext> 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;
}

Expand All @@ -103,12 +104,13 @@ public static void DynamicAppendReadWriteConnectionString<TShardingDbContext>(IV
/// <param name="configId"></param>
/// <param name="dataSourceName"></param>
/// <param name="connectionString"></param>
/// <param name="readNodeName"></param>
public static void DynamicAppendReadWriteConnectionString<TShardingDbContext>(string configId, string dataSourceName,
string connectionString) where TShardingDbContext : DbContext, IShardingDbContext
string connectionString, string readNodeName = null) where TShardingDbContext : DbContext, IShardingDbContext
{
var virtualDataSourceManager = ShardingContainer.GetRequiredVirtualDataSourceManager<TShardingDbContext>();
var virtualDataSource = virtualDataSourceManager.GetVirtualDataSource(configId);
DynamicAppendReadWriteConnectionString(virtualDataSource, dataSourceName, connectionString);
DynamicAppendReadWriteConnectionString(virtualDataSource, dataSourceName, connectionString, readNodeName);
}
}
}
39 changes: 27 additions & 12 deletions src/ShardingCore/Sharding/ActualConnectionStringManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,50 @@ private string GetWriteConnectionString(string dataSourceName)
private string GetReadWriteSeparationConnectString(string dataSourceName)
{
var support = ReadWriteSeparation;
string readNodeName = null;
var hasConfig = false;
var shardingReadWriteContext = _shardingReadWriteManager.GetCurrent<TShardingDbContext>();
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)}]");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

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);
}
}
Loading

0 comments on commit 58ee2a7

Please sign in to comment.