Skip to content
mengshi edited this page Jan 28, 2014 · 4 revisions

 

Maven依赖

 

    <dependency>
        <groupId>com.taobao.tddl</groupId>
        <artifactId>tddl-matrix</artifactId>
        <version>5.0.0-SNAPSHOT</version>
    </dependency>

 

远程配置模式

例程

 

    //初始化DataSource,只需要一个appname,我们会帮您配好
    TDataSource ds = new TDataSource();
    ds.setAppName("app_name");
    ds.init();
//其余的just jdbc
Connection con = null;
con = us.getConnection();

PreparedStatement ps = con.prepareStatement("select * from auction_auctions limit 10");

ResultSet rs = ps.executeQuery();

while (rs.next()) {
    System.out.println(rs.getObject(1));
    System.out.println(rs.getObject(2));
    System.out.println(rs.getString(3));
    System.out.println(rs.getObject(4));
}</pre>

 

Spring数据源

 

    <bean id="ds" class="com.taobao.tddl.matrix.jdbc.TDataSource" init-method="init">
        <property name="appName" value="app_name" />
    </bean>

 

本地配置模式

 拓扑文件

    topology.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<matrix name="test_matrix" xmlns="https://github.com/tddl/tddl/schema/matrix"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://github.com/tddl/tddl/schema/matrix https://raw.github.com/tddl/tddl/master/tddl-common/src/main/resources/META-INF/matrix.xsd">
&lt;group name="andor_show_group0" type="MYSQL_JDBC"&gt;
	&lt;properties&gt;
		&lt;property name="com.taobao.tddl.jdbc.group_V2.4.1_andor_show_group0"&gt;
			&lt;value&gt;andor_show_group0_atom0:r10w10p0&lt;/value&gt;
		&lt;/property&gt;
	&lt;/properties&gt;

	&lt;atom name="andor_show_group0_atom0"&gt;
		&lt;properties&gt;
			&lt;property name="com.taobao.tddl.atom.global.andor_show_group0_atom0"&gt;
				&lt;value&gt;
				ip=10.232.31.21
				port=3306
				dbName=andor_show
				dbType=mysql
				dbStatus=RW
			&lt;/value&gt;
			&lt;/property&gt;
			&lt;property name="com.taobao.tddl.atom.app.andor_show.andor_show_group0_atom0"&gt;
				&lt;value&gt;
				userName=andor
				minPoolSize=20
				maxPoolSize=40
				idleTimeout=20
				blockingTimeout=10000
				preparedStatementCacheSize=1000
				connectionProperties=characterEncoding=gbk;autoReconnect=true
				readRestrictTimes=0
				writeRestrictTimes=0
				timeSliceInMillis=0
			&lt;/value&gt;
			&lt;/property&gt;

			&lt;property name="com.taobao.tddl.atom.passwd.andor_show.mysql.andor"&gt;
				&lt;value&gt;encPasswd=andor&lt;/value&gt;
			&lt;/property&gt;
		&lt;/properties&gt;
	&lt;/atom&gt;

	&lt;atom name=""&gt;
	&lt;/atom&gt;

	&lt;atom name=""&gt;
	&lt;/atom&gt;
&lt;/group&gt;


&lt;group name="andor_show_group1" type="MYSQL_JDBC"&gt;
&lt;/group&gt;

&lt;group name="ob_group" type="OCEANBASE_JDBC"&gt;
	&lt;properties&gt;
		&lt;property name="CONFIGURL"&gt;
			&lt;value&gt;http://obconsole.test.alibaba-inc.com/ob-config/config.co?dataId=wireless_message&lt;/value&gt;
		&lt;/property&gt;
	&lt;/properties&gt;
&lt;/group&gt;

</matrix>

 

规则文件:

rule.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="vtabroot" class="com.taobao.tddl.interact.rule.VirtualTableRoot"
		init-method="init">
		<property name="dbType" value="MYSQL" />
		<property name="defaultDbIndex" value="ob_group" />
		<property name="tableRules">
			<map>
				<entry key="auction_auctions._auction_id" value-ref="auction_auctions._auction_id" />
				<entry key="bmw_users._id" value-ref="bmw_users._id" />
				<entry key="buyer_tc_biz_order._biz_order_id" value-ref="buyer_tc_biz_order._biz_order_id" />
				<entry key="seller_tc_biz_order._biz_order_id" value-ref="seller_tc_biz_order._biz_order_id" />
				<entry key="sqls._id" value-ref="sqls._id" />
				<entry key="testtable._id" value-ref="testtable._id" />
				<entry key="testtable2._id" value-ref="testtable2._id" />
				<entry key="bmw_users" value-ref="bmw_users._id" />
				<entry key="xsl" value-ref="xsl._id" />
			</map>
		</property>
	</bean>
