Spring+mybatis using Interceptor (plugin) to redeem database read-write separation

Source: Internet
Author: User
Tags database load balancing

Using spring's dynamic routing to achieve database load balancing

Many servers exist in the system are "status equivalent", but at the same time they are active (active) state, in load balancing and other factors, data access requests need to be allocated between these database servers reasonable allocation, this time, Through a unified datasource to block this request allocation requirements, thereby shielding the data access class and the specific datasource coupling; Multiple database servers present in the system may or may not be comparable, However, when the data access class is unable to determine exactly which data source should be used for data access at system startup time, it is necessary to decide which data source should be used while the system is running, and we have to use this "alliances" way Exposing a unified datasource to the data access class, the DataSource to disassociate the data access class from the specific data source;
More scenarios need to be judged by a specific application, but not all applications need to be treated as simple as possible. To achieve this "alliances" multi-data source management approach, the general guideline is to implement a custom datasource, Let the DataSource manage multiple database-linked data sources that exist in the system, and the data access class only deals with this custom datasource. There may be multiple ways to manage multiple data sources for this scenario in various projects before spring2.0.1 is released, but After the spring2.0.1 was released, Abstractroutingdatasource was introduced, and the class could be used to implement multi-data source management functions in a universal sense.

Assuming we have three databases for load balancing, and all data access requests end up being evenly distributed across the three database servers, we can quickly implement a prototype that satisfies such a scenario by inheriting the Abstractroutingdatasource ( Prototype):[Java]  View Plain Copy public class prototypeloadbalancedatasource extends  abstractroutingdatasource  {       private lock lock =  new reentrantlock ();       private int counter = 0 ;       private int dataSourceNumber = 3;         @Override        protected Object  Determinecurrentlookupkey ()  {           lock.lock ();            try{                counter++;                int lookupkey = counter % getdatasourcenumber ();                return new integer (lookupkey);            }finally{                lock.unlock ();           }        }       // ...  }   When we introduced Abstractroutingdatasource, we said that to inherit the class, we would usually just give the logic of the Determinecurrentlookupkey () method. Here is the configuration for Prototypeloadbalancedatasource:
[HTML]View plain copy <bean id= "DataSourc1" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property name= "url" value= "..." /> <property name= "driverclassname" value= "..." /> <property name= "username" value= "..." /> <property name= "password" value= "..." /> <!--other property settings-</bean> <bean id= "DataSource2" class= "org.apache.commons.d Bcp. Basicdatasource "destroy-method=" Close "> <property name=" url "value=" ... " />

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.