Skip to content

quick start

Allen edited this page Jul 21, 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</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>

step1: configure routing

<!-- configuring route rule, which is optional-->
<bean id="idRule" class="org.hellojavaer.ddal.ddr.shard.simple.SpelShardRouteRule">
    <property name="scRouteRule" value="{#scName}_{#format('%02d', #sdValue % 4)}"></property>
    <property name="tbRouteRule" value="{#tbName}_{#format('%04d', #sdValue % 8)}"></property>
</bean>
<bean id="nameRule" class="org.hellojavaer.ddal.ddr.shard.simple.SpelShardRouteRule">
    <property name="tbRouteRule" value="user_{#format('%04d', #abs(#sdValue.hashCode()) % 4)}"></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.DatabaseIdRangeGetter">
    <property name="dataSource" ref="wDs00"/>
    <property name="scName" value="member_00"/>
</bean>
Clone this wiki locally