-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
342 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
samples/Sample.MySQLDataSourceOnly/Controllers/WeatherForecastController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Sample.MySQLDataSourceOnly.Domain; | ||
|
||
namespace Sample.MySQLDataSourceOnly.Controllers; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
private readonly MyDbContext _myDbContext; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger,MyDbContext myDbContext) | ||
{ | ||
_logger = logger; | ||
_myDbContext = myDbContext; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
var user = _myDbContext.Set<SysUser>().FirstOrDefault(o=>o.Id=="1"); | ||
user.Name = "456"+DateTime.Now.ToString(); | ||
_myDbContext.SaveChanges(); | ||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using ShardingCore.Sharding; | ||
|
||
namespace Sample.MySQLDataSourceOnly.Domain; | ||
|
||
|
||
public class MyDbContext : AbstractShardingDbContext | ||
{ | ||
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) | ||
{ | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
base.OnModelCreating(modelBuilder); | ||
modelBuilder.Entity<SysUser>(entity => | ||
{ | ||
entity.HasKey(o => o.Id); | ||
entity.Property(o => o.Id).IsRequired().IsUnicode(false).HasMaxLength(50); | ||
entity.Property(o => o.Name).IsRequired().IsUnicode(false).HasMaxLength(50); | ||
entity.Property(o => o.Area).IsRequired().IsUnicode(false).HasMaxLength(50); | ||
entity.ToTable(nameof(SysUser)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Sample.MySQLDataSourceOnly.Domain; | ||
|
||
public class SysUser | ||
{ | ||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
public string Area { get; set; } | ||
} |
55 changes: 55 additions & 0 deletions
55
samples/Sample.MySQLDataSourceOnly/Domain/SysUserVirtualDataSourceRoute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using ShardingCore.Core.EntityMetadatas; | ||
using ShardingCore.Core.VirtualRoutes; | ||
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions; | ||
|
||
namespace Sample.MySQLDataSourceOnly.Domain; | ||
|
||
public class SysUserVirtualDataSourceRoute : AbstractShardingOperatorVirtualDataSourceRoute<SysUser, string> | ||
{ | ||
private readonly List<string> _dataSources = new List<string>() | ||
{ | ||
"A", "B", "C" | ||
}; | ||
protected string ConvertToShardingKey(object shardingKey) | ||
{ | ||
return shardingKey?.ToString() ?? string.Empty; | ||
} | ||
|
||
//我们设置区域就是数据库 | ||
public override string ShardingKeyToDataSourceName(object shardingKey) | ||
{ | ||
return ConvertToShardingKey(shardingKey); | ||
} | ||
|
||
public override List<string> GetAllDataSourceNames() | ||
{ | ||
return _dataSources; | ||
} | ||
|
||
public override bool AddDataSourceName(string dataSourceName) | ||
{ | ||
if (_dataSources.Any(o => o == dataSourceName)) | ||
return false; | ||
_dataSources.Add(dataSourceName); | ||
return true; | ||
} | ||
|
||
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) | ||
{ | ||
|
||
var t = ShardingKeyToDataSourceName(shardingKey); | ||
switch (shardingOperator) | ||
{ | ||
case ShardingOperatorEnum.Equal: return tail => tail == t; | ||
default: | ||
{ | ||
return tail => true; | ||
} | ||
} | ||
} | ||
|
||
public override void Configure(EntityMetadataDataSourceBuilder<SysUser> builder) | ||
{ | ||
builder.ShardingProperty(o => o.Area); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Sample.MySQLDataSourceOnly.Domain; | ||
using ShardingCore; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
|
||
|
||
builder.Services.AddShardingDbContext<MyDbContext>() | ||
.UseRouteConfig(o => | ||
{ | ||
//o.CreateShardingTableOnStart = true; | ||
//o.EnsureCreatedWithOutShardingTable = true; | ||
o.AddShardingDataSourceRoute<SysUserVirtualDataSourceRoute>(); | ||
}) | ||
.UseConfig((sp,op) => | ||
{ | ||
var loggerFactory = sp.ApplicationServiceProvider.GetService<ILoggerFactory>(); | ||
//op.ConfigId = "c1"; | ||
op.UseShardingQuery((conStr, builder) => | ||
{ | ||
builder.UseMySql(conStr,new MySqlServerVersion(new Version())) | ||
.UseLoggerFactory(loggerFactory).EnableSensitiveDataLogging(); | ||
}); | ||
op.UseShardingTransaction((connection, builder) => | ||
{ | ||
builder.UseMySql(connection,new MySqlServerVersion(new Version())) | ||
.UseLoggerFactory(loggerFactory).EnableSensitiveDataLogging();; | ||
}); | ||
//op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<MyDbContext>()); | ||
op.AddDefaultDataSource("A", @"server=127.0.0.1;port=3306;database=onlyds1;userid=root;password=root;"); | ||
op.AddExtraDataSource(sp => | ||
{ | ||
return new Dictionary<string, string>() | ||
{ | ||
{ | ||
"B", | ||
@"server=127.0.0.1;port=3306;database=onlyds2;userid=root;password=root;" | ||
}, | ||
{ | ||
"C", | ||
@"server=127.0.0.1;port=3306;database=onlyds3;userid=root;password=root;" | ||
}, | ||
}; | ||
}); | ||
}).AddShardingCore(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
app.Services.UseAutoTryCompensateTable(); | ||
// using (var serviceScope = app.Services.CreateScope()) | ||
// { | ||
// var myDbContext = serviceScope.ServiceProvider.GetService<MyDbContext>(); | ||
// myDbContext.Database.EnsureCreated(); | ||
// myDbContext.Add(new SysUser() { Id = "1", Area = "A", Name = "name" }); | ||
// myDbContext.SaveChanges(); | ||
// } | ||
app.Run(); |
41 changes: 41 additions & 0 deletions
41
samples/Sample.MySQLDataSourceOnly/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:35669", | ||
"sslPort": 44323 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5029", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7187;http://localhost:5029", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
samples/Sample.MySQLDataSourceOnly/Sample.MySQLDataSourceOnly.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0" /> | ||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ShardingCore\ShardingCore.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Sample.MySQLDataSourceOnly; | ||
|
||
public class WeatherForecast | ||
{ | ||
public DateOnly Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
samples/Sample.MySQLDataSourceOnly/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.