数据源bean,ibatis核心beans,spring事务bean
配置项说明
cuntaosccDataSource:分库分表数据源,appName数据库代号从dba获得,appRuleFile分库分表配置文件,useLocalConfig使用本地配置。
sqlMapClientPartition和sqlMapClientTemplatePartition:创建SqlMapClientTemplate,该类为ibatis执行sqlmap语句的核心实现类。
其中:configLocations是ibatis的sql配置总文件。
transactionManagerPartition是spring的事务控制器,tx:advice标志adviser,aop:pointcut中是切口。
beans-persisstence.xml
<?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: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.xsd![]()
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd![]()
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
"
default-autowire="byName">
<bean id="cuntaosccDataSource" class="com.taobao.tddl.client.jdbc.TDataSource" init-method="init">
<property name="appName" value="CUNTAO_SUPPLIER_APP"/>
<property name="appRuleFile" value="classpath:/com/taobao/cun/scc/biz/spring/beans-tddl-rule.xml"/>
<property name="useLocalConfig" value="true"/>
</bean>
<bean id="sqlMapClientPartition" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocations">
<list>
<value>classpath:/com/taobao/cun/scc/biz/sqlmap/sqlmap-config.xml</value>
</list>
</property>
<property name="dataSource" ref="cuntaosccDataSource" />
</bean>
<bean id="sqlMapClientTemplatePartition" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClientPartition"/>
</property>
<property name="dataSource" ref="cuntaosccDataSource" />
</bean>
<bean id="transactionManagerPartition"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="cuntaosccDataSource"/>
</bean>
<tx:advice id="txAdvicePartition" transaction-manager="transactionManagerPartition">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allServiceMethod"
expression="( (execution(* com.taobao.cun.scc.biz..AoImpl.(..))) or (execution(* com.taobao.cun.scc.biz..BoImpl.(..))) ) and not (execution (* com.taobao.cun..QueryBoImpl.(..))) and not (execution (* com.taobao.cun..QueryBOImpl.(..))) and not (execution (* com.taobao.cun..StationBuyerBuyerBoImpl.(..))) and not (execution (* com.taobao.cun..BoImpl.get(..))) "/>
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvicePartition"/>
</aop:config>
</beans>
TDDL分库分表配置bean
配置项说明
分表总配置VirtualTableRoot:
其中,defaultDbIndex是appgroup,从DBA获取
dbType,数据库类型
tableRules引入各个表的配置
分库分表配置TableRule:
其中,dbNamePattern分库规则,大括号会替换成dbRuleArray中的值
dbRuleArray,分库计算方法,(#supplier_id,1,256#.longValue() % 10000 % 256).intdiv(32)表示以supplier_id为分表键,256张表,8个库所以除以32,我们计算规则取后四位计算所以先模10000.
tbNamePattern分表规则,大括号会替换成tbRuleArray中的值。
dbRuleArray,分表计算方法,#supplier_id,1,256#.longValue() % 10000 % 256 就是分库计算方法不除以32.
beans-tddl-rule.xml
<?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: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-2.0.xsd![]()
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd![]()
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
">
<bean id="vtabroot" class="com.taobao.tddl.interact.rule.VirtualTableRoot" init-method="init">
<property name="defaultDbIndex" value="CUNTAO_SUPPLIER_GROUP"/>
<property name="dbType" value="MYSQL"/>
<property name="tableRules">
<map>
<entry key="cuntao_purchase_order" value-ref="cuntao_purchase_order_bean"/>
<entry key="cuntao_sub_purchase_order" value-ref="cuntao_sub_purchase_order_bean"/>
<entry key="cuntao_logistics_order" value-ref="cuntao_logistics_order_bean"/>
<entry key="cuntao_sub_logistics_order" value-ref="cuntao_sub_logistics_order_bean"/>
</map>
</property>
</bean>
<bean id="cuntao_purchase_order_bean" class="com.taobao.tddl.interact.rule.TableRule">
<property name="dbNamePattern" value="CUNTAO_SUPPLIER_BODY{00}_GROUP"/>
<property name="dbRuleArray">
<value>(#supplier_id,1,256#.longValue() % 10000 % 256).intdiv(32)</value>
</property>
<property name="tbNamePattern" value="cuntao_purchase_order_{0000}" />
<property name="tbRuleArray">
<value>#supplier_id,1,256#.longValue() % 10000 % 256</value>
</property>
</bean>
</beans>
sequence生成bean
我们用的sequence是一个单库单表的数据库,即每次使用GroupSequence获取新的seq值来计算id,保证全局各个表不会发生id冲突。
appName,从DBA获得。
dbGroupKeys,所在库名称
tableName,sequence表名,可以使用一个sequence表来计算多个表的id,因为每个表实际只占用一行数据。
name,目标表在sequence表中的key,一般取表名,其实是任意的。
beans-tddl-sequence.xml
<?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: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-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
">
<!-- sequence生成器的数据源与属性的配置,以及生成可用bean -->
<bean id="sequenceDao" class="com.taobao.tddl.client.sequence.impl.GroupSequenceDao" init-method="init">
<!-- appName,必填 -->
<property name="appName" value="CUNTAO_SUPPLIER_APP" />
<!-- 数据源的个数 -->
<property name="dscount" value="1" />
<!-- dbGroupKeys 必填 -->
<!-- 如果在末尾插入"-OFF",该源将被关掉,该源占据的SQL段会被保留" -->
<!-- 当dbGroupKeys中配置的个数小于dbcount的值的时候,默认配置了"-OFF"的源 -->
<property name="dbGroupKeys">
<list>
<value>CUNTAO_SUPPLIER_BODY00_GROUP</value>
</list>
</property>
<!-- 内步长 ,默认为1000,取值在1-100000之间 -->
<property name="innerStep" value="500" />
<!-- 重试次数,在多个groupDataSource的场景下,建议设置成1-2次。默认为2次 -->
<property name="retryTimes" value="2" />
<!-- sequence表的表名 -->
<property name="tableName" value="cuntao_supplier_sequence" />
<!-- 自适应开关 ,默认为false -->
<property name="adjust" value="true" />
</bean>
<bean id="cuntaoPurchaseOrderSequence" class="com.taobao.tddl.client.sequence.impl.GroupSequence" init-method="init">
<property name="sequenceDao" ref="sequenceDao"/>
<property name="name" value="cuntao_purchase_order_seq"/>
</bean>
</bean>
</beans>
本文详细介绍了TDDL(淘宝分库分表中间件)的配置方法,包括数据源bean(cuntaosccDataSource)、分库分表配置bean以及sequence生成bean。数据源配置涉及appName、appRuleFile和useLocalConfig;分库分表配置涉及到VirtualTableRoot、TableRule,如cuntao_purchase_order_bean,以及分库规则和分表规则;sequence生成bean中,通过GroupSequenceDao和GroupSequence确保全局序列号不冲突。
856

被折叠的 条评论
为什么被折叠?



