.netcore實現一個讀寫分離的數據庫訪問中間件

在實際業務系統中,當單個數據庫不能承載負載壓力的時候,通常咱們採用數據庫讀寫分離的方式來分擔數據庫負載。主庫承擔寫以及事務操做,從庫承擔讀操做。mysql

爲了支持多種數據庫咱們先定義一個數據類型字典。key爲鏈接字符串,value爲數據庫類型:算法

        /// <summary>
        /// 數據庫方言集合
        /// </summary>
        private readonly Dictionary<string, DatabaseDialectEnum> DialectDictionary
          = new Dictionary<string, DatabaseDialectEnum>
          {
              ["sqlconnection"] = DatabaseDialectEnum.MSSQL,
              ["sqlceconnection"] = DatabaseDialectEnum.SQLCE,
              ["npgsqlconnection"] = DatabaseDialectEnum.POSTGRES,
              ["sqliteconnection"] = DatabaseDialectEnum.SQLLITE,
              ["mysqlconnection"] = DatabaseDialectEnum.MYSQL,
              ["fbconnection"] = DatabaseDialectEnum.FIREBASE
          };

這樣咱們切換不一樣的數據庫只須要配置數據庫鏈接字符串便可。sql

以mssql爲例,配置數據庫鏈接字符串數據庫

  "ConnectionString": {
    "sqlconnection": "Data Source=.;Initial Catalog=Db;User ID=sa;Password=**;Enlist=false;Max Pool SIZE=500;Min Pool SIZE=50;MultipleActiveResultSets=True",
    "sqlconnection_slaver_1": "Data Source=.;Initial Catalog=Db;User ID=sa;Password=**;Enlist=false;Max Pool SIZE=500;Min Pool SIZE=50;MultipleActiveResultSets=True",
    "sqlconnection_slaver_2": "Data Source=.;Initial Catalog=Db;User ID=sa;Password=**;Enlist=false;Max Pool SIZE=500;Min Pool SIZE=50;MultipleActiveResultSets=True"
  }
Key: sqlconnection爲主庫(master)鏈接字符串,Key: sqlconnection_slaver_1和sqlconnection_slaver_2爲兩個從庫(slaver)鏈接字符串。多個從庫(slaver)能夠實現隨機訪問。也能夠採用其餘算法來負載均衡。

根據字符串鏈接配置咱們獲得 主庫 鏈接串,和從庫鏈接串集合。同時根據鏈接串的key 肯定數據庫種類。代碼以下:

        /// <summary>
        /// 主數據庫鏈接串
        /// </summary>
        private string MasterConnectionString { get; set; }
        /// <summary>
        /// 從數據庫鏈接串集合
        /// </summary>
        private List<string> SlaverConnectionStrings { get; set; } = new List<string>();
        public ConnectionFactory(IConfiguration configuration, ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<ConnectionFactory>();
            var connectionKeys = configuration.GetSection("ConnectionString").GetChildren().Select(s => s.Key).ToArray();
            foreach (var connKey in connectionKeys)
            {
                var connSplit = connKey.Split('_');
                if (connSplit.Length == 1)
                {
                    MasterConnectionString = configuration[$"ConnectionString:{connKey}"];
            //根據鏈接字符串約定,肯定數據庫類型 DatabaseDialect
= DialectDictionary[connKey]; } else { SlaverConnectionStrings.Add(configuration[$"ConnectionString:{connKey}"]); } } }
        /// <summary>
        /// 數據庫類型
        /// </summary>
        public DatabaseDialectEnum DatabaseDialect { get; private set; }

獲取主庫鏈接多線程

        private IDbConnection GetMasterConnection()
        {
            return GetConnection(MasterConnectionString);
        }

獲取從庫鏈接,這裏採用隨機算法,若是沒有配置從庫,這裏會返回主庫鏈接。app

        private IDbConnection GetSlaverConnection()
        {
            int sc = SlaverConnectionStrings.Count();
            if (sc > 0)
            {
                Random random = new Random();
                int index = random.Next(0, sc);
                return GetConnection(SlaverConnectionStrings[index]);
            }
            else
            {
                _logger.LogInformation("沒有設置從庫,將創建主庫鏈接");
                return GetMasterConnection();
            }
        }    
        private IDbConnection GetConnection(string connectionString) => DatabaseDialect switch
        {
            DatabaseDialectEnum.MSSQL =>new ProfiledDbConnection(new SqlConnection(connectionString),MiniProfiler.Current),
            DatabaseDialectEnum.MYSQL => new ProfiledDbConnection(new MySqlConnection(connectionString), MiniProfiler.Current),
            _ => throw new NotImplementedException()
        };
注:這裏採用MiniProfiler來監控數據庫鏈接性能,因此 返回的connection用ProfiledDbConnection進行了包裝。

主從數據源類型以下:負載均衡

    public enum DataSourceEnum
    {
        MASTER,
        SLAVE
    }

