EntityFramework-based master-slave database read/write splitting service plug-in, master-slave database Separation

Source: Internet
Author: User
Tags mssqlserver

EntityFramework-based master-slave database read/write splitting service plug-in, master-slave database Separation
EntityFramework-based plug-in for database Master/Slave read/write splitting 1. Version Information and source code

1.1Version Information

V1.01 beta (), developed based on EF 6.1, supports all EF6 versions after EF 6.1.

 

1.2Open Source Code address

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

The core source code of the EF database Master/Slave read/write splitting service is located in the src \ NDF. Data. EntityFramework \ MasterSlaves folder.

 

2. Function Overview

2.1Data Operations Based on EF6 are supported:

2.1.1 all data write operations are automatically forwarded to the Master server (Master, that is, the write operation server );

2.1.2 all data query operations are automatically forwarded to the Slave server (Slave, that is, the query operation server );

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

 

2.2When forwarding read/write command requests to the corresponding database server, one master, multiple slave management is supported.

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;

Note: A data synchronization mechanism should be established between the Master server and the Slave server in advance, which can be completed by configuring the DBMS system.

 

2.3Supports automatic detection of server running status:

2.3.1 automatically checks the online status of the Master server;

2.3.2 automatically checks the online status of each Slave server node in the Set Slave server list;

2.3.3 you can customize the time frequency for automatic server detection;

 

2.4You can automatically switch to the Master node when the Slave server node is unavailable:

If multiple Slave server nodes are set, the available server nodes are automatically selected based on the online status of the automatically detected Slave server during each query operation. If all Slave nodes are unavailable, you can determine whether to automatically switch the Data Query operation to the Master server based on the configuration;

 

2.5You can automatically switch to the Slave node when the Master server node is unavailable:

When EF6-based data change operations are performed, if the status of the Master server is detected to be unavailable, you can determine whether to automatically switch the data change operation to the first available item in the Slave server list based on the configuration. (This setting is generally not recommended, because the use of the Server Load balancer as the Master server can make the application program offline after the Master node fails, it also brings about Data Consistency issues between the Server Load balancer nodes .);

 

2.6Supports load balancing among multiple Slave nodes:

If multiple Slave server nodes are set, you can select the first available Slave server in the set order for each query operation, you can also randomly select any of the available Server Load balancer instances (this setting can effectively distribute the query pressure on the server Load balancer) to run the query command.

 

2.7 support configuration of the Action interceptor of the Aspect-Oriented EF database Master/Slave read/write splitting service:

You can define and register the interceptor that implements the interface IMasterSlaveInterceptor to execute specific actions in the EF database Master/Slave read/write splitting service:

Before scanning the available status of the server node, after scanning the available status of the server node, before modifying the connection string of the Database Operation Command, and after modifying the connection string of the Database Operation Command, the user-specified additional actions (for example, logs are automatically recorded or message notifications are sent when the scan to the server node is unavailable ).

 

2.8Supports multi-DbContext configuration in EF:

If the project uses multiple types of EF object context (System. Data. Entity. DbContext) objects, different master-slave read/write splitting database connection schemes can be configured for different types of DbContext;

 

2.9Supports hot swapping configurations for Master server nodes and Slave server nodes:

You can directly modify the configuration file ef. masterslave. the content in config to automatically refresh related configuration connections. After the configuration file is modified and re-activated, custom change event notifications are supported.

 

3. Instructions for use

3.1Set automatic synchronization between multiple database server instances

It is preferred to use the database management system (DBMS) to configure the master-slave automatic synchronization mechanism between multiple database server instances. For example:

3.1.1 If the MSSQLSERVER database system is used, you can configure the replication and subscription policies between multiple database server instances;

3.1.2 if the MySQL database system is used, you can configure a master-slave replication policy between multiple database server instances;

3.1.3 other Oracle, db2...

 

3.2Add 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:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 3 <configuration> 4 5 <configSections> 6 7 <section name = "ef. masterslave "type =" NDF. data. entityFramework. masterSlaves. configFile. EFMasterSlaveSection, NDF. data. entityFramework "8 9 requirePermission =" false "/> 10 11 </configSections> 12 13 <ef. masterslave> 14 15 <! -- The following is a MyProject that uses the EF object context (DbContext) type. data. myDbContext database operation configuration Master/Slave read/write splitting service --> 16 17 <applyItem targetContext = "MyProject. data. myDbContext, MyProject. data "18 19 bytes =" false "autoSwitchMasterOnSlavesFauled =" true "20 21 serverStateScanInterval =" 60 "serverStateScanWithNoOffline =" false "22 23 slaveRandomization =" true "> 24 25 <master connectionString "server = 192.168.0.99; port = 330 6; user id = root; password = 123456; persistsecurityinfo = True; database = testdb; convertzerodatetime = True; allowzerodatetime = True "/> 26 27 <slaves> 28 29 <add connectionString =" server = 192.168.0.101; port = 3306; user id = root; password = 123456; persistsecurityinfo = True; database = testdb; convertzerodatetime = True; allowzerodatetime = True "order =" 0 "/> 30 31 <add connectionString =" server = 192.168.0.102; port = 3306; user id = root; Password = 123456; persistsecurityinfo = True; database = testdb; convertzerodatetime = True; allowzerodatetime = True "order =" 1 "/> 32 33 <add connectionString =" server = 192.168.0.103; port = 3306; user id = root; password = 123456; persistsecurityinfo = True; database = testdb; convertzerodatetime = True; allowzerodatetime = True "order =" 2 "/> 34 35 </slaves> 36 37 </applyItem> 38 39 <! -- Configure the master-slave read/write splitting service for another EF object context (DbContext) --> 40 41 <! -- <ApplyItem...> 42 43 <master... /> 44 45 <slaves> 46 47 <add... /> 48 49 <add... /> 50 51 </slaves> 52 53 </applyItem> --> 54 55 </ef. masterslave> 56 57 </configuration>

 

3.3Introduce the dependent package in the project

3.3.1 EntityFramework 6.1 and later versions;

3.3.2 Microsoft Enterprise Library-Data Access Application Block 6;

3.3.3 Newtonsoft. Json. dll 6.0 or later;

3.3.4 NDF. Utilities. dll;

3.3.5 NDF. Data. dll;

3.3.6 NDF. Data. EntityFramework. dll;

 

3.4Add startup code to the Project

In the project startup Code (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 snippet:

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

The type parameter passed in the method should be ef. masterslave. the type shown in the targetContext attribute of applyItem In the config file indicates the type of EF object context (DbContext) for which the read/write splitting service is to be configured.

 

4. Miscellaneous

4.1 The automatic synchronization mechanism for data content in the master-slave database is completed by the database management system (DBMS, such as MSSQLSERVER, Oracle, MySQL, DB2, and so on, these functions are not provided by this plug-in. Currently, almost all mainstream DBMS systems provide functions related to the automatic synchronization mechanism of master-slave databases;

4.2 The EF database Master/Slave read/write splitting solution supports all common database transactions and distributed transaction operations. However, distributed transactions also require the support of the database management system (DBMS); otherwise, they are invalid;

4.3 perform master-slave database read/write Splitting Based on EF6 and the plug-in. The program automatically checks the Transaction Status of the executed database operation, and automatically forward all add, delete, modify, and query requests with database transactions or distributed transactions to the Master server.

4.4 This article briefly introduces the master-slave read/write splitting plug-in for this EF database. I will introduce the source code implementation principles and ideas of this plug-in future blog posts.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.