2021-03-22 11:12:00 +08:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-01-18 11:22:12 +08:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-03-22 11:12:00 +08:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2022-06-23 07:44:26 +08:00
|
|
|
using MySqlConnector;
|
2022-01-18 11:22:12 +08:00
|
|
|
using Sample.MySql.DbContexts;
|
|
|
|
using Sample.MySql.Shardings;
|
|
|
|
using ShardingCore;
|
2022-06-23 07:44:26 +08:00
|
|
|
using ShardingCore.Helpers;
|
2022-01-18 11:22:12 +08:00
|
|
|
using ShardingCore.TableExists;
|
2021-03-22 11:12:00 +08:00
|
|
|
|
|
|
|
namespace Sample.MySql
|
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
2022-01-18 11:22:12 +08:00
|
|
|
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
|
|
|
{
|
|
|
|
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
|
|
|
});
|
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)
|
|
|
|
{
|
|
|
|
services.AddControllers();
|
2021-08-23 19:30:52 +08:00
|
|
|
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
|
|
|
|
// ,op =>
|
2021-08-20 23:10:34 +08:00
|
|
|
// {
|
2021-08-23 19:30:52 +08:00
|
|
|
// op.EnsureCreatedWithOutShardingTable = true;
|
|
|
|
// op.CreateShardingTableOnStart = true;
|
|
|
|
// op.UseShardingOptionsBuilder((connection, builder) => builder.UseMySql(connection,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger),
|
|
|
|
// (conStr,builder)=> builder.UseMySql(conStr,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger));
|
|
|
|
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
|
|
|
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
|
2021-08-20 23:10:34 +08:00
|
|
|
// });
|
2022-01-18 11:22:12 +08:00
|
|
|
|
2022-02-27 23:10:28 +08:00
|
|
|
services.AddShardingDbContext<DefaultShardingDbContext>()
|
2022-01-18 11:22:12 +08:00
|
|
|
.AddEntityConfig(o =>
|
|
|
|
{
|
2022-06-12 14:07:37 +08:00
|
|
|
o.CreateDataBaseOnlyOnStart = true;
|
2022-01-18 11:22:12 +08:00
|
|
|
o.CreateShardingTableOnStart = true;
|
|
|
|
o.EnsureCreatedWithOutShardingTable = true;
|
2022-06-15 22:21:19 +08:00
|
|
|
o.IgnoreCreateTableError = true;
|
2022-01-18 11:22:12 +08:00
|
|
|
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
|
|
|
|
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
2022-06-23 07:44:26 +08:00
|
|
|
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
|
2022-01-18 11:22:12 +08:00
|
|
|
o.UseShardingQuery((conStr, builder) =>
|
|
|
|
{
|
2022-06-15 17:53:43 +08:00
|
|
|
builder.UseMySql(conStr, new MySqlServerVersion(new Version())
|
|
|
|
,b=>b.EnableRetryOnFailure()
|
|
|
|
)
|
|
|
|
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseLoggerFactory(efLogger);
|
2022-01-18 11:22:12 +08:00
|
|
|
//builder.UseMySql(conStr, new MySqlServerVersion(new Version()));
|
|
|
|
});
|
|
|
|
o.UseShardingTransaction((connection, builder) =>
|
|
|
|
{
|
2022-06-15 17:53:43 +08:00
|
|
|
builder.UseMySql(connection, new MySqlServerVersion(new Version())
|
|
|
|
,b=>b.EnableRetryOnFailure()
|
|
|
|
)
|
|
|
|
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseLoggerFactory(efLogger);
|
2022-01-18 11:22:12 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.AddConfig(op =>
|
|
|
|
{
|
2022-06-23 07:44:26 +08:00
|
|
|
op.ConfigId = "c0";
|
2022-01-18 11:22:12 +08:00
|
|
|
op.AddDefaultDataSource("ds0",
|
2022-06-23 07:44:26 +08:00
|
|
|
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
|
2022-01-18 11:22:12 +08:00
|
|
|
|
|
|
|
//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)
|
|
|
|
{
|
2022-06-15 22:21:19 +08:00
|
|
|
Console.WriteLine("11111");
|
2021-03-22 11:12:00 +08:00
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseShardingCore();
|
2022-06-26 22:21:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
using (var serviceScope = app.ApplicationServices.CreateScope())
|
|
|
|
{
|
|
|
|
var defaultShardingDbContext = serviceScope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.WriteLine("------------------");
|
|
|
|
using (var serviceScope = app.ApplicationServices.CreateScope())
|
|
|
|
{
|
|
|
|
var defaultShardingDbContext = serviceScope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
|
|
|
}
|
|
|
|
Console.WriteLine("------------------");
|
2021-03-22 11:12:00 +08:00
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|