本ConnectionFactory爲單例模式,存在多線程訪問的狀況,因此數據源設置爲ThreadLocal<DataSourceEnum>,線程內共享。dom

private static ThreadLocal<DataSourceEnum> threadLocal = new ThreadLocal<DataSourceEnum>();
        /// <summary>
        /// 當前線程數據源 
        /// </summary>
        /// <param name="sourceEnum"></param>     
        public DataSourceEnum DataSource
        {
            set { threadLocal.Value = value; }
            get { return threadLocal.Value; }
        }

下面正式獲取IDbConnection性能

        public IDbConnection GetDbConnection()
        {
            if (DataSource == DataSourceEnum.MASTER)
            {
                return GetMasterConnection();
            }
            else
            {
                return GetSlaverConnection();
            }
        }

使用:spa

根據文章開頭所描述的實際操做來進行主從庫訪問。

        private IDbConnection GetDbConnection(DataSourceEnum dataSource)
        {
            ConnectionFactory.DataSource = dataSource;
            return ConnectionFactory.GetDbConnection();
        }
using var connection = GetDbConnection(DataSourceEnum.MASTER);
 connection.Execute(sql, param, CurrentTransaction, null, commandType)
 using var connection = GetDbConnection(DataSourceEnum.SLAVE);
 connection.Get<T>(id, CurrentTransaction, CommandTimeout)

 

奉上所有代碼

    public class ConnectionFactory : IConnectionFactory
    {
        private readonly ILogger _logger;
        private static ThreadLocal<DataSourceEnum> threadLocal = new ThreadLocal<DataSourceEnum>();
        static ConnectionFactory()
        {
            //設置dapper的tableName取值
            SqlMapperExtensions.TableNameMapper = (type) => type.Name;
        } 

        /// <summary>
        /// 當前線程數據源 
        /// </summary>
        /// <param name="sourceEnum"></param>     
        public DataSourceEnum DataSource
        {
            set { threadLocal.Value = value; }
            get { return threadLocal.Value; }
        }

        /// <summary>
        /// 主數據庫鏈接串
        /// </summary>
        private string MasterConnectionString { get; set; }
        /// <summary>
        /// 從數據庫鏈接串集合
        /// </summary>
        private List<string> SlaverConnectionStrings { get; set; } = new List<string>();
        public ConnectionFactory(IConfiguration configuration, ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<ConnectionFactory>();
            var connectionKeys = configuration.GetSection("ConnectionString").GetChildren().Select(s => s.Key).ToArray();
            foreach (var connKey in connectionKeys)
            {
                var connSplit = connKey.Split('_');
                if (connSplit.Length == 1)
                {
                    MasterConnectionString = configuration[$"ConnectionString:{connKey}"];
                    DatabaseDialect = DialectDictionary[connKey];
                }
                else
                {
                    SlaverConnectionStrings.Add(configuration[$"ConnectionString:{connKey}"]);
                }

            }
        }
        /// <summary>
        /// 數據庫方言集合
        /// </summary>
        private readonly Dictionary<string, DatabaseDialectEnum> DialectDictionary
          = new Dictionary<string, DatabaseDialectEnum>
          {
              ["sqlconnection"] = DatabaseDialectEnum.MSSQL,
              ["sqlceconnection"] = DatabaseDialectEnum.SQLCE,
              ["npgsqlconnection"] = DatabaseDialectEnum.POSTGRES,
              ["sqliteconnection"] = DatabaseDialectEnum.SQLLITE,
              ["mysqlconnection"] = DatabaseDialectEnum.MYSQL,
              ["fbconnection"] = DatabaseDialectEnum.FIREBASE
          };
        /// <summary>
        /// 數據庫方言
        /// </summary>
        public DatabaseDialectEnum DatabaseDialect { get; private set; }

        private IDbConnection GetConnection(string connectionString) => DatabaseDialect switch
        {
            DatabaseDialectEnum.MSSQL =>new ProfiledDbConnection(new SqlConnection(connectionString),MiniProfiler.Current),
            DatabaseDialectEnum.MYSQL => new ProfiledDbConnection(new MySqlConnection(connectionString), MiniProfiler.Current),
            _ => throw new NotImplementedException()
        };
        public IDbConnection GetDbConnection()
        {
            if (DataSource == DataSourceEnum.MASTER)
            {
                return GetMasterConnection();
            }
            else
            {
                return GetSlaverConnection();
            }
        }
        private IDbConnection GetMasterConnection()
        {
            return GetConnection(MasterConnectionString);
        }
        private IDbConnection GetSlaverConnection()
        {
            int sc = SlaverConnectionStrings.Count();
            if (sc > 0)
            {
                Random random = new Random();
                int index = random.Next(0, sc);
                return GetConnection(SlaverConnectionStrings[index]);
            }
            else
            {
                _logger.LogInformation("沒有設置從庫,將從創建主庫鏈接");
                return GetMasterConnection();
            }
        }    
    }

    public enum DataSourceEnum
    {
        MASTER,
        SLAVE
    }
相關文章
相關標籤/搜索