添加分配和未分片的测试
This commit is contained in:
parent
04d85c9660
commit
42914f8734
|
@ -61,13 +61,14 @@ namespace Sample.MySql.Controllers
|
||||||
[Route("[controller]/[action]")]
|
[Route("[controller]/[action]")]
|
||||||
public class WeatherForecastController : ControllerBase
|
public class WeatherForecastController : ControllerBase
|
||||||
{
|
{
|
||||||
|
private readonly UnShardingDbContext _unShardingDbContext;
|
||||||
private readonly DefaultShardingDbContext _defaultTableDbContext;
|
private readonly DefaultShardingDbContext _defaultTableDbContext;
|
||||||
private readonly IShardingRuntimeContext _shardingRuntimeContext;
|
private readonly IShardingRuntimeContext _shardingRuntimeContext;
|
||||||
private readonly ABC _abc;
|
private readonly ABC _abc;
|
||||||
|
|
||||||
public WeatherForecastController(DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext)
|
public WeatherForecastController(UnShardingDbContext unShardingDbContext,DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext)
|
||||||
{
|
{
|
||||||
|
_unShardingDbContext = unShardingDbContext;
|
||||||
_defaultTableDbContext = defaultTableDbContext;
|
_defaultTableDbContext = defaultTableDbContext;
|
||||||
_shardingRuntimeContext = shardingRuntimeContext;
|
_shardingRuntimeContext = shardingRuntimeContext;
|
||||||
_abc=new ABC(_defaultTableDbContext);
|
_abc=new ABC(_defaultTableDbContext);
|
||||||
|
@ -383,5 +384,41 @@ namespace Sample.MySql.Controllers
|
||||||
}).ToListAsync();
|
}).ToListAsync();
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> Get14()
|
||||||
|
{
|
||||||
|
var sysUserLogByMonths = new List<SysUserLogByMonth>();
|
||||||
|
for (int i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
var sysUserLogByMonth = new SysUserLogByMonth();
|
||||||
|
sysUserLogByMonth.Id = Guid.NewGuid().ToString("n");
|
||||||
|
sysUserLogByMonth.Time=DateTime.Now;
|
||||||
|
sysUserLogByMonths.Add(sysUserLogByMonth);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _defaultTableDbContext.AddRangeAsync(sysUserLogByMonths);
|
||||||
|
await _defaultTableDbContext.SaveChangesAsync();
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> Get15()
|
||||||
|
{
|
||||||
|
var sysUserLogByMonths = new List<SysUserLogByMonth>();
|
||||||
|
for (int i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
var sysUserLogByMonth = new SysUserLogByMonth();
|
||||||
|
sysUserLogByMonth.Id = Guid.NewGuid().ToString("n");
|
||||||
|
sysUserLogByMonth.Time=DateTime.Now;
|
||||||
|
sysUserLogByMonths.Add(sysUserLogByMonth);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _unShardingDbContext.AddRangeAsync(sysUserLogByMonths);
|
||||||
|
await _unShardingDbContext.SaveChangesAsync();
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Sample.MySql.Domain.Maps;
|
||||||
|
|
||||||
|
namespace Sample.MySql.DbContexts
|
||||||
|
{
|
||||||
|
|
||||||
|
public class UnShardingDbContext:DbContext
|
||||||
|
{
|
||||||
|
public UnShardingDbContext(DbContextOptions<UnShardingDbContext> options):base(options)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
namespace Sample.MySql.Domain.Entities
|
||||||
|
{
|
||||||
|
public class RangeOrder
|
||||||
|
{
|
||||||
|
public long OrderId { get; set; }
|
||||||
|
public string OrderNo { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,60 @@
|
||||||
|
using Sample.MySql.Domain.Entities;
|
||||||
|
using ShardingCore.Core.EntityMetadatas;
|
||||||
|
using ShardingCore.Core.VirtualRoutes;
|
||||||
|
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
|
||||||
|
|
||||||
|
namespace Sample.MySql.Shardings
|
||||||
|
{
|
||||||
|
public class RangeOrderTableRoute : AbstractShardingOperatorVirtualTableRoute<RangeOrder, long>
|
||||||
|
{
|
||||||
|
public override string ShardingKeyToTail(object shardingKey)
|
||||||
|
{
|
||||||
|
//取商
|
||||||
|
var value = ((long)shardingKey) / (1000 * 10000);
|
||||||
|
return value.ToString().PadLeft(4, '0'); //左补零 range_order_0000 range_order_0001 range_order_0002
|
||||||
|
}
|
||||||
|
|
||||||
|
public override List<string> GetTails()
|
||||||
|
{
|
||||||
|
//查询拨号器现在是多少位
|
||||||
|
long nextId = 0;
|
||||||
|
var id = nextId / (1000 * 10000);
|
||||||
|
var tails = new List<string>();
|
||||||
|
for (int i = 0; i <= id; i++)
|
||||||
|
{
|
||||||
|
var tail = i.ToString().PadLeft(4, '0');
|
||||||
|
tails.Add(tail);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tails;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Configure(EntityMetadataTableBuilder<RangeOrder> builder)
|
||||||
|
{
|
||||||
|
builder.ShardingProperty(o => o.OrderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Func<string, bool> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
|
||||||
|
{
|
||||||
|
//当前值是对应表后缀多少
|
||||||
|
var t = ShardingKeyToTail(shardingKey);
|
||||||
|
switch (shardingOperator)
|
||||||
|
{
|
||||||
|
case ShardingOperatorEnum.GreaterThan:
|
||||||
|
case ShardingOperatorEnum.GreaterThanOrEqual:
|
||||||
|
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
|
||||||
|
case ShardingOperatorEnum.LessThan:
|
||||||
|
{
|
||||||
|
return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0;
|
||||||
|
}
|
||||||
|
case ShardingOperatorEnum.LessThanOrEqual:
|
||||||
|
return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0;
|
||||||
|
case ShardingOperatorEnum.Equal: return tail => tail == t;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
return tail => true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -70,7 +70,10 @@ namespace Sample.MySql
|
||||||
// });
|
// });
|
||||||
//
|
//
|
||||||
|
|
||||||
|
services.AddDbContext<UnShardingDbContext>(o =>
|
||||||
|
o.UseMySql(
|
||||||
|
"server=127.0.0.1;port=3306;database=dbdbdxx;userid=root;password=root;",new MySqlServerVersion(
|
||||||
|
new Version())));
|
||||||
services.AddShardingDbContext<DefaultShardingDbContext>()
|
services.AddShardingDbContext<DefaultShardingDbContext>()
|
||||||
.UseRouteConfig((sp, o) =>
|
.UseRouteConfig((sp, o) =>
|
||||||
{
|
{
|
||||||
|
@ -186,7 +189,15 @@ namespace Sample.MySql
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
app.ApplicationServices.UseAutoTryCompensateTable();
|
app.ApplicationServices.UseAutoTryCompensateTable();
|
||||||
|
using (var scope = app.ApplicationServices.CreateScope())
|
||||||
|
{
|
||||||
|
var unShardingDbContext = scope.ServiceProvider.GetService<UnShardingDbContext>();
|
||||||
|
unShardingDbContext.Database.EnsureCreated();
|
||||||
|
// var shardingRuntimeContext = defaultShardingDbContext.GetShardingRuntimeContext();
|
||||||
|
// var tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
|
||||||
|
// var virtualTableRoute = (SysUserLogByMonthRoute)tableRouteManager.GetRoute(typeof(SysUserLogByMonth));
|
||||||
|
// virtualTableRoute.Append("2021");
|
||||||
|
}
|
||||||
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
|
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
|
||||||
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
||||||
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
|
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
|
||||||
|
|
Loading…
Reference in New Issue