From 592a5016d89efddc2663fb5881d75bba295a7e0c Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Thu, 27 Apr 2023 17:09:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=81=9A=E5=90=88=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E5=88=B07.x.1.8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nuget-publish.bat | 10 ++--- .../Controllers/WeatherForecastController.cs | 6 ++- .../DbContexts/DefaultShardingDbContext.cs | 2 +- .../Sample.MySql/Domain/Entities/SysTest.cs | 1 + .../Sample.MySql/Domain/Maps/SysUserModMap.cs | 2 +- samples/Sample.MySql/NoReadWriteMiddleware.cs | 37 +++++++++++++++++++ samples/Sample.MySql/Startup.cs | 2 +- .../Core/VirtualRoutes/ShardingRouteResult.cs | 2 - .../RoutingRuleEngine/TableRouteRuleEngine.cs | 1 + .../ShardingExecutors/ShardingExecutor.cs | 5 +++ 10 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 samples/Sample.MySql/NoReadWriteMiddleware.cs diff --git a/nuget-publish.bat b/nuget-publish.bat index ce541ef4..fa87bc34 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,10 +1,10 @@ :start ::定义版本 -set EFCORE7=7.7.1.7 -set EFCORE6=7.6.1.7 -set EFCORE5=7.5.1.7 -set EFCORE3=7.3.1.7 -set EFCORE2=7.2.1.7 +set EFCORE7=7.7.1.8 +set EFCORE6=7.6.1.8 +set EFCORE5=7.5.1.8 +set EFCORE3=7.3.1.8 +set EFCORE2=7.2.1.8 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index a8560418..6e571c6c 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -9,6 +9,7 @@ using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions; +using ShardingCore.Extensions; using ShardingCore.Extensions.ShardingPageExtensions; using ShardingCore.Extensions.ShardingQueryableExtensions; using ShardingCore.Helpers; @@ -78,8 +79,9 @@ namespace Sample.MySql.Controllers //如果你已经添加好了的情况下并且没有生成对应的库和表想要生成表和库 var dataSourceInitializer = _shardingRuntimeContext.GetDataSourceInitializer(); dataSourceInitializer.InitConfigure("ds9",true,true); - - + + // _defaultTableDbContext.ReadWriteSeparationReadOnly();//读库 + // _defaultTableDbContext.ReadWriteSeparationWriteOnly();//写库 var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator(); var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager(); //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 diff --git a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs index 1b164d91..110c2ab3 100644 --- a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs @@ -40,7 +40,7 @@ namespace Sample.MySql.DbContexts modelBuilder.ApplyConfiguration(new SysTestMap()); modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap()); - // modelBuilder.Entity().HasData(new SysUserLogByMonth() { Id = "1", Time = DateTime.Now }); + modelBuilder.Entity().HasData(new SysUserLogByMonth() { Id = "1", Time = DateTime.Now }); // modelBuilder.Entity().HasData(new SysTest() { Id = "1", UserId = "123" }); // modelBuilder.Entity().ToTable(nameof(TestMod)); // modelBuilder.Entity().ToTable("xxx"); diff --git a/samples/Sample.MySql/Domain/Entities/SysTest.cs b/samples/Sample.MySql/Domain/Entities/SysTest.cs index cfa91034..16a3cddf 100644 --- a/samples/Sample.MySql/Domain/Entities/SysTest.cs +++ b/samples/Sample.MySql/Domain/Entities/SysTest.cs @@ -8,5 +8,6 @@ { public string Id { get; set; } public string UserId { get; set; } + } } \ No newline at end of file diff --git a/samples/Sample.MySql/Domain/Maps/SysUserModMap.cs b/samples/Sample.MySql/Domain/Maps/SysUserModMap.cs index 0d629c94..14bed4b6 100644 --- a/samples/Sample.MySql/Domain/Maps/SysUserModMap.cs +++ b/samples/Sample.MySql/Domain/Maps/SysUserModMap.cs @@ -15,7 +15,7 @@ namespace Sample.MySql.Domain.Maps public void Configure(EntityTypeBuilder builder) { builder.HasKey(o => o.Id); - builder.Property(o => o.Id).IsRequired().HasMaxLength(128); + builder.Property(o => o.Id).ValueGeneratedNever().IsRequired().HasMaxLength(128); builder.Property(o => o.Name).HasMaxLength(128); builder.ToTable(nameof(SysUserMod)); } diff --git a/samples/Sample.MySql/NoReadWriteMiddleware.cs b/samples/Sample.MySql/NoReadWriteMiddleware.cs new file mode 100644 index 00000000..6963ea06 --- /dev/null +++ b/samples/Sample.MySql/NoReadWriteMiddleware.cs @@ -0,0 +1,37 @@ +using Sample.MySql.DbContexts; +using ShardingCore.Core.RuntimeContexts; +using ShardingCore.Extensions; + +namespace Sample.MySql +{ + + public class NoReadWriteMiddleware + { + private readonly RequestDelegate _next; + + public NoReadWriteMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task Invoke(HttpContext context) + { + if (context.Request.Path.ToString().StartsWith("/admin")) + { + var shardingRuntimeContext = context.RequestServices.GetService>(); + var shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); + using (var scope = shardingReadWriteManager.CreateScope()) + { + shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(9999,false); + await _next(context); + } + } + else + { + await _next(context); + } + + } + + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 81813b6f..8a2d1f47 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -93,6 +93,7 @@ namespace Sample.MySql // { // b.UseMemoryCache(memoryCache); // }); + o.IgnoreCreateTableError = false; o.UseEntityFrameworkCoreProxies = true; o.CacheModelLockConcurrencyLevel = 1024; o.CacheEntrySize = 1; @@ -213,7 +214,6 @@ namespace Sample.MySql // app.ApplicationServices.UseAutoTryCompensateTable(12); app.UseRouting(); - app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); diff --git a/src/ShardingCore/Core/VirtualRoutes/ShardingRouteResult.cs b/src/ShardingCore/Core/VirtualRoutes/ShardingRouteResult.cs index b1ec61ba..2debe18b 100644 --- a/src/ShardingCore/Core/VirtualRoutes/ShardingRouteResult.cs +++ b/src/ShardingCore/Core/VirtualRoutes/ShardingRouteResult.cs @@ -15,9 +15,7 @@ namespace ShardingCore.Core.VirtualRoutes public ShardingRouteResult(List routeUnits,bool isEmpty,bool isCrossDataSource,bool isCrossTable,bool existCrossTableTails) { - var routeUnitGroup = routeUnits.GroupBy(o=>o.DataSourceName); RouteUnits = routeUnits; - var count = routeUnitGroup.Count(); IsEmpty =isEmpty; IsCrossDataSource = isCrossDataSource; IsCrossTable = isCrossTable; diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs index ee5ddb08..52902ef5 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs @@ -114,6 +114,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine var routeResults = routeMap.Select(o => o.Value).Cartesian() .Select(o => new TableRouteResult(o.ToList())).Where(o => !o.IsEmpty).ToArray(); + //平行表 var tableRouteResults = GetTableRouteResults(tableRouteRuleContext, routeResults); if (tableRouteResults.IsNotEmpty()) { diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingExecutors/ShardingExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingExecutors/ShardingExecutor.cs index 374922e2..233d77e9 100644 --- a/src/ShardingCore/Sharding/MergeEngines/ShardingExecutors/ShardingExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingExecutors/ShardingExecutor.cs @@ -33,6 +33,11 @@ namespace ShardingCore.Sharding.MergeEngines.ShardingExecutors .ToList(); if (results.IsEmpty()) throw new ShardingCoreException("sharding execute result empty"); + //不需要merge + if (results.Count == 1) + { + return results[0]; + } var streamMerge = executor.GetShardingMerger().StreamMerge(results); return streamMerge; }