分库分表之当当开源中间件sharding-jdbc体验

a 夏天 提交于 2019-12-03 12:30:32

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

如果你觉得不错,请帮忙点击下面的《顶》!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!