上一篇中討論了Rookey.Frame框架菜單配置功能,這一節我們繼續學習Rookey.Frame框架的數據庫連接配置。
之前介紹了Rookey.Frame框架支持跨多數據庫,並且支持讀寫分離,不過目前還不是太完善,目前只對mssql server完全支持,對其他數據庫還有部分功能未實現,在后續會逐漸完善。針對多數據庫配置,框架中有兩個地方配置數據庫連接字符串,一個是網站根目前下的web.config,另外一個是Rookey.Frame.Web\Config\modelConfig.xml,在web.config中數據庫連接配置如下:
<connectionStrings> <add name="DbReadConnString" connectionString="Data Source=.;Initial Catalog=Rookey_Frame;User ID=sa;Password=123456;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30" providerName="System.Data.SqlClient" /> </connectionStrings>
web.config中配置的連接字符串為默認連接數據庫,其中name為DbReadConnString的默認為讀數據庫,name為DbWriteConnString的為寫數據庫。
在modelConfig.xml中配置如下:
<?xml version="1.0" encoding="utf-8"?> <Root> <BaseLogEntity isEnableCache="0" cacheType="0" dbType="MsSqlServer" readConnString="Data Source=.;Initial Catalog=Rookey_Log;User ID=sa;Password=123456;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30"></BaseLogEntity> <!--系統模塊全部啟用緩存--> <BaseSysEntity isEnableCache="1" cacheType="0"></BaseSysEntity> </Root>
其中BaseLogEntity、BaseSysEntity分別為日志基類和系統模塊基類,這里配置的意思是,系統日志所有模塊對應的連接字符串為Data Source=.;Initial Catalog=Rookey_Log;User ID=sa;Password=123456;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30,對於系統模塊則為默認的連接數據庫,這里BaseLogEntity、BaseSysEntity也可以換成具體的模塊類名(也即數據表名),那所配置的模塊則是單獨的數據庫,也就是說系統中所有的功能模塊都可以配置單獨的數據庫,系統查找數據庫連接串時,會先找當前模塊有沒有配置數據庫連接串,如果沒有則找父類配置,父類找不到再找父類,目前支持三級查找,如果沒有找到自定義配置數據庫連接,則取默認數據庫連接,在Rookey.Frame.DALFactory\OrmLiteDalFactory.cs類中有個專門獲取連接字符串的方法:
/// <summary> /// 獲取連接字符串 /// </summary> /// <param name="connString">數據庫連接字符串</param> /// <param name="read">是否讀</param> /// <returns></returns> private string GetConnString(string connString, bool read = true) { string lastConnStr = !string.IsNullOrEmpty(connString) ? connString : (read ? this.ReadConnectionString : this.WriteConnectionString); string modelConfigConnString = GetModelConfigConnString(read); //取模塊自定義數據庫連接配置 if (string.IsNullOrEmpty(connString) && !string.IsNullOrEmpty(modelConfigConnString)) { lastConnStr = modelConfigConnString; } NotNullCheck.NotEmpty(lastConnStr, "數據庫連接字符串"); return lastConnStr; }
/// <summary> /// 獲取實體連接字符串 /// </summary> /// <param name="modelType">實體類型對象</param> /// <param name="dbType">數據庫類型</param> /// <param name="read">讀寫分離標識,是否讀數據庫,為否則取寫數據庫</param> /// <returns></returns> public static string GetModelConnString(Type modelType, out string dbType, bool read = true) { dbType = string.Empty; string modelConfigPath = GetModelConfigXml(); string node = string.Format("/Root/{0}", modelType.Name); bool nodeIsExists = XmlHelper.NodeIsExists(modelConfigPath, node); if (!nodeIsExists) //不存在實體節點配置信息,找對應基類的節點配置信息 { //取實體基類 Type baseType = modelType.BaseType; if (baseType != null) //存在基類 { node = string.Format("/Root/{0}", baseType.Name); //基類節點 nodeIsExists = XmlHelper.NodeIsExists(modelConfigPath, node); } } if (!nodeIsExists) return string.Empty; string tempConnStr = XmlHelper.Read(modelConfigPath, node, read ? "readConnString" : "writeConnString"); if (!read && string.IsNullOrEmpty(tempConnStr)) { tempConnStr = XmlHelper.Read(modelConfigPath, node, "readConnString"); } dbType = XmlHelper.Read(modelConfigPath, node, "dbType"); return tempConnStr; }
另外isEnableCache為緩存配置標識,等於1時表示該基類下所有模塊或該模塊啟用緩存,cacheType的值與以下枚舉值對應:
/// <summary> /// 緩存類型 /// </summary> public enum CacheProviderType { /// <summary> /// 本地緩存 /// </summary> LOCALMEMORYCACHE = 0, /// <summary> /// MemcachedCache分布式緩存 /// </summary> MEMCACHEDCACHE = 1, /// <summary> /// redis分布式緩存 /// </summary> REDIS = 2 }
另外緩存配置或數據庫配置也可在系統界面上實現 :
最后需要說明的是針對Memcached和Redis實現分布式緩存則還需要配置Rookey.Frame.Web\Config下的memcachedcache.config和rediscache.config
memcachedcache.config:
<?xml version="1.0"?> <MemcachedCacheConfigInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ServerList> <string>127.0.0.1:11211</string> </ServerList> <MinPoolSize>10</MinPoolSize> <MaxPoolSize>20</MaxPoolSize> <ConnectionTimeOut>10</ConnectionTimeOut> <QueueTimeOut>10</QueueTimeOut> <ReceiveTimeOut>10</ReceiveTimeOut> <DeadTimeOut>100</DeadTimeOut> <CacheTimeOut>3600</CacheTimeOut> </MemcachedCacheConfigInfo>
rediscache.config:
<?xml version="1.0" encoding="utf-8"?> <RedisConfigInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Host>127.0.0.1</Host> <Port>6379</Port> <Pwd></Pwd> <InitalDB>0</InitalDB> </RedisConfigInfo>
好了,今天的介紹就到此地,下一篇介紹系統初始化功能,祝大家生活愉快!