Merge pull request #163 from dodu2014/main

完善示例批量功能及数据链接异常说明
This commit is contained in:
xuejmnet 2022-06-24 16:29:38 +08:00 committed by GitHub
commit efb785a005
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 142 additions and 44 deletions

View File

@ -1,9 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using ShardingCore.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using WebApplication1.Data;
using WebApplication1.Data.Helpers;
using WebApplication1.Data.Sharding;
@ -18,27 +16,23 @@ namespace WebApplication1.Data.Extensions
/// <param name="serviceProvider"></param>
public static IServiceProvider InitialDynamicVirtualDataSource(this IServiceProvider serviceProvider)
{
var dblist = JsonFileHelper.Read<List<string>>(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName) ?? new List<string>();
foreach (var key in dblist)
using (var scope = serviceProvider.CreateScope())
{
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{key};");
var db = scope.ServiceProvider.GetRequiredService<AbstaractShardingDbContext>();
db.Database.EnsureCreated();
var dblist = db.TestModelKeys.Select(m => m.Key).ToList();
// 存入到动态库配置文件缓存中
JsonFileHelper.Save(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName, dblist);
// 遍历添加动态数据源
foreach (var key in dblist)
{
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{key};");
}
}
return serviceProvider;
//using (var scope = serviceProvider.CreateScope())
//{
// var db = scope.ServiceProvider.GetRequiredService<AbstaractShardingDbContext>();
// db.Database.EnsureCreated();
// var dbKeys = db.TestModelKeys.ToList();
// if (dbKeys.Any())
// {
// foreach (var item in dbKeys)
// {
// DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", item.Key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{item.Key};");
// }
// }
//}
return serviceProvider;
}
}

View File

@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.6" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.5" />
<PackageReference Include="ShardingCore" Version="6.5.0.9" />
<PackageReference Include="ShardingCore" Version="6.5.0.11" />
</ItemGroup>
</Project>

View File

@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.5" />
<PackageReference Include="ShardingCore" Version="6.5.0.9" />
<PackageReference Include="ShardingCore" Version="6.5.0.11" />
</ItemGroup>
<ItemGroup>

View File

@ -13,7 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.5" />
<PackageReference Include="ShardingCore" Version="6.5.0.9" />
<PackageReference Include="ShardingCore" Version="6.5.0.11" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,40 @@
@page
@model BatchCreateDbKeyModel
@{
ViewData["title"] = "批量增加分库key";
}
<form asp-page="BatchCreateDbKey" method="post">
<div class="mb-3 row">
<label asp-for="CreateModel.Len" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="CreateModel.Len" placeholder="编号长度,左侧自动补零" class="form-control" required />
<div id="emailHelp" class="form-text">编号长度,编号长度不够左侧自动补零.</div>
</div>
</div>
<div class="mb-3 row">
<label asp-for="CreateModel.StarNum" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="CreateModel.StarNum" class="form-control" placeholder="分库key必填" required />
<div id="emailHelp" class="form-text">起始编号,数值型</div>
</div>
</div>
<div class="mb-3 row">
<label asp-for="CreateModel.EndNum" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="CreateModel.EndNum" class="form-control" required />
<div id="emailHelp" class="form-text">结束编号,数值型.</div>
</div>
</div>
<div class="mb-3 row">
<div class="offset-sm-2">
例如长度为3起始为1结束为5则生成的编号为001002003004005
</div>
</div>
<div class="row">
<div class="offset-sm-2">
<button type="submit" class="btn btn-primary">批量新增</button>
<a asp-page="DbKeyMan" class="btn btn-secondary">返回</a>
</div>
</div>
</form>

View File

