161920. Using spring AOP to implement the MySQL database read-write separation case analysis

Source: Internet
Author: User

First, preface

The database reading and writing separation strategy in distributed environment is a key solution to solve the bottleneck of database reading and writing performance, and it is to maximize the speed and concurrency of reading (read) data in the application.

In the database read and write separation, we first have to do the master and slave configuration of the database, the simplest is a master and a slave (large site system, of course, it will be very complex, here is only the simplest case analysis). The master-slave database maintains the same data through the master-slave configuration, we access the slave from the database during the read operation and access the primary database master while the write is in progress. This reduces the pressure on a single server.

When conducting a read-write separation case analysis. First, the master-slave replication of the configuration database, the following are two methods (optional):

1, MySQL5.6 database Master-Slave (Master/slave) synchronous installation and configuration of the detailed

2. Use the Mysqlreplicate command to quickly build Mysql master-slave replication

Of course, simply to see how the code to implement the database read and write separation, completely do not have to configure the master-slave database, only two installed the same database machine.

two ways to realize the separation of reading and writing

In development, there are two common ways to achieve read-write separation:

1, the first way is our most common way, is to define 2 database connections, one is Masterdatasource, the other is Slavedatasource. When we update the data we read Masterdatasource, we read the Slavedatasource when we query the data. This is a very simple way, I will not repeat it.

2, the second way Dynamic Data source switching, that is, when the program is running, the data source is dynamically woven into the program, so as to choose to read the main library or from the library. The main techniques used are: annotation,spring AOP, Reflection.

The implementation method is described in detail below.

Third, AOP implementation of master-slave database read and write separation cases

1. Project Code Address

At present, the project address of the demo is open source China code Cloud Top: Http://git.oschina.net/xuliugen/aop-choose-db-demo

or csdn free Download:

http://download.csdn.net/detail/u010870518/9724872

2. Project Structure

In addition to the tagged code, the other is primarily the configuration code and the business code.

3. Specific analysis

The project is a demo,spring, Spring MVC, and MyBatis for the SSM framework, and the specific configuration file is not too much to describe.

(1) Usercontoller analog read-write data

Analog read-write data, call Iuserservice.

(2) Spring-db.xml read-write Data source configuration

<?xml version= "1.0" encoding= "UTF-8"?><Beansxmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation="Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-bea Ns-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd " ><BeanId="Statfilter"class="Com.alibaba.druid.filter.stat.StatFilter"lazy-init="True" ><PropertyName="Logslowsql"Value="True"/><PropertyName="Mergesql"Value="True"/></Bean><!--database Connection--<BeanId="Readdatasource"class="Com.alibaba.druid.pool.DruidDataSource"Destroy-method="Close"Init-method="Init"lazy-init="True" ><PropertyName="Driverclassname"Value="${driver}"/><PropertyName="url"Value="${url1}"/><PropertyName="Username"Value="Root"/><PropertyName="Password"Value="${password}"/><!--omit some content--</Bean><BeanId="Writedatasource"class="Com.alibaba.druid.pool.DruidDataSource"Destroy-method="Close"Init-method="Init"lazy-init="True" ><PropertyName="Driverclassname"Value="${driver}"/><PropertyName="url"Value="${url}"/><PropertyName="Username"Value="Root"/><PropertyName="Password"Value="${password}"/><!--omit some content--</Bean><!--configure dynamically allocated read and write data sources--<BeanId="DataSource"class="Com.xuliugen.choosedb.demo.aspect.ChooseDataSource"lazy-init="True" ><PropertyName="Targetdatasources" ><MapKey-type="Java.lang.String"Value-type="Javax.sql.DataSource" ><!--write--<Entrykey="Write"value-ref="Writedatasource"/><!--read--<Entrykey="Read"value-ref="Readdatasource"/></Map></Property><PropertyName="Defaulttargetdatasource"ref="Writedatasource"/><PropertyName="Methodtype" ><map key-type="java.lang.String" > <!--read ---<                Entry key="read" value=", get,select,count,list,query"/> <!--write-- <entry key="Write"value=", Add,create,update,delete,remove,"/> </ map> </property> </bean></beans>

In the above configuration, Readdatasource and Writedatasource two data sources are configured, but only DataSource is assigned to Sqlsessionfactorybean for management, which is used to: com.xuliugen.choosedb.demo.aspect.ChooseDataSource This is the database selection.


(3) Choosedatasource


(4) Datasourceaspect for specific methods of AOP interception


(5) Datasourcehandler, handler class of data source



161920. Using spring AOP to implement the MySQL database read-write separation case analysis

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.