2022-07-02 14:08:08 +08:00
|
|
|
using System.Diagnostics;
|
2022-01-18 11:22:12 +08:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-11-28 13:04:07 +08:00
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
2022-07-04 22:47:03 +08:00
|
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
2022-11-28 13:04:07 +08:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2022-01-18 11:22:12 +08:00
|
|
|
using Sample.MySql.DbContexts;
|
2022-07-19 20:53:33 +08:00
|
|
|
using Sample.MySql.Domain.Entities;
|
2022-07-20 13:44:52 +08:00
|
|
|
using Sample.MySql.multi;
|
2022-01-18 11:22:12 +08:00
|
|
|
using Sample.MySql.Shardings;
|
|
|
|
using ShardingCore;
|
2022-07-02 14:08:08 +08:00
|
|
|
using ShardingCore.Bootstrappers;
|
2022-07-01 20:58:20 +08:00
|
|
|
using ShardingCore.Core;
|
2022-11-28 13:04:07 +08:00
|
|
|
using ShardingCore.Core.ModelCacheLockerProviders;
|
2022-07-02 00:05:35 +08:00
|
|
|
using ShardingCore.Core.RuntimeContexts;
|
2022-07-04 22:47:03 +08:00
|
|
|
using ShardingCore.EFCores;
|
2022-07-01 20:58:20 +08:00
|
|
|
using ShardingCore.Extensions;
|
2022-07-12 10:29:31 +08:00
|
|
|
using ShardingCore.Helpers;
|
2022-12-13 17:36:51 +08:00
|
|
|
using ShardingCore.Sharding.ParallelTables;
|
2022-07-20 13:44:52 +08:00
|
|
|
using ShardingCore.Sharding.ReadWriteConfigurations;
|
2022-07-03 22:26:31 +08:00
|
|
|
using ShardingCore.TableExists;
|
|
|
|
using ShardingCore.TableExists.Abstractions;
|
2021-03-22 11:12:00 +08:00
|
|
|
|
|
|
|
namespace Sample.MySql
|
|
|
|
{
|
2022-07-04 22:47:03 +08:00
|
|
|
// public class AutoStart : IHostedService
|
|
|
|
// {
|
|
|
|
//
|
|
|
|
// public AutoStart(IShardingBootstrapper shardingBootstrapper)
|
|
|
|
// {
|
|
|
|
// shardingBootstrapper.Start();
|
|
|
|
// }
|
|
|
|
// public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
// {
|
|
|
|
// return Task.CompletedTask;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
// {
|
|
|
|
// return Task.CompletedTask;
|
|
|
|
// }
|
|
|
|
// }
|
2021-03-22 11:12:00 +08:00
|
|
|
public class Startup
|
|
|
|
{
|
2022-01-18 11:22:12 +08:00
|
|
|
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
|
|
|
{
|
2022-07-20 13:44:52 +08:00
|
|
|
builder.AddFilter((category, level) =>
|
|
|
|
category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
2022-01-18 11:22:12 +08:00
|
|
|
});
|
2022-07-20 13:44:52 +08:00
|
|
|
|
2021-03-22 11:12:00 +08:00
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
Configuration = configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
2022-07-04 22:47:03 +08:00
|
|
|
// services.AddHostedService<AutoStart>();
|
2022-11-28 13:04:07 +08:00
|
|
|
services.AddControllers();
|
|
|
|
services.AddSingleton<IMemoryCache>(sp => new MemoryCache(new MemoryCacheOptions { SizeLimit = 102400 }));
|
2022-12-09 21:02:52 +08:00
|
|
|
//
|
|
|
|
// Action<IServiceProvider, DbContextOptionsBuilder> optionsBuilder = null;
|
|
|
|
// services.AddDbContext<DefaultShardingDbContext>(optionsBuilder);
|
|
|
|
// services.AddDbContext<DefaultShardingDbContext>((sp,builder) =>
|
|
|
|
// {
|
|
|
|
// optionsBuilder(sp, builder);
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2022-09-19 10:42:10 +08:00
|
|
|
services.AddShardingDbContext<DefaultShardingDbContext>()
|
|
|
|
.UseRouteConfig(o =>
|
2022-08-30 10:25:01 +08:00
|
|
|
{
|
2022-11-16 19:57:29 +08:00
|
|
|
o.AddShardingTableRoute<DynamicTableRoute>();
|
2022-12-22 22:47:25 +08:00
|
|
|
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
|
|
|
|
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
2022-09-19 10:42:10 +08:00
|
|
|
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
|
2022-12-13 17:36:51 +08:00
|
|
|
o.AddShardingTableRoute<TestModRoute>();
|
|
|
|
o.AddShardingTableRoute<TestModItemRoute>();
|
|
|
|
o.AddParallelTableGroupNode(new ParallelTableGroupNode(new List<ParallelTableComparerType>()
|
|
|
|
{
|
|
|
|
new ParallelTableComparerType(typeof(TestMod)),
|
|
|
|
new ParallelTableComparerType(typeof(TestModItem)),
|
|
|
|
}));
|
2022-11-16 19:57:29 +08:00
|
|
|
}).UseConfig((sp,o) =>
|
2022-07-20 13:44:52 +08:00
|
|
|
{
|
2022-11-28 13:04:07 +08:00
|
|
|
var memoryCache = sp.ApplicationServiceProvider.GetRequiredService<IMemoryCache>();
|
|
|
|
o.UseExecutorDbContextConfigure(b =>
|
|
|
|
{
|
|
|
|
b.UseMemoryCache(memoryCache);
|
|
|
|
});
|
2022-12-09 21:02:52 +08:00
|
|
|
o.UseEntityFrameworkCoreProxies = true;
|
2022-11-27 00:08:36 +08:00
|
|
|
o.CacheModelLockConcurrencyLevel = 1024;
|
|
|
|
o.CacheEntrySize = 1;
|
2022-11-27 10:12:51 +08:00
|
|
|
o.CacheModelLockObjectSeconds = 10;
|
2022-11-26 10:06:46 +08:00
|
|
|
o.CheckShardingKeyValueGenerated = false;
|
2022-11-16 19:57:29 +08:00
|
|
|
var loggerFactory1= sp.GetService<ILoggerFactory>();
|
|
|
|
var loggerFactory2 = sp.ApplicationServiceProvider.GetService<ILoggerFactory>();
|
2022-11-01 13:46:52 +08:00
|
|
|
// o.UseEntityFrameworkCoreProxies = true;
|
|
|
|
o.ThrowIfQueryRouteNotMatch = false;
|
2022-09-19 10:42:10 +08:00
|
|
|
o.AutoUseWriteConnectionStringAfterWriteDb = true;
|
2022-12-09 21:02:52 +08:00
|
|
|
|
2022-07-20 13:44:52 +08:00
|
|
|
o.UseShardingQuery((conStr, builder) =>
|
|
|
|
{
|
2022-12-23 16:32:54 +08:00
|
|
|
builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
|
2023-01-03 17:16:45 +08:00
|
|
|
.UseLoggerFactory(efLogger)
|
2022-12-23 16:32:54 +08:00
|
|
|
.EnableSensitiveDataLogging();
|
2022-12-09 21:02:52 +08:00
|
|
|
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
2022-07-20 13:44:52 +08:00
|
|
|
});
|
|
|
|
o.UseShardingTransaction((connection, builder) =>
|
|
|
|
{
|
|
|
|
builder
|
2022-12-23 16:32:54 +08:00
|
|
|
.UseMySql(connection, new MySqlServerVersion(new Version()))
|
|
|
|
.UseLoggerFactory(efLogger)
|
|
|
|
.EnableSensitiveDataLogging();
|
2022-11-26 10:06:46 +08:00
|
|
|
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
2022-07-20 13:44:52 +08:00
|
|
|
});
|
2022-09-19 10:42:10 +08:00
|
|
|
o.AddDefaultDataSource("ds0",
|
2022-12-22 22:47:25 +08:00
|
|
|
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
|
2022-09-19 10:42:10 +08:00
|
|
|
o.AddExtraDataSource(sp => new Dictionary<string, string>()
|
|
|
|
{
|
2022-12-22 22:47:25 +08:00
|
|
|
{ "ds1", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;" },
|
|
|
|
{ "ds2", "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;" }
|
2022-09-19 10:42:10 +08:00
|
|
|
});
|
2022-07-20 13:44:52 +08:00
|
|
|
o.UseShardingMigrationConfigure(b =>
|
|
|
|
{
|
|
|
|
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
|
|
|
|
});
|
2022-11-28 13:04:07 +08:00
|
|
|
}).ReplaceService<IModelCacheLockerProvider,DicModelCacheLockerProvider>()
|
2022-09-19 10:42:10 +08:00
|
|
|
.AddShardingCore();
|
|
|
|
// services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
|
|
|
|
// .UseMutliDefaultSharding<DefaultShardingDbContext>);
|
2022-07-01 20:58:20 +08:00
|
|
|
// services.AddShardingDbContext<DefaultShardingDbContext>()
|
|
|
|
// .AddEntityConfig(o =>
|
|
|
|
// {
|
|
|
|
// o.CreateDataBaseOnlyOnStart = true;
|
|
|
|
// o.CreateShardingTableOnStart = true;
|
|
|
|
// o.EnsureCreatedWithOutShardingTable = true;
|
|
|
|
// o.IgnoreCreateTableError = true;
|
|
|
|
// o.AddShardingTableRoute<SysUserLogByMonthRoute>();
|
|
|
|
// o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
|
|
|
// o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
|
|
|
|
// o.UseShardingQuery((conStr, builder) =>
|
|
|
|
// {
|
|
|
|
// builder.UseMySql(conStr, new MySqlServerVersion(new Version())
|
|
|
|
// ,b=>b.EnableRetryOnFailure()
|
|
|
|
// )
|
|
|
|
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseLoggerFactory(efLogger);
|
|
|
|
// //builder.UseMySql(conStr, new MySqlServerVersion(new Version()));
|
|
|
|
// });
|
|
|
|
// o.UseShardingTransaction((connection, builder) =>
|
|
|
|
// {
|
|
|
|
// builder.UseMySql(connection, new MySqlServerVersion(new Version())
|
|
|
|
// ,b=>b.EnableRetryOnFailure()
|
|
|
|
// )
|
|
|
|
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseLoggerFactory(efLogger);
|
|
|
|
// });
|
|
|
|
// })
|
|
|
|
// .AddConfig(op =>
|
|
|
|
// {
|
|
|
|
// op.ConfigId = "c0";
|
|
|
|
// op.AddDefaultDataSource("ds0",
|
|
|
|
// "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
|
|
|
|
//
|
|
|
|
// //op.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=db2;userid=root;password=L6yBtV6qNENrwBy7;")
|
|
|
|
// op.ReplaceTableEnsureManager(sp=>new MySqlTableEnsureManager<DefaultShardingDbContext>());
|
|
|
|
// }).EnsureConfig();
|
2021-03-22 11:12:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
{
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
2022-09-19 10:42:10 +08:00
|
|
|
// app.ApplicationServices.UseAutoTryCompensateTable();
|
|
|
|
|
2022-07-20 13:44:52 +08:00
|
|
|
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
|
|
|
|
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
|
|
|
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
|
2022-11-01 13:46:52 +08:00
|
|
|
// using (var scope = app.ApplicationServices.CreateScope())
|
|
|
|
// {
|
|
|
|
// var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
|
|
|
// // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
|
|
|
|
// {
|
|
|
|
// defaultShardingDbContext.Database.Migrate();
|
|
|
|
// }
|
|
|
|
// }
|
2022-10-09 14:51:22 +08:00
|
|
|
app.ApplicationServices.UseAutoTryCompensateTable();
|
2022-07-20 13:44:52 +08:00
|
|
|
// using (var scope = app.ApplicationServices.CreateScope())
|
|
|
|
// {
|
|
|
|
// var defaultShardingDbContext = scope.ServiceProvider.GetService<OtherDbContext>();
|
|
|
|
// // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
|
|
|
|
// {
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
//
|
|
|
|
// defaultShardingDbContext.Database.Migrate();
|
|
|
|
// }
|
|
|
|
// catch (Exception e)
|
|
|
|
// {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// app.ApplicationServices.UseAutoTryCompensateTable(12);
|
2022-07-05 11:09:26 +08:00
|
|
|
|
2021-03-22 11:12:00 +08:00
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
2022-07-20 13:44:52 +08:00
|
|
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
2022-06-23 07:44:26 +08:00
|
|
|
// for (int i = 1; i < 500; i++)
|
|
|
|
// {
|
|
|
|
// using (var conn = new MySqlConnection(
|
|
|
|
// $"server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;"))
|
|
|
|
// {
|
|
|
|
// conn.Open();
|
|
|
|
// }
|
2022-06-24 14:48:14 +08:00
|
|
|
// DynamicShardingHelper.DynamicAppendDataSource<DefaultShardingDbContext>($"c0",$"ds{i}",$"server=127.0.0.1;port=3306;database=dbdbd{i};userid=root;password=root;");
|
2022-06-23 07:44:26 +08:00
|
|
|
//
|
|
|
|
// }
|
2021-03-22 11:12:00 +08:00
|
|
|
app.DbSeed();
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 13:44:52 +08:00
|
|
|
}
|