sharding/samples/Sample.SqlServer/Startup.cs

99 lines
5.2 KiB
C#
Raw Normal View History

2021-01-26 14:39:56 +08:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
2021-08-15 07:32:50 +08:00
using Microsoft.EntityFrameworkCore;
2021-01-26 14:39:56 +08:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2021-08-17 22:17:18 +08:00
using Microsoft.Extensions.Logging;
using Sample.SqlServer.DbContexts;
2021-01-26 14:39:56 +08:00
using Sample.SqlServer.Shardings;
2021-08-17 22:17:18 +08:00
using ShardingCore;
2021-11-30 09:53:07 +08:00
using ShardingCore.Sharding.ReadWriteConfigurations;
2021-11-03 08:48:24 +08:00
using System;
2021-11-13 11:13:47 +08:00
using System.Collections.Generic;
2021-01-26 14:39:56 +08:00
namespace Sample.SqlServer
{
public class Startup
{
2021-08-17 22:17:18 +08:00
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
2021-01-26 14:39:56 +08:00
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
2021-08-20 17:36:15 +08:00
//services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx3;Integrated Security=True"));
2021-08-16 04:21:46 +08:00
2021-10-11 20:58:55 +08:00
services.AddShardingDbContext<DefaultShardingDbContext>(
2021-10-15 17:18:23 +08:00
(conn, o) =>
o.UseSqlServer(conn).UseLoggerFactory(efLogger)
).Begin(o =>
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.AutoTrackEntity = true;
o.ParallelQueryMaxThreadCount = 100;
2021-11-13 11:13:47 +08:00
o.ParallelQueryTimeOut = TimeSpan.FromSeconds(10);
//if SysTest entity not exists in db and db is exists
//o.AddEntityTryCreateTable<SysTest>(); // or `o.AddEntitiesTryCreateTable(typeof(SysTest));`
})
2021-10-15 17:18:23 +08:00
//.AddShardingQuery((conStr, builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger))//无需添加.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) 并发查询系统会自动添加NoTracking
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
.AddDefaultDataSource("ds0",
2021-09-22 20:35:19 +08:00
"Data Source=localhost;Initial Catalog=ShardingCoreDB1;Integrated Security=True;")
.AddShardingTableRoute(o =>
{
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
2021-11-04 17:42:26 +08:00
o.AddShardingTableRoute<TestYearShardingVirtualTableRoute>();
2021-11-13 11:13:47 +08:00
}).AddReadWriteSeparation(sp =>
{
return new Dictionary<string, IEnumerable<string>>()
2021-11-13 11:13:47 +08:00
{
{"ds0",new HashSet<string>(){"Data Source=localhost;Initial Catalog=ShardingCoreDB1;Integrated Security=True;"}}
};
},ReadStrategyEnum.Loop,true).End();
2021-11-25 14:00:01 +08:00
services.AddHealthChecks().AddDbContextCheck<DefaultShardingDbContext>();
//services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
// o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
// , op =>
// {
// op.EnsureCreatedWithOutShardingTable = true;
// op.CreateShardingTableOnStart = true;
// op.UseShardingOptionsBuilder(
// (connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),//使用dbconnection创建dbcontext支持事务
// (conStr,builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
// //.ReplaceService<IQueryTranslationPostprocessorFactory,SqlServer2008QueryTranslationPostprocessorFactory>()//支持sqlserver2008r2
// );//使用链接字符串创建dbcontext
// //op.UseReadWriteConfiguration(sp => new List<string>()
// //{
// // "Data Source=localhost;Initial Catalog=ShardingCoreDB1;Integrated Security=True;",
// // "Data Source=localhost;Initial Catalog=ShardingCoreDB2;Integrated Security=True;"
// //}, ReadStrategyEnum.Random);
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// });
2021-01-26 14:39:56 +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();
}
app.UseShardingCore();
app.UseRouting();
2021-03-08 14:59:32 +08:00
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
2021-11-09 14:13:00 +08:00
app.DbSeed();
2021-01-26 14:39:56 +08:00
}
}
}