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=" ... " />