Skip to content

Commit

Permalink
Merge branch 'optimize-dbconfig'
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoy8 committed Oct 31, 2023
2 parents ac9fd34 + eed5d5c commit bea5332
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 234 deletions.
26 changes: 13 additions & 13 deletions Blog.Core.Api/Controllers/DbFirst/DbFirstController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public MessageModel<string> GetFrameFiles()
{
var data = new MessageModel<string>() { success = true, msg = "" };
data.response += @"file path is:C:\my-file\}";
var isMuti = AppSettings.app(new string[] { "MutiDBEnabled" }).ObjToBool();
var isMuti = BaseDBConfig.IsMulti;
if (Env.IsDevelopment())
{
data.response += $"Controller层生成:{FrameSeed.CreateControllers(_sqlSugarClient)} || ";

BaseDBConfig.MutiConnectionString.allDbs.ToList().ForEach(m =>
BaseDBConfig.ValidConfig.ForEach(m =>
{
_sqlSugarClient.ChangeDatabase(m.ConnId.ToLower());
data.response += $"库{m.ConnId}-Model层生成:{FrameSeed.CreateModels(_sqlSugarClient, m.ConnId, isMuti)} || ";
data.response += $"库{m.ConnId}-IRepositorys层生成:{FrameSeed.CreateIRepositorys(_sqlSugarClient, m.ConnId, isMuti)} || ";
data.response += $"库{m.ConnId}-IServices层生成:{FrameSeed.CreateIServices(_sqlSugarClient, m.ConnId, isMuti)} || ";
data.response += $"库{m.ConnId}-Repository层生成:{FrameSeed.CreateRepository(_sqlSugarClient, m.ConnId, isMuti)} || ";
data.response += $"库{m.ConnId}-Services层生成:{FrameSeed.CreateServices(_sqlSugarClient, m.ConnId, isMuti)} || ";
_sqlSugarClient.ChangeDatabase(m.ConfigId.ToLower());
data.response += $"库{m.ConfigId}-Model层生成:{FrameSeed.CreateModels(_sqlSugarClient, m.ConfigId, isMuti)} || ";
data.response += $"库{m.ConfigId}-IRepositorys层生成:{FrameSeed.CreateIRepositorys(_sqlSugarClient, m.ConfigId, isMuti)} || ";
data.response += $"库{m.ConfigId}-IServices层生成:{FrameSeed.CreateIServices(_sqlSugarClient, m.ConfigId, isMuti)} || ";
data.response += $"库{m.ConfigId}-Repository层生成:{FrameSeed.CreateRepository(_sqlSugarClient, m.ConfigId, isMuti)} || ";
data.response += $"库{m.ConfigId}-Services层生成:{FrameSeed.CreateServices(_sqlSugarClient, m.ConfigId, isMuti)} || ";
});

// 切回主库
Expand All @@ -74,7 +74,7 @@ public MessageModel<string> GetFrameFilesByTableNames([FromBody]string[] tableNa
{
ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;

var isMuti = AppSettings.app(new string[] { "MutiDBEnabled" }).ObjToBool();
var isMuti = BaseDBConfig.IsMulti;
var data = new MessageModel<string>() { success = true, msg = "" };
if (Env.IsDevelopment())
{
Expand Down Expand Up @@ -102,7 +102,7 @@ public MessageModel<string> GetFrameFilesByTableNamesForEntity([FromBody] string
{
ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;

var isMuti = AppSettings.app(new string[] { "MutiDBEnabled" }).ObjToBool();
var isMuti = BaseDBConfig.IsMulti;
var data = new MessageModel<string>() { success = true, msg = "" };
if (Env.IsDevelopment())
{
Expand All @@ -112,7 +112,7 @@ public MessageModel<string> GetFrameFilesByTableNamesForEntity([FromBody] string
{
data.success = false;
data.msg = "当前不处于开发模式,代码生成不可用!";
}
}
return data;
}
/// <summary>
Expand All @@ -126,7 +126,7 @@ public MessageModel<string> GetFrameFilesByTableNamesForController([FromBody] st
{
ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;

var isMuti = AppSettings.app(new string[] { "MutiDBEnabled" }).ObjToBool();
var isMuti = BaseDBConfig.IsMulti;
var data = new MessageModel<string>() { success = true, msg = "" };
if (Env.IsDevelopment())
{
Expand All @@ -151,7 +151,7 @@ public MessageModel<string> GetAllFrameFilesByTableNames([FromBody]string[] tabl
{
ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;

var isMuti = AppSettings.app(new string[] { "MutiDBEnabled" }).ObjToBool();
var isMuti = BaseDBConfig.IsMulti;
var data = new MessageModel<string>() { success = true, msg = "" };
if (Env.IsDevelopment())
{
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Api/Controllers/MonitorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public async Task<MessageModel<AccessApiDateView>> GetIds4Users()
{
List<ApiDate> apiDates = new List<ApiDate>();

if (AppSettings.app(new string[] { "MutiDBEnabled" }).ObjToBool())
if (_applicationUserServices.IsEnable())
{
var users = await _applicationUserServices.Query(d => d.tdIsDelete == false);

Expand Down
51 changes: 32 additions & 19 deletions Blog.Core.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@
"UseLoadTest": false
},

// 请配置MainDB为你想要的主库的ConnId值,并设置对应的Enabled为true;
// *** 单库操作,把 MutiDBEnabled 设为false ***;
// *** 多库操作,把 MutiDBEnabled 设为true,其他的从库Enabled也为true **;
// 具体配置看视频:https://www.bilibili.com/video/BV1BJ411B7mn?p=6
//Log:日志库;
"MainDB": "WMBLOG_SQLITE", //当前项目的主库,所对应的连接字符串的Enabled必须为true
"MutiDBEnabled": true, //是否开启多库模式
"CQRSEnabled": false, //是否开启读写分离模式,必须是单库模式,且数据库类型一致,比如都是SqlServer
//优化DB配置、不会再区分单库多库
//MainDb:标识当前项目的主库,所对应的连接字符串的Enabled必须为true
//Log:标识日志库,所对应的连接字符串的Enabled必须为true
//从库只需配置Slaves数组,要求数据库类型一致!,比如都是SqlServer
//
//新增,故障转移方案
//如果主库挂了,会自动切换到备用连接(比如说主库+备用库)
//备用连接的ConnId配置为主库的ConnId+数字即可,比如主库的ConnId为Main,那么备用连接的ConnId为Mian1
//主库、备用库无需数据库类型一致!
//备用库不会有程序维护,需要手动维护
"MainDB": "Main", //当前项目的主库,所对应的连接字符串的Enabled必须为true
"DBS": [
/*
对应下边的 DBType
Expand All @@ -100,68 +103,78 @@
Kdbndp = 6,//人大金仓
*/
{
"ConnId": "WMBLOG_SQLITE",
"ConnId": "Main",
"DBType": 2,
"Enabled": true,
"HitRate": 50, // 值越大,优先级越高
"Connection": "WMBlog.db" //sqlite只写数据库名就行
"Connection": "WMBlog.db", //sqlite只写数据库名就行
"Slaves": [
{
"HitRate": 0,// 值越大,优先级越高 0不使用
"Connection": "WMBlog2.db"
}
]
},
{
"ConnId": "Main2",
"DBType": 2,
"Enabled": true,
"Connection": "WMBlog3.db", //sqlite只写数据库名就行
"Slaves": [
{
"HitRate": 0,// 值越大,优先级越高 0不使用
"Connection": "WMBlog4.db"
}
]
},
{
"ConnId": "Log", //日志库连接固定名称,不要改,其他的可以改
"DBType": 2,
"Enabled": true,
"HitRate": 50, // 值越大,优先级越高
"HitRate": 50,
"Connection": "WMBlogLog.db" //sqlite只写数据库名就行
},
{
"ConnId": "WMBLOG_MSSQL_1",
"DBType": 1,
"Enabled": false,
"HitRate": 40,
"Connection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=WMBLOG_MSSQL_1;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
"ProviderName": "System.Data.SqlClient"
},
{
"ConnId": "WMBLOG_MSSQL_2",
"DBType": 1,
"Enabled": false,
"HitRate": 30,
"Connection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=WMBLOG_MSSQL_2;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
"ProviderName": "System.Data.SqlClient"
},
{
"ConnId": "WMBLOG_MYSQL",
"DBType": 0,
"Enabled": false,
"HitRate": 20,
"Connection": "server=localhost;Database=blog;Uid=root;Pwd=root;Port=3306;Allow User Variables=True;"
},
{
"ConnId": "WMBLOG_MYSQL_2",
"DBType": 0,
"Enabled": false,
"HitRate": 20,
"Connection": "server=localhost;Database=blogcore001;Uid=root;Pwd=root;Port=3306;Allow User Variables=True;"
},
{
"ConnId": "WMBLOG_ORACLE",
"DBType": 3,
"Enabled": false,
"HitRate": 10,
"Connection": "Data Source=127.0.0.1/ops;User ID=OPS;Password=123456;Persist Security Info=True;Connection Timeout=60;"
},
{
"ConnId": "WMBLOG_DM",
"DBType": 5,
"Enabled": false,
"HitRate": 10,
"Connection": "Server=xxxxx:5236;User Id=xxxxx;PWD=xxxxx;SCHEMA=TESTDBA;"
},
{
"ConnId": "WMBLOG_KDBNDP",
"DBType": 6,
"Enabled": false,
"HitRate": 10,
"Connection": "Server=127.0.0.1;Port=54321;UID=SYSTEM;PWD=system;database=SQLSUGAR4XTEST1;"
}
],
Expand Down
23 changes: 23 additions & 0 deletions Blog.Core.Common/DB/Aop/SqlSugarReuse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Linq;
using SqlSugar;

namespace Blog.Core.Common.DB.Aop;

public class SqlSugarReuse
{
public static void AutoChangeAvailableConnect(SqlSugarClient db)
{
if (db == null) return;
if (db.Ado.IsValidConnection()) return;
if (!BaseDBConfig.ReuseConfigs.Any()) return;

foreach (var connectionConfig in BaseDBConfig.ReuseConfigs)
{
var config = db.CurrentConnectionConfig.ConfigId;
db.ChangeDatabase(connectionConfig.ConfigId);
//移除旧的连接,只会在本次上下文移除,因为主库已经故障会导致多库事务无法使用
db.RemoveConnection(config);
if (db.Ado.IsValidConnection()) return;
}
}
}
102 changes: 47 additions & 55 deletions Blog.Core.Common/DB/BaseDBConfig.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
using SqlSugar;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SqlSugar;

namespace Blog.Core.Common.DB
{
public class BaseDBConfig
{
public static readonly List<ConnectionConfig> AllConfigs = new(); //所有库配置
public static readonly List<SlaveConnectionConfig> AllSlaveConfigs = new(); //从库配置
public static List<ConnectionConfig> ValidConfig = new(); //有效的库连接(除去Log库)
public static ConnectionConfig LogConfig; //日志库

/// <summary>
/// 所有库配置
/// </summary>
public static readonly List<ConnectionConfig> AllConfigs = new();

/// <summary>
/// 主库的备用连接配置
/// </summary>
public static readonly List<ConnectionConfig> ReuseConfigs = new();

/// <summary>
/// 有效的库连接(除去Log库)
/// </summary>
public static List<ConnectionConfig> ValidConfig = new();

public static ConnectionConfig MainConfig;
public static ConnectionConfig LogConfig; //日志库

public static bool IsMulti => ValidConfig.Count > 1;

/* 之前的单库操作已经删除,如果想要之前的代码,可以查看我的GitHub的历史记录
* 目前是多库操作,默认加载的是appsettings.json设置为true的第一个db连接。
*
* 优化配置连接
* 老的配置方式,再多库和从库中有些冲突
* 直接在单个配置中可以配置从库
*
* 新增故障转移方案
* 增加主库备用连接,配置方式为ConfigId为主库的ConfigId+随便数字 只要不重复就好
*
* 主库在无法连接后会自动切换到备用链接
*/
public static (List<MutiDBOperate> allDbs, List<MutiDBOperate> slaveDbs) MutiConnectionString => MutiInitConn();



private static string DifDBConnOfSecurity(params string[] conn)
{
Expand All @@ -44,52 +66,13 @@ public static (List<MutiDBOperate>, List<MutiDBOperate>) MutiInitConn()
{
List<MutiDBOperate> listdatabase = AppSettings.app<MutiDBOperate>("DBS")
.Where(i => i.Enabled).ToList();
var mainDbId = AppSettings.app(new string[] { "MainDB" }).ObjToString();
var mainDbId = AppSettings.app(new string[] {"MainDB"}).ObjToString();
var mainDbModel = listdatabase.Single(d => d.ConnId == mainDbId);
listdatabase.Remove(mainDbModel);
listdatabase.Insert(0, mainDbModel);

foreach (var i in listdatabase)
{
SpecialDbString(i);
}

List<MutiDBOperate> listdatabaseSimpleDB = new List<MutiDBOperate>(); //单库
List<MutiDBOperate> listdatabaseSlaveDB = new List<MutiDBOperate>(); //从库

// 单库,且不开启读写分离,只保留一个
if (!AppSettings.app(new string[] {"CQRSEnabled"}).ObjToBool() && !AppSettings.app(new string[] {"MutiDBEnabled"}).ObjToBool())
{
if (listdatabase.Count == 1)
{
return (listdatabase, listdatabaseSlaveDB);
}
else
{
var dbFirst = listdatabase.FirstOrDefault(d => d.ConnId == AppSettings.app(new string[] {"MainDB"}).ObjToString());
if (dbFirst == null)
{
dbFirst = listdatabase.FirstOrDefault();
}

listdatabaseSimpleDB.Add(dbFirst);
return (listdatabaseSimpleDB, listdatabaseSlaveDB);
}
}


// 读写分离,且必须是单库模式,获取从库
if (AppSettings.app(new string[] {"CQRSEnabled"}).ObjToBool() && !AppSettings.app(new string[] {"MutiDBEnabled"}).ObjToBool())
{
if (listdatabase.Count > 1)
{
listdatabaseSlaveDB = listdatabase.Where(d => d.ConnId != AppSettings.app(new string[] {"MainDB"}).ObjToString()).ToList();
}
}


return (listdatabase, listdatabaseSlaveDB);
//}
foreach (var i in listdatabase) SpecialDbString(i);
return (listdatabase, mainDbModel.Slaves);
}

/// <summary>
Expand All @@ -102,19 +85,23 @@ private static MutiDBOperate SpecialDbString(MutiDBOperate mutiDBOperate)
{
if (mutiDBOperate.DbType == DataBaseType.Sqlite)
{
mutiDBOperate.Connection = $"DataSource=" + Path.Combine(Environment.CurrentDirectory, mutiDBOperate.Connection);
mutiDBOperate.Connection =
$"DataSource=" + Path.Combine(Environment.CurrentDirectory, mutiDBOperate.Connection);
}
else if (mutiDBOperate.DbType == DataBaseType.SqlServer)
{
mutiDBOperate.Connection = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_SqlserverConn.txt", mutiDBOperate.Connection);
mutiDBOperate.Connection = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_SqlserverConn.txt",
mutiDBOperate.Connection);
}
else if (mutiDBOperate.DbType == DataBaseType.MySql)
{
mutiDBOperate.Connection = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_MySqlConn.txt", mutiDBOperate.Connection);
mutiDBOperate.Connection =
DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_MySqlConn.txt", mutiDBOperate.Connection);
}
else if (mutiDBOperate.DbType == DataBaseType.Oracle)
{
mutiDBOperate.Connection = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_OracleConn.txt", mutiDBOperate.Connection);
mutiDBOperate.Connection =
DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_OracleConn.txt", mutiDBOperate.Connection);
}

return mutiDBOperate;
Expand Down Expand Up @@ -159,5 +146,10 @@ public class MutiDBOperate
/// 数据库类型
/// </summary>
public DataBaseType DbType { get; set; }

/// <summary>
/// 从库
/// </summary>
public List<MutiDBOperate> Slaves { get; set; }
}
}
14 changes: 14 additions & 0 deletions Blog.Core.Common/DB/Extension/DbEntityException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Reflection;
using SqlSugar;

namespace Blog.Core.Common.DB.Extension;

public static class DbEntityException
{
public static object GetEntityTenant(this Type type)
{
var tenant = type.GetCustomAttribute<TenantAttribute>();
return tenant?.configId;
}
}
2 changes: 1 addition & 1 deletion Blog.Core.Common/DB/MainDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public static class MainDb
{
public static string CurrentDbConnId = "1";
public static string CurrentDbConnId = "Main";
}
}
Loading

0 comments on commit bea5332

Please sign in to comment.