分庫分表之噹噹開源中介軟體sharding-jdbc體驗

來源:互聯網
上載者:User
0.資料庫sharding基本思想 水平切分:把表的資料按某種規則(比如按ID散列)切分到多張表或者多個資料庫(server)上。 垂直切分:把關係緊密(比如同一模組)的表切分出來放在一個server上。 1.sharding-jdbc 噹噹開源中介軟體:https://github.com/dangdangdotcom/sharding-jdbc 注意:Sharding-jdbc目前實現的是水平切分,垂直切分配置暫未實現。

2.架構圖


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完整配置 設定檔基於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/spring-beans-3.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd         http://www.springframework.org/schema/aop          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 0.spring upload config --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      <property name="defaultEncoding" value="utf-8"></property>      <property name="maxUploadSize" value="10485760000"></property>      <property name="maxInMemorySize" value="10960"></property>  </bean>  <!-- 1.配置掃描包service,dao --><context:component-scan base-package="com.ccy.*"/><!-- 2.配置屬性檔案jdbc.properties --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 3.配置資料來源dataSource --><bean id="ds_0"  class="com.mchange.v2.c3p0.ComboPooledDataSource" 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 name="jdbcUrl" value="jdbc:mysql://localhost:3306/ds_1"/>        <property name="user" value="root"/>        <property name="password" value="root"/>    </bean> <bean id="dataSourceRule" class="com.dangdang.ddframe.rdb.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>                <value>t_order_1</value>            </list>        </constructor-arg>        <constructor-arg index="2" ref="dataSourceRule"/>    </bean><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.SingleKeyModuloDatabaseShardingAlgorithm" />        </constructor-arg>    </bean>        <bean id="tableShardingStrategy" 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>         <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>        <constructor-arg index="2" ref="databaseShardingStrategy"/>        <constructor-arg index="3" ref="tableShardingStrategy"/>    </bean>      <!--sharding資料來源-->     <bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource">         <constructor-arg ref="shardingRule"/>     </bean><!-- 4.配置SqlSessionFactory 整合mybatis,自動掃描mapper檔案 --><!-- sqlSessionFactory -->      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">          <!-- 資料庫連接池 -->          <property name="dataSource" ref="shardingDataSource" />          <!-- 載入mybatis的全域設定檔 -->          <property name="configLocation" value="classpath:sqlMapConfig.xml" />      </bean>            <!-- mapper掃描器 -->      <!-- mapper批量掃描,從mapper包中掃描出mapper介面,自動建立代理對象並且在spring容器中注入       遵循規範:將mapper.java和mapper.xml對應檔名稱保持一致,且在一個目錄中.      自動掃描出來的mapper的bean的id為mapper類名(首字母小寫)-->      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">          <!-- 掃描包路徑,如果需要掃描多個包,中間使用半形逗號隔開 -->          <property name="basePackage" value="com.ccy.dao"></property>          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />      </bean>  <!-- 5.配置事務 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="shardingDataSource"/></bean> <tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="find*" read-only="true"/><tx:method name="get*" read-only="true"/><tx:method name="view*" read-only="true"/></tx:attributes></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.使用感受

暫時不支援spring配置和動態表配置,使用起來確實不太方便,希望噹噹的小夥伴們趕緊實現制定的未來線路規劃


ps:更多資料庫切分知識連結直達:http://blog.csdn.net/column/details/sharding.html (來自Laurence的技術部落格)


更多精彩內容請繼續關注我的部落格:http://blog.csdn.net/caicongyang 記錄與分享,你我共成長 -from  caicongyang
如果你覺得不錯,請幫忙點擊下面的《頂》。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.