EntityFramework-based database master-slave read-write separation service plugin

1. Version information and source code

1.1  Version Information

v1.01 beta (2015-04-07), developed based on EF 6.1, supports all EF6 versions after EF 6.1.

 

1.2  Open source address

https://github.com/cjw0511/NDF.Infrastructure

About the EF database master-slave read-write separation service core source code is located in the folder: src\ NDF.Data.EntityFramework\MasterSlaves folder.

 

2. Function overview

2.1  Supports data operations based on EF6:

2.1.1 For all data write operations, automatically forward the request to the master server (Master, that is, the write operation server);

2.1.2 For all data query operations, automatically forward the request to the slave server (Slave, that is, the query operation server);

2.1.3 The above database operation request forwarding is completed by changing the database connection string before executing the command, but the change of the data connection string does not require the business developer to change any existing code;

 

2.2  When forwarding read and write command requests to the corresponding database server, support one master and multiple slave management

That is, one database server can be set as the Master server, and one or more database servers can be set as the Slave server at the same time;

Note: The data synchronization mechanism needs to be established in advance between the Master server and the Slave server. This part of the work can be done by configuring the DBMS system.

 

2.3  Support automatic detection of server running status:

2.3.1 It can automatically detect the online status of the Master server;

2.3.2 It can automatically detect the online status of each Slave server node in the set Slave server list;

2.3.3 The time and frequency of automatic detection of server status can be customized;

 

2.4  Supports automatic switching to the Master node when the Slave server node is unavailable:

If multiple Slave server nodes are set, each time a query operation is performed, an available server node will be automatically selected according to the automatically detected online status of the Slave server; if all Slaves are unavailable, you can determine whether to automatically transfer data The query operation is switched to the Master server;

 

2.5  Supports automatic switching to the Slave node when the Master server node is unavailable:

During data change operations based on EF6, if it is detected that the status of the Master server is unavailable, you can determine whether to automatically switch the data change operation to the first available item in the Slave server list according to the configuration (this setting is generally not recommended. Because the use of the Slave server as the Master server can prevent the application from going offline after the Master failure, but it will also bring about data consistency problems between the Slave server nodes.);

 

2.6  Support load balancing between multiple slave nodes:

If multiple slave server nodes are set, each time a query operation is performed, the first available slave server can be selected according to the set order, or any one of all available slave servers can be selected randomly (this setting can be effective Disperse Slave server query pressure) to execute query commands.

 

2.7 Support aspect-oriented EF database master-slave read-write separation service action interceptor configuration:

You can define and register an interceptor that implements the interface IMasterSlaveInterceptor to implement specific actions in the EF database master-slave read-write separation service:

Before scanning the server node availability status, after scanning the server node availability status, before modifying the connection string of the database operation command, after modifying the connection string of the database operation command to perform additional actions specified by the user (for example, when the server node is scanned to be unavailable) Automatically record logs or send message notifications).

 

2.8  Support multiple DbContext type configuration in EF:

If multiple types of EF entity context (System.Data.Entity.DbContext) objects are used in the project, it is supported to configure different master-slave read-write separation database connection schemes for each different type of DbContext;

 

2.9  Support hot-plug configuration of Master server node and Slave server node:

That is, without stopping the running of the project, you can directly modify the content in the configuration file ef.masterslave.config to achieve the effect of automatically refreshing the relevant configuration connections; after modifying the configuration file and taking effect again, it supports custom change event notifications.

 

3. Instructions for use

3.1  Setting up automatic synchronization between multiple database server instances

It is preferred to configure a master-slave automatic synchronization mechanism between multiple database server instances through a database management system (DBMS), for example:

3.1.1 If the MSSQLSERVER database system is used, the replication and subscription strategies between multiple database server instances can be configured;

3.1.2 If the MySQL database system is used, the master-slave replication strategy between multiple database server instances can be configured;

3.1.3 Other Oracle, DB2...

 

3.2  Add a configuration file to the project