@ -0,0 +1,74 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ShardingCore.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using WebApplication1.Data;
using WebApplication1.Data.Helpers;
using WebApplication1.Data.Models;
using WebApplication1.Data.Sharding;
namespace WebApplication1.Pages
{
public class BatchCreateDbKeyModel : PageModel
{
private readonly AbstaractShardingDbContext db;
[BindProperty]
public BatchCreateModel CreateModel { get; set; }
public BatchCreateDbKeyModel(AbstaractShardingDbContext db)
{
this.db = db;
}
public void OnGet()
{
CreateModel = new BatchCreateModel();
}
public IActionResult OnPost()
{
var keyList = new List<string>();
for (int i = CreateModel.StarNum; i < CreateModel.EndNum; i++)
{
keyList.Add(i.ToString().PadLeft(CreateModel.Len, '0'));
}
foreach (var item in keyList)
{
db.TestModelKeys.Add(new TestModelKey { Key = item });
}
db.SaveChanges();
foreach (var item in keyList)
{
// 动态新增数据源
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", item, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{item};");
}
// 读取并写入到配置
var dblist = JsonFileHelper.Read<List<string>>(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName);
dblist.AddRange(keyList);
JsonFileHelper.Save(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName, dblist);
return RedirectToPage("DbKeyMan");
}
}
public class BatchCreateModel
{
[Display(Name = "编号长度")]
public int Len { get; set; } = 3;
[Display(Name = "起始编号")]
public int StarNum { get; set; } = 1;
[Display(Name = "结束编号")]
public int EndNum { get; set; } = 300;
}
}

View File

@ -6,23 +6,12 @@
<form asp-page="CreateDbKey" method="post">
<div class="mb-3 row">
<label asp-for="NewModel.Id" class="col-sm-2 col-form-label"></label>
<label asp-for="Key" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="NewModel.Id" class="form-control" readonly />
</div>
</div>
<div class="mb-3 row">
<label asp-for="NewModel.Key" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="NewModel.Key" class="form-control" placeholder="分库key必填" required />
</div>
</div>
<div class="mb-3 row">
<label asp-for="NewModel.CreationDate" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="NewModel.CreationDate" class="form-control" readonly />
<input asp-for="Key" class="form-control" placeholder="分库key必填" required />
</div>
</div>
<div class="row">
<div class="offset-sm-2">
<button type="submit" class="btn btn-primary">提交</button>

View File

@ -16,7 +16,7 @@ namespace WebApplication1.Pages
private readonly AbstaractShardingDbContext db;
[BindProperty]
public TestModelKey NewModel { get; set; }
public string Key { get; set; }
public CreateDbKeyModel(AbstaractShardingDbContext db)
{
@ -25,24 +25,22 @@ namespace WebApplication1.Pages
public void OnGet()
{
NewModel = new TestModelKey();
Key = "";
}
public IActionResult OnPost()
{
NewModel.Id = Guid.NewGuid();
NewModel.CreationDate = DateTime.Now;
db.TestModelKeys.Add(NewModel);
db.TestModelKeys.Add(new TestModelKey { Key = Key });
db.SaveChanges();
// 读取并写入到配置
var dblist = JsonFileHelper.Read<List<string>>(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName);
dblist.Add(NewModel.Key);
dblist.Add(Key);
dblist.Sort();
JsonFileHelper.Save(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName, dblist);
// 动态新增数据源
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", NewModel.Key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{NewModel.Key};");
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", Key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{Key};");
return RedirectToPage("DbKeyMan");
}

View File

@ -5,6 +5,7 @@
}
<a asp-page="CreateDbKey">新增分库key</a>
<a asp-page="BatchCreateDbKey">批量新增分库key</a>
<table class="table">
<thead>

View File

@ -13,7 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.5" />
<PackageReference Include="ShardingCore" Version="6.5.0.9" />
<PackageReference Include="ShardingCore" Version="6.5.0.11" />
</ItemGroup>
<ItemGroup>

View File

@ -33,6 +33,8 @@
如果本地未生成库时,请在 tool 项目目录下,使用 ef tool 命令来做数据库的初始迁移(包含分库分表),需要将 WebApplication1/bin/net 6.0 目录下的 muitDbConfig.json 拷贝到 WebApplication1.Migrations.Tool/bin/net 6.0 目录下,以供 tool 项目使用,当然也可以利用其他持久化方法。如果本地已经存在完整的库(包含分库),则可直接运行 webapp 项目
本示例采用的是 postgresql 引擎,默认的最大链接数是 100如果分库数量大于 100 时,运行时会报 pgsql 53300 异常超出最大链接数需要修改引擎配置。max_connection根据自己需要自行调整。
初始化后的库结构如下:
![02](images/02.png)