Discuz!NT3.1 数据库读写分离 代码

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中实现读写分离和读数据库的负载均衡

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值