Add the configuration file ef.masterslave.config in the project root directory, and modify the content according to the rules. The following is a reference configuration method:

<?xml version="1.0" encoding="utf-8" ?>

        <configuration>

          <configSections>

            <section name="ef.masterslave" type="NDF.Data.EntityFramework.MasterSlaves.ConfigFile.EFMasterSlaveSection, NDF.Data.EntityFramework"

                     requirePermission="false" />

          </configSections>

          <ef.masterslave>

            <!-- The following is to configure the master-slave read-write separation service for all database operations with EF entity context (DbContext) type MyProject.Data.MyDbContext -->

            <applyItem targetContext="MyProject.Data.MyDbContext, MyProject.Data"

                       autoSwitchSlaveOnMasterFauled="false" autoSwitchMasterOnSlavesFauled="true"

                       serverStateScanInterval="60" serverStateScanWithNoOffline="false"

                       slaveRandomization="true" >

              <master connectionString="server=192.168.0.99;port=3306;user id=root;password=123456;persistsecurityinfo=True;database=testdb;convertzerodatetime=True;allowzerodatetime=True" />

              <slaves>

                <add connectionString="server=192.168.0.101;port=3306;user id=root;password=123456;persistsecurityinfo=True;database=testdb;convertzerodatetime=True;allowzerodatetime=True" order="0" />

                <add connectionString="server=192.168.0.102;port=3306;user id=root;password=123456;persistsecurityinfo=True;database=testdb;convertzerodatetime=True;allowzerodatetime=True" order="1" />

                <add connectionString="server=192.168.0.103;port=3306;user id=root;password=123456;persistsecurityinfo=True;database=testdb;convertzerodatetime=True;allowzerodatetime=True" order="2" />

              </slaves>

            </applyItem>

            <!-- The following is to configure the master-slave read-write separation service for another EF entity context (DbContext) -->

            <!--<applyItem ...>

              <master ... />

              <slaves>

                <add ... />

                <add ... />

              </slaves>

            </applyItem>-->

          </ef.masterslave>

        </configuration>

 

3.3  Introducing dependent packages into the project

3.3.1 EntityFramework 6.1 or later;

3.3.2   Microsoft Enterprise Library - Data Access Application Block 6;

3.3.3 Newtonsoft.Json.dll version 6.0 or above;

3.3.4   NDF.Utilities.dll;

3.3.5 NDF.Data.dll Download

3.3.6   NDF.Data.EntityFramework.dll;

 

3.4  Add startup code to the project

In the startup code of the project (the console and desktop programs are generally the Main method of the Program type, and the ASP.NET program is generally the Application_Start code block of the Global.asax file), add the following code segment:

NDF.Data.EntityFramework.MasterSlaves.EFMasterSlaveConfig.Register(typeof(MyDbContext));  

 

The type parameter passed in the method should be the type shown in the targetContext attribute of the applyItem section in the ef.masterslave.config configuration file, indicating which type of EF entity context (DbContext) to configure the read-write separation service.

 

4. Other

4.1 The automatic synchronization mechanism of the relevant data content in the master-slave database is completed by the database management system (DBMS, such as MSSQLSERVER, Oracle, MySQL, DB2, etc.), and the functions of this part are not provided by this plug-in; at present, almost all mainstream DBMSs The system provides the functions related to the automatic synchronization mechanism of the master-slave database;

4.2 The EF database master-slave read-write separation scheme supports all common database transactions and distributed transaction operations, but distributed transactions also require the support of a database management system (DBMS), otherwise it is invalid;

4.3 When the database master-slave read-write separation operation is performed based on the cooperation of EF6 and this plug-in, the program will automatically detect the transaction status of the executed database operation, and automatically send all additions, deletions, and changes with database transactions or distributed transactions. Requests and query requests are forwarded to the Master server.

4.4 This article is just an overview of the EF database master-slave read-write separation plug-in written by me. I will introduce the source code implementation principle and ideas of the plug-in in future blog posts.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326440293&siteId=291194637