The SHARDING-JDBC experience of open source middleware

Source: Internet
Author: User
Tags aop constructor xmlns database sharding
0. Basic idea of database shardingHorizontal slicing: Splits the table's data into multiple tables or multiple databases (servers) by some rule, such as by ID hashing. Vertical slicing: A table that is closely related (such as the same module) is sliced out and placed on a server. 1.sharding-jdbcWhen open source middleware: Https://github.com/dangdangdotcom/sharding-jdbc Note: SHARDING-JDBC is currently implementing horizontal slicing, and the vertical split configuration is not implemented.

2. Architecture Diagram


3.maven Pom.xml

<dependency>
	<groupId>com.dangdang</groupId>
	<artifactid>sharding-jdbc-core </artifactId>
	<version>1.0.0</version>
</dependency>
4.SPRING+MYBATIS+SHARDING-JDBC Full ConfigurationConfiguration file based on GitHub demo

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:
	Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spri Ng-beans-3.0.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPRINGFR Amework.org/schema/tx/spring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SP Ringframework.org/schema/aop/spring-aop-3.0.xsd "> <!--0.spring upload config---<bean id=" Multipartreso Lver "class=" Org.springframework.web.multipart.commonS.commonsmultipartresolver "> <property name=" defaultencoding "value=" Utf-8 "></property> <p Roperty name= "maxuploadsize" value= "10485760000" ></property> <property name= "maxinmemorysize" value= "10 960 "></property> </bean> <!--1. Configuring the Scan Package Service,dao-<context:component-scan base-package = "Com.ccy.*"/> <!--2. Configuration Properties File Jdbc.properties--<context:property-placeholder location= "classpath: Jdbc.properties "/> <!--3. Configuring the Data source DataSource <bean id=" Ds_0 "class=" Com.mchange.v2.c3p0.ComboPooledData Source "destroy-method=" Close "> <property name=" driverclass "value=" Com.mysql.jdbc.Driver "/> <
        Property Name= "Jdbcurl" value= "Jdbc:mysql://localhost:3306/ds_0"/> <property name= "user" value= "root"/> <property name= "Password" value= "root"/> </bean> <bean id= "Ds_1" class= "com.mchange.v2 . c3p0. Combopooleddatasource "Destroy-method= "Close" > <property name= "driverclass" value= "Com.mysql.jdbc.Driver"/> <property nam E= "Jdbcurl" value= "Jdbc:mysql://localhost:3306/ds_1"/> <property name= "user" value= "root"/> <p Roperty name= "Password" value= "root"/> </bean> <bean id= "Datasourcerule" class= "COM.DANGDANG.DDFRAME.R Db.sharding.api.rule.DataSourceRule "> <constructor-arg> <map> <entry
        key= "Ds_0" value-ref= "Ds_0"/> <entry key= "ds_1" value-ref= "Ds_1"/> </map> </constructor-arg> </bean> <bean id= "Ordertablerule" class= "Com.dangdang.ddframe.rdb.sharding.api. Rule.
            Tablerule "> <constructor-arg value=" t_order "index=" 0 "/> <constructor-arg index=" 1 "> <list> <value>t_order_0</value> &LT;VALUE&GT;T_ORDER_1&LT;/VALUE&G
            T </list> </constructor-arg> <constructor-arg index= "2" ref= "Datasourcerule"/> &LT;/BEAN&G
	
	T <bean id= "Databaseshardingstrategy" class= " Com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy "> <constructor-arg index= "0" value= "user_id"/> <constructor-arg index= "1" > <bean class= "Com.ccy.algorithm.SingleKey Modulodatabaseshardingalgorithm "/> </constructor-arg> </bean> <bean id=" Tableshard Ingstrategy "class=" Com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy "> < Constructor-arg index= "0" value= "order_id"/> <constructor-arg index= "1" > <bean class= "com. Ccy.algorithm.SingleKeyModuloTableShardingAlgorithm "/> </constructor-arg> </bean> &L T;bean id= "Shardingrule" class= "Com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule" > <cOnstructor-arg index= "0" ref= "datasourcerule"/> <constructor-arg index= "1" > <list> <ref bean= "Ordertablerule"/> </list> </constructor-arg> <cons Tructor-arg index= "2" ref= "Databaseshardingstrategy"/> <constructor-arg index= "3" ref= "TableShardingStrategy "/> </bean> <!--sharding Data Source--<bean id=" Shardingdatasource "class=" Com.dangdang.ddframe . Rdb.sharding.api.ShardingDataSource "> <constructor-arg ref=" shardingrule "/> </bean> &lt ;! --4. Configure Sqlsessionfactory integrated MyBatis, automatically scan mapper files-<!--sqlsessionfactory--<bean id= "sqlsessionfact Ory "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <!--database connection pool-<property name=" dat Asource "ref=" Shardingdatasource "/> <!--load MyBatis Global profile--<property name=" Configlocat Ion "Value=" Classpath:Sqlmapconfig.xml "/> </bean> <!--mapper Scanner--<!--mapper batch scan, scan map from mapper package  
	    Per interface, the proxy object is created automatically and injected in the spring container follows specification: Mapper.java and Mapper.xml Map file names are consistent and in one directory. The ID of the mapper bean that is automatically scanned is the Mapper class name (first letter lowercase)--<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <!--Scan package path, if you need to scan multiple packages, use half-width comma-separated-<property name= "Basepackage" value= "Com.ccy.dao" > </property> <property name= "Sqlsessionfactorybeanname" value= "Sqlsessionfactory"/> </bean&  
	
	Gt <!--5. Configuring Transactions--<bean id= "Txmanager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <property name=" DataSource "ref=" Shardingdatasource "/> </bean> <tx:advice id=" Txadvice "transaction-manager=" Txmanager "> <tx:attri butes> <tx:method name= "insert*" propagation= "REQUIRED"/> <tx:method name= "update*" propagation= "REQUIRE
		D "/>	<tx:method name= "delete*" propagation= "REQUIRED"/> <tx:method name= "find*" read-only= "true"/> <t X:method name= "get*" read-only= "true"/> <tx:method name= "view*" read-only= "true"/> </tx:attributes> & lt;/tx:advice> <aop:config> <aop:pointcut expression= "Execution (* com.ccy.services.*.* (..))" Id= " 
				         
Txpointcut "/> <aop:advisor advice-ref=" Txadvice "pointcut-ref=" Txpointcut "/> </aop:config> </beans>

5. Use feelings

Spring Configuration and dynamic table configuration are not supported for the time being, and it is not very convenient to use, hoping that when the small partners to quickly achieve the development of future line planning


PS: More Database segmentation Knowledge link Direct: http://blog.csdn.net/column/details/sharding.html (Technical blog from Laurence)


More content please continue to follow my blog: Http://blog.csdn.net/caicongyang record and share, you and I grow together-from Caicongyang
If you feel good, please help click on the "Top" below.

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.