&lt;bean id="testtable._id" class="com.taobao.tddl.interact.rule.TableRule"&gt;
	&lt;property name="allowFullTableScan" value="true" /&gt;
	&lt;property name="dbNamePattern" value="andor_show_group0" /&gt;
	&lt;property name="tbRules" value="Math.abs(Long.valueOf(#id,1,4#)) % 4" /&gt;
	&lt;property name="tbNamePattern"
		value="testtable_{0000}" /&gt;
&lt;/bean&gt;

&lt;bean id="testtable2._id" class="com.taobao.tddl.interact.rule.TableRule"&gt;
	&lt;property name="allowFullTableScan" value="true" /&gt;
	&lt;property name="dbNamePattern" value="andor_show_group0" /&gt;
	&lt;property name="tbRules" value="Math.abs(Long.valueOf(#id,1,4#)) % 4" /&gt;
	&lt;property name="tbNamePattern"
		value="testtable2_{0000}" /&gt;
&lt;/bean&gt;
&lt;bean id="auction_auctions._auction_id" class="com.taobao.tddl.interact.rule.TableRule"&gt;
	&lt;property name="allowFullTableScan" value="true" /&gt;
	&lt;property name="dbNamePattern" value="andor_show_group0" /&gt;
	&lt;property name="tbRules" value="Math.abs(Long.valueOf(#auction_id,1,4#)) % 4" /&gt;
	&lt;property name="tbNamePattern"
		value="auction_auctions_{0000}" /&gt;
&lt;/bean&gt;

&lt;bean id="bmw_users._id" class="com.taobao.tddl.interact.rule.TableRule"&gt;
	&lt;property name="allowFullTableScan" value="true" /&gt;
	&lt;property name="dbNamePattern" value="andor_show_group{0}" /&gt;
	&lt;property name="dbRules" value="Math.abs(Long.valueOf(#id,1,1#)) % 2" /&gt;
	&lt;property name="tbRules" value="Math.abs(Long.valueOf(#id,1,4#)) % 4" /&gt;
	&lt;property name="tbNamePattern"
		value="bmw_users_{0000}" /&gt;
&lt;/bean&gt;

&lt;bean id="buyer_tc_biz_order._biz_order_id" class="com.taobao.tddl.interact.rule.TableRule"&gt;
	&lt;property name="allowFullTableScan" value="true" /&gt;
	&lt;property name="dbNamePattern" value="andor_show_group0" /&gt;
	&lt;property name="tbRules" value="Math.abs(Long.valueOf(#buyer_id,1,4#)) % 4" /&gt;
	&lt;property name="tbNamePattern"
		value="buyer_tc_biz_order_{0000}" /&gt;
&lt;/bean&gt;

&lt;bean id="seller_tc_biz_order._biz_order_id" class="com.taobao.tddl.interact.rule.TableRule"&gt;
	&lt;property name="allowFullTableScan" value="true" /&gt;
	&lt;property name="dbNamePattern" value="andor_show_group0" /&gt;
	&lt;property name="tbRules" value="Math.abs(Long.valueOf(#seller_id,1,4#)) % 4" /&gt;
	&lt;property name="tbNamePattern"
		value="seller_tc_biz_order_{0000}" /&gt;
&lt;/bean&gt;

&lt;bean id="sqls._id" class="com.taobao.tddl.interact.rule.TableRule"&gt;
	&lt;property name="allowFullTableScan" value="true" /&gt;
	&lt;property name="dbNamePattern" value="andor_show_group0" /&gt;
	&lt;property name="tbNamePattern"
		value="sqls" /&gt;
&lt;/bean&gt;

	&lt;bean id="xsl._id" class="com.taobao.tddl.interact.rule.TableRule"&gt;
	&lt;property name="allowFullTableScan" value="true" /&gt;
	&lt;property name="dbNamePattern" value="ob_group" /&gt;
	&lt;property name="tbNamePattern"
		value="xsl" /&gt;
&lt;/bean&gt;

</beans>

 

 

 

Clone this wiki locally