Discuz!NT 3.1版本为项目用户专门开发了负载均衡、数据库读写分离、分布式缓存和检测工具一系列套件,为超大型社区建设提供了完美的技术解决方案。但在标准版中没有实现读写分离的.在此为标准版实现了数据读写分离
1. 第一步 实现数据库负载均衡类
/// <summary>
/// 负载均衡调度接口
/// </summary>
public interface ILoadBalanceScheduling
{
/// <summary>
/// 获取应用当前负载均衡调度算法下的快照链接信息
/// </summary>
/// <returns></returns>
DbSnapInfo GetConnectDbSnap();
}
以上是Discuz!NT 3.1中提供的:读数据库 的负载均衡调度接口 (注:在标准版中少了Discuz.EntLib.dll)接下来我们来实现上面接口
using System;
using System.Collections.Generic;
using System.Text;
using Discuz.Config;
using DCG = Discuz.Common.Generic;
namespace Discuz.EntLib
{
public class DBLoadBalanceScheduling : ILoadBalanceScheduling
{
/// <summary>
/// 锁对象
/// </summary>
private static object m_lockHelper = new object();
#region ILoadBalanceScheduling 成员
public DbSnapInfo GetConnectDbSnap()
{
lock (m_lockHelper)
{
DbSnapAppConfig snapConfigs = DbSnapConfigs.GetConfig();
if (DbSnapConfigs.GetEnableSnapList().Count <= 0)
throw new NotImplementedException();
return DBLoadBalanceManager.GetDbSnapInfo();
}
}
#endregion
}
class DBLoadBalanceManager
{
/// <summary>
/// 锁对象
/// </summary>
private static object m_lockHelper = new object();
private static DCG.List<DbSnapInfo> enableSnapList = null;
private static int i = -1;
public static DbSnapInfo GetDbSnapInfo()
{
lock (m_lockHelper)
{
DbSnapAppConfig snapConfigs = DbSnapConfigs.GetConfig();
enableSnapList = DbSnapConfigs.GetEnableSnapList();
int count = enableSnapList.Count;
while (true)
{
i = (i + 1) % count;
int cw = enableSnapList[i].Weight;
if (i == 0)
{
cw = cw - GetGcd();
if (cw <= 0)
{
cw = GetMax();
if(cw == 0)
throw new NotImplementedException();
}
}
if (enableSnapList[i].Weight >= cw)
return enableSnapList[i];
}
}
}
private static int GetMax()
{
int max = 0;
foreach (DbSnapInfo info in enableSnapList)
{
if (max <= info.Weight)
{
max = info.Weight;
}
}
return max;
}
private static int GetMin()
{
int min = 0;
for (int i = 0; i < enableSnapList.Count; i++)
{
if (i == 0)
{
min = enableSnapList[i].Weight;
}
if (min > enableSnapList[i].Weight)
{
min = enableSnapList[i].Weight;
}
}
return min;
}
private static int GetGcd()
{
int gcd = 1;
int min = GetMin();
for (int i = min; i <= 2; i--)
{
if (GetMod(i))
{
gcd = i;
break;
}
}
return gcd;
}
/// <summary>
/// 是否能被所有权重整除
/// </summary>
/// <param name="num"></param>
/// <returns>能true,不能false</returns>
private static bool GetMod(int num)
{
bool flag = true;
foreach (DbSnapInfo info in enableSnapList)
{
if (0 != (info.Weight % num))
{
flag = false;
break;
}
}
return flag;
}
}
}
可以在Discuz!NT 3.1中添加一个Discuz.EntLib类库项目 并添加一个DBLoadBalanceScheduling.cs文件并加上面代码
2.第二步添加数据库负载均衡config文件
由于Discuz!NT 3.1中需要dbsnap.config文件,所以需要在"项目路径/web/config/"下添加“dbsnap.config”文件,内容如下:
<?xml version="1.0"?>
<DbSnapAppConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AppDbSnap>true</AppDbSnap>
<WriteWaitTime>6</WriteWaitTime>
<LoadBalanceScheduling>DBLoadBalanceScheduling</LoadBalanceScheduling>
<RecordeLog>false</RecordeLog>
<DbSnapInfoList>
<DbSnapInfo>
<SouceID>0</SouceID>
<Enable>true</Enable>
<DbconnectString>第一个数据连接串</DbconnectString>
<Weight>1</Weight>
</DbSnapInfo>
<DbSnapInfo>
<SouceID>1</SouceID>
<Enable>true</Enable>
<DbconnectString>第一个数据连接串</DbconnectString>
<Weight>1</Weight>
</DbSnapInfo>
</DbSnapInfoList>
</DbSnapAppConfig>
完成以上操作便可以在Discuz!NT 3.1中实现读写分离和读数据库的负载均衡
Discuz!NT3.1 数据库读写分离 代码
最新推荐文章于 2024-09-19 16:47:21 发布