天天看點

windows平台的分布式微服務解決方案(3)--資料庫的負載均衡

本文技術點思路梳理:

  1. 建立用來實施負載均衡的一組資料庫。
  2. 通過DeveloperSharp.xml來配置負載均衡政策。
  3. 在App.config/Web.config中添加連結DeveloperSharp.xml的appSettings節點。
  4. 建立基于DeveloperSharp.dll元件的“負載均衡器”類(本篇為:StudentLB類)。
  5. 通過“負載均衡器”産生的ConnectionString連結資料庫。

為了示範資料庫的負載均衡(Load Balance),我們先建立三個資料庫,它們的名字分别為YZZ、YZZ1、YZZ2。然後在這三個資料庫中分别建立表t_Student,這些表的結構一樣,内容稍有差異,見下圖:

YZZ中t_Student的内容

---------------------

Id  Name  Age

1   張安   25

2   王鑫   22

3   周雲   20

YZZ1中t_Student的内容

1   張安1  25

2   王鑫1  22

3   周雲1  20

YZZ2中t_Student的内容

1   張安2  25

2   王鑫2  22

3   周雲2  20

然後,我們在DeveloperSharp.xml這個配置檔案中設定如上三個資料源的負載均衡政策,檔案内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<DeveloperSharp>
    <DatabaseClusterList>
        <DatabaseCluster Id="StudentData">
            <Database Id="A1" Enable="true" Weight="100" DatabaseType="SqlServer" ConnectionString="Server=localhost;Database=YZZ;Uid=sa;Pwd=123"/>
            <Database Id="A2" Enable="true" Weight="100" DatabaseType="SqlServer" ConnectionString="Server=localhost;Database=YZZ1;Uid=sa;Pwd=123"/>
            <Database Id="A3" Enable="true" Weight="100" DatabaseType="SqlServer" ConnectionString="Server=localhost;Database=YZZ2;Uid=sa;Pwd=123"/>
        </DatabaseCluster>
    </DatabaseClusterList> 
</DeveloperSharp>      
windows平台的分布式微服務解決方案(3)--資料庫的負載均衡

對此xml配置檔案說明如下:

(1)     每一個DatabaseCluster節點代表了一組資料庫,此節點的Id值(本文示例值是:StudentData)後續會在程式中使用。

(2)     Database節點中的Weight屬性代表了使用權重。本文示例的三個資料庫的Weight值分别是100、100、100,則這三個資料庫的負載均衡使用配置設定比例将會是1:1:1。若把這三個值分别設定為100、50、50,則這三個資料庫的使用配置設定比例将會變為2:1:1。設定成你想要的比例吧。

(3)     Database節點中的Enable屬性代表了是否可用。true代表可用,false代表不可用。

接下來,我們看看怎麼在程式中使用上述的這組資料庫及其配置檔案,來實作負載均衡。

為了示範負載均衡效果,我們首先在Visual Studio中建立一個控制台工程,并為工程引用添加了Entity Framework元件作為通路資料庫的工具(你也可以換成其它資料庫通路工具,原理一樣,完全不受影響)。

然後,我們做如下四個操作。

【第一步】:為工程引用添加DeveloperSharp.dll這個核心元件。

【第二步】:在工程配置檔案App.config(或Web.config)中添加appSettings節點,節點内容如下:

<appSettings>
    <add key="DatabaseType" value="" />
    <add key="ConnectionString" value="" />
    <add key="ErrorPage" value="" />
    <add key="ErrorLog" value="D:\Test\Assist\log.txt" />
    <add key="ConfigFile" value="D:\Test\Assist\DeveloperSharp.xml" />
  </appSettings>      

其中,ConfigFile的設定是為了連結前述的DeveloperSharp.xml這個配置檔案。ErrorLog則是設定一個錯誤日志檔案。它們均需要設定為檔案的“絕對路徑”(此處使用“絕對路徑”而不是“相對路徑”,一是有利于安全性,二是有利于分布式部署)

【第三步】:建立一個StudentLB.cs類檔案,它就是資料庫負載均衡的核心器件,内容如下:

//這個屬性就是用作映射負載均衡。
    //其“值”需設定為前述DeveloperSharp.xml配置檔案中某個DatabaseCluster節點的Id值。
    [DeveloperSharp.Structure.Model.LoadBalance.DataSourceCluster("StudentData")]
    public class StudentLB : DeveloperSharp.Structure.Model.DataLayer
    {
        //類中沒有任何代碼
    }      

說明:“負載均衡器”類(本篇為:StudentLB類)必須繼承自DeveloperSharp.Structure.Model.DataLayer類,并且在其上設定DeveloperSharp.Structure.Model.LoadBalance.DataSourceCluster屬性的初始化值為DeveloperSharp.xml配置檔案中某個DatabaseCluster節點的Id值。

【第四步】:為控制台應用類,添加通過負載均衡器(StudentLB類)通路資料庫的代碼,注意:核心代碼就一行而已!!此示例連續3次通路資料庫做同一操作,看會顯示什麼結果。如下:

class Program
    {
        static void Main(string[] args)
        {
            string NameList = "";

            //第一次通路資料庫
            var SLB = (new StudentLB()) as DeveloperSharp.Structure.Model.DataLayer;
            var db = new Entities(SLB.IDA.ConnectionString);//每次會根據配置的負載均衡政策輸出對應的ConnectionString
            t_Student Stu = db.t_Student.Where(t => t.Id == 3).FirstOrDefault();
            NameList += Stu.Name;

            //第二次通路資料庫
            SLB = (new StudentLB()) as DeveloperSharp.Structure.Model.DataLayer;
            db = new Entities(SLB.IDA.ConnectionString);//每次會根據配置的負載均衡政策輸出對應的ConnectionString
            Stu = db.t_Student.Where(t => t.Id == 3).FirstOrDefault();
            NameList += Stu.Name;

            //第三次通路資料庫
            SLB = (new StudentLB()) as DeveloperSharp.Structure.Model.DataLayer;
            db = new Entities(SLB.IDA.ConnectionString);//每次會根據配置的負載均衡政策輸出對應的ConnectionString
            Stu = db.t_Student.Where(t => t.Id == 3).FirstOrDefault();
            NameList += Stu.Name;

            //輸出
            Console.WriteLine(NameList);
            Console.ReadLine();

        }
    }      

從以上示例代碼我們可以清晰的得知:其實資料庫連結字元串ConnectionString就是實作負載均衡的關鍵所在。而SLB.IDA.ConnectionString則每次會根據配置的負載均衡政策輸出對應的連結字元串。

示例程式輸出顯示結果如下:

周雲周雲1周雲2

最後提示一點:若要把一組資料庫的負載均衡應用改為單資料庫應用,隻需要把DeveloperSharp.xml配置檔案中DatabaseCluster節點下的Database節點數量設定為一個即可實作。

【本文采用.NET/C#語言撰寫,如需其它語言版本,請聯系作者索取。】

【本文采用Sql Server資料庫撰寫,如需其它資料庫版本,請聯系作者索取。】

相關下載下傳:

資料庫的負載均衡-示例代碼(dp1-DbBalance.rar)

windows平台的分布式微服務解決方案(3)--資料庫的負載均衡

如果文章對你有幫助,請點贊、收藏、關注(原創内容,歡迎轉載,轉載請注明出處)

有疑問想擷取專業技術支援?請掃描左側微信二維碼聯系作者

出處:https://www.cnblogs.com/DeveloperSharp/

本文采用「CC BY 4.0」知識共享協定進行許可,轉載請注明作者及出處。