之前介绍了Rookey.Frame框架支持跨多数据库,并且支持读写分离,不过目前还不是太完善,目前只对mssqlserver完全支持,对其他数据库还有部分功能未实现,在后续会逐渐完善。针对多数据库配置,框架中有两个地方配置数据库连接字符串,一个是网站根目前下的web.config,另外一个是Rookey.Frame.Web\Config\modelConfig.xml,在web.config中数据库连接配置如下:
<connectionStrings> <addname="DbReadConnString"connectionString="DataSource=.;InitialCatalog=Rookey_Frame;UserID=sa;Password=123456;Pooling=true;MAXPoolSize=512;MinPoolSize=50;ConnectionLifetime=30"providerName="System.Data.SqlClient"/> </connectionStrings> |
web.config中配置的连接字符串为默认连接数据库,其中name为DbReadConnString的默认为读数据库,name为DbWriteConnString的为写数据库。
在modelConfig.xml中配置如下:
<?xmlversion="1.0"encoding="utf-8"?> <Root> <BaseLogEntityisEnableCache="0"cacheType="0"dbType="MsSqlServer"readConnString="DataSource=.;InitialCatalog=Rookey_Log;UserID=sa;Password=123456;Pooling=true;MAXPoolSize=512;MinPoolSize=50;ConnectionLifetime=30"></BaseLogEntity> <!--系统模块全部启用缓存--> <BaseSysEntityisEnableCache="1"cacheType="0"></BaseSysEntity> </Root> |
其中BaseLogEntity、BaseSysEntity分别为日志基类和系统模块基类,这里配置的意思是,系统日志所有模块对应的连接字符串为DataSource=.;InitialCatalog=Rookey_Log;UserID=sa;Password=123456;Pooling=true;MAXPoolSize=512;MinPoolSize=50;ConnectionLifetime=30,对于系统模块则为默认的连接数据库,这里BaseLogEntity、BaseSysEntity也可以换成具体的模块类名(也即数据表名),那所配置的模块则是单独的数据库,也就是说系统中所有的功能模块都可以配置单独的数据库,系统查找数据库连接串时,会先找当前模块有没有配置数据库连接串,如果没有则找父类配置,父类找不到再找父类,目前支持三级查找,如果没有找到自定义配置数据库连接,则取默认数据库连接,在Rookey.Frame.DALFactory\OrmLiteDalFactory.cs类中有个专门获取连接字符串的方法:
///<summary> ///获取连接字符串 ///</summary> ///<paramname="connString">数据库连接字符串</param> ///<paramname="read">是否读</param> ///<returns></returns> privatestringGetConnString(stringconnString,boolread=true) { stringlastConnStr=!string.IsNullOrEmpty(connString)?connString:(read?this.ReadConnectionString:this.WriteConnectionString); stringmodelConfigConnString=GetModelConfigConnString(read);//取模块自定义数据库连接配置 if(string.IsNullOrEmpty(connString)&&!string.IsNullOrEmpty(modelConfigConnString)) { lastConnStr=modelConfigConnString; } NotNullCheck.NotEmpty(lastConnStr,"数据库连接字符串"); returnlastConnStr; } ///<summary> ///获取实体连接字符串 ///</summary> ///<paramname="modelType">实体类型对象</param> ///<paramname="dbType">数据库类型</param> ///<paramname="read">读写分离标识,是否读数据库,为否则取写数据库</param> ///<returns></returns> publicstaticstringGetModelConnString(TypemodelType,outstringdbType,boolread=true) { dbType=string.Empty; stringmodelConfigPath=GetModelConfigXml(); stringnode=string.Format("/Root/{0}",modelType.Name); boolnodeIsExists=XmlHelper.NodeIsExists(modelConfigPath,node); if(!nodeIsExists)//不存在实体节点配置信息,找对应基类的节点配置信息 { //取实体基类 TypebaseType=modelType.BaseType; if(baseType!=null)//存在基类 { node=string.Format("/Root/{0}",baseType.Name);//基类节点 nodeIsExists=XmlHelper.NodeIsExists(modelConfigPath,node); } } if(!nodeIsExists)returnstring.Empty; stringtempConnStr=XmlHelper.Read(modelConfigPath,node,read?"readConnString":"writeConnString"); if(!read&&string.IsNullOrEmpty(tempConnStr)) { tempConnStr=XmlHelper.Read(modelConfigPath,node,"readConnString"); } dbType=XmlHelper.Read(modelConfigPath,node,"dbType"); returntempConnStr; } |
另外isEnableCache为缓存配置标识,等于1时表示该基类下所有模块或该模块启用缓存,cacheType的值与以下枚举值对应:
///<summary> ///缓存类型 ///</summary> publicenumCacheProviderType { ///<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:
<?xmlversion="1.0"?> <MemcachedCacheConfigInfoxmlns: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: <?xmlversion="1.0"encoding="utf-8"?> <RedisConfigInfoxmlns: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> |
好了,今天的介绍就到此地,下一篇介绍系统初始化功能,祝大家生活愉快!