-
Notifications
You must be signed in to change notification settings - Fork 15
quick start
Kaiming Zou edited this page Jan 29, 2017
·
5 revisions
- This demo is based on spring bean.
##step0: add dependency
<dependency>
<groupId>org.hellojavaer.ddal</groupId>
<artifactId>ddal-ddr</artifactId>
<version>x.x.x</version>
</dependency>
<dependency>
<groupId>org.hellojavaer.ddal</groupId>
<artifactId>ddal-jsqlparser</artifactId>
<version>x.x.x</version>
</dependency>
<!-- to use annotation to route in spring environment -->
<dependency>
<groupId>org.hellojavaer.ddal</groupId>
<artifactId>ddal-spring-context</artifactId>
<version>x.x.x</version>
</dependency>
<!-- to use sequence service -->
<dependency>
<groupId>org.hellojavaer.ddal</groupId>
<artifactId>ddal-sequence</artifactId>
<version>x.x.x</version>
</dependency>
<!-- configuring route rule, which is optional-->
<bean id="idRule" class="org.hellojavaer.ddal.ddr.shard.simple.SimpleShardRouteRule">
<property name="scRoute" value="{#sdValue % 4}"></property>
<property name="scFormat" value="{scName}_{scRoute:'%2s'}"></property>
<property name="tbRoute" value="{#sdValue % 8}"></property>
<property name="tbFormat" value="{tbName}_{tbRoute:'%4s'}"></property>
</bean>
<bean id="nameRule" class="org.hellojavaer.ddal.ddr.shard.simple.SimpleShardRouteRule">
<property name="tbRoute" value="{#abs(#sdValue.hashCode()) % 4}"></property>
<property name="tbFormat" value="user_{tbRoute:'%4s'}"></property>
</bean>
<!-- binding route rule -->
<bean id="shardRouter" class="org.hellojavaer.ddal.ddr.shard.simple.SimpleShardRouter">
<property name="routeRuleBindings">
<list>
<bean class="org.hellojavaer.ddal.ddr.shard.simple.SimpleShardRouteRuleBinding">
<property name="scName" value="member"></property>
<property name="tbName" value="user"></property>
<property name="rule" ref="idRule"></property> <!--optional-->
<property name="sdKey" value="id"></property> <!--optional-->
<property name="sdValues" value="[0~127]"></property> <!--optional:can be used for security checking and generating a table sequence to iterate all tables -->
</bean>
<bean class="org.hellojavaer.ddal.ddr.shard.simple.SimpleShardRouteRuleBinding">
<property name="scName" value="member_name"></property>
<property name="tbName" value="user"></property>
<property name="rule" ref="nameRule"></property> <!--optional-->
<property name="sdKey" value="name"></property> <!--optional-->
<property name="sdValues" value="[0~127]"></property> <!--optional-->
</bean>
</list>
</property>
</bean>
<!-- associate route rule with sql -->
<bean id="shardParser" class="org.hellojavaer.ddal.ddr.shard.simple.SimpleShardParser">
<property name="shardRouter" ref="shardRouter"></property>
<property name="sqlParser">
<bean class="org.hellojavaer.ddal.ddr.sqlparse.cache.LRUSQLParserCache">
<property name="capacity" value="1000"/>
<property name="sqlParser">
<bean class="org.hellojavaer.ddal.jsqlparser.JSQLParser"></bean>
</property>
</bean>
</property>
</bean>
##step2: configure datasource manager
- single datasource
<bean id="dataSourceManager" class="org.hellojavaer.ddal.ddr.datasource.manager.SingleDataSourceManager">
<property name="dataSource" ref="ds00"></property>
</bean>
- multi-datasource
<bean id="dataSourceManager"
class="org.hellojavaer.ddal.ddr.datasource.manager.rw.DefaultReadWriteDataSourceManager">
<property name="readOnlyDataSourceMonitorServer">
<bean class="org.hellojavaer.ddal.ddr.datasource.manager.rw.monitor.mbean.MBeanReadOnlyDataSourceMonitorServer"></bean>
</property>
<property name="writeOnlyDataSources">
<list>
<bean class="org.hellojavaer.ddal.ddr.datasource.manager.rw.WriteOnlyDataSourceBinding">
<property name="scNames" value="member_0[0~1]"></property>
<property name="dataSource" ref="wDs00"></property>
</bean>
<bean class="org.hellojavaer.ddal.ddr.datasource.manager.rw.WriteOnlyDataSourceBinding">
<property name="scNames" value="member_0[2~3]"></property>
<property name="dataSource" ref="wDs01"></property>
</bean>
<bean class="org.hellojavaer.ddal.ddr.datasource.manager.rw.WriteOnlyDataSourceBinding">
<property name="scNames" value="member_name"></property>
<property name="dataSource" ref="wDs10"></property>
</bean>
</list>
</property>
<property name="readOnlyDataSources">
<list>
<bean class="org.hellojavaer.ddal.ddr.datasource.manager.rw.ReadOnlyDataSourceBinding">
<property name="scNames" value="member_0[0~1]"></property>
<property name="dataSources">
<list>
<bean class="org.hellojavaer.ddal.ddr.datasource.WeightedDataSource">
<property name="name" value="rDs00"></property>
<property name="weight" value="9"></property>
<property name="dataSource" ref="rDs00"></property>
</bean>
<bean class="org.hellojavaer.ddal.ddr.datasource.WeightedDataSource">
<property name="name" value="wDs00"></property>
<property name="weight" value="1"></property>
<property name="dataSource" ref="wDs00"></property>
</bean>
</list>
</property>
</bean>
<bean class="org.hellojavaer.ddal.ddr.datasource.manager.rw.ReadOnlyDataSourceBinding">
<property name="scNames" value="member_0[2~3]"></property>
<property name="dataSources">
<list>
<bean class="org.hellojavaer.ddal.ddr.datasource.WeightedDataSource">
<property name="name" value="rDs01"></property>
<property name="weight" value="9"></property>
<property name="dataSource" ref="rDs01"></property>
</bean>
<bean class="org.hellojavaer.ddal.ddr.datasource.WeightedDataSource">
<property name="name" value="wDs01"></property>
<property name="weight" value="1"></property>
<property name="dataSource" ref="wDs01"></property>
</bean>
</list>
</property>
</bean>
<bean class="org.hellojavaer.ddal.ddr.datasource.manager.rw.ReadOnlyDataSourceBinding">
<property name="scNames" value="member_name"></property>
<property name="dataSources">
<list>
<bean class="org.hellojavaer.ddal.ddr.datasource.WeightedDataSource">
<property name="name" value="rDs01"></property>
<property name="weight" value="9"></property>
<property name="dataSource" ref="rDs10"></property>
</bean>
<bean class="org.hellojavaer.ddal.ddr.datasource.WeightedDataSource">
<property name="name" value="wDs01"></property>
<property name="weight" value="1"></property>
<property name="dataSource" ref="wDs10"></property>
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
##step3: build DDRDataSource for external reference
<bean id="dataSource" class="org.hellojavaer.ddal.ddr.datasource.jdbc.DefaultDDRDataSource">
<property name="dataSourceManager" ref="dataSourceManager"></property>
<property name="shardParser" ref="shardParser"></property>
</bean>
##step4: use sequence service [optional]
<bean id="idGetter" class="org.hellojavaer.ddal.sequence.db.DatabaseIdGetter">
<property name="dataSource" ref="wDs00"/>
<property name="scName" value="member_00"/>
</bean>