Skip to content
Allen edited this page Jul 9, 2017 · 9 revisions

Welcome to the ddal wiki!

Usage scenarios

The simplest configuration

this configuration will not trigger any shard routing or datasource routing.

<bean id="dataSourceManager" class="org.hellojavaer.ddal.ddr.datasource.manager.SingleDataSourceManager">
    <property name="dataSource" ref="wDs00"></property>
</bean>
<bean id="dataSource" class="org.hellojavaer.ddal.ddr.datasource.jdbc.DefaultDDRDataSource">
    <property name="dataSourceManager" ref="dataSourceManager"></property>
</bean>

Using sql parameter for routing

select * from user where id = 506
// After converted ===>
SELECT * FROM db_02.user_0122 AS user WHERE id = 506

Using ShardRouteContext for routing

if you need to fully scan all shard tables, you can set a route information in ShardRouteContext to visit the appointed shard table.

ShardRouteContext.setRouteInfo("scName", "user", 1); // set sdValue for schema-table
ShardRouteContext.setRouteInfo("scName", "user", new RouteInfo("scName_00", "user_0001")); // direct set route result
ShardRouteContext.setDefaultRouteInfo("scName", 2); // set default sdValue for a schema

Using annotation for routing

You can use annotation of ShardRoute for shard routing. ShardRoute is actually implement this by invocation ShardRouteContext.setDefaultRouteInfo(). To provide ShardRoute is just to make using of easier.

@ShardRoute(scName = "schemaName", sdValue = "{#$0}")
public InventoryDTO getById(Long sdValue, Long id) {
    return inventoryDao.getById(id);
}

Other key points

1.full scan all tables

1. set sdValues for the appointed schema-table
<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>
    <property name="sdKey" value="id"></property>
    <property name="sdValues" value="[0~127]"></property>
</bean>
....

2. iterator all route information
List<RouteInfo> routeInfos = shardRouter.getRouteInfos(scName, tbName);
for (RouteInfo routeInfo : routeInfos) {
    ShardRouteContext.setRouteInfo(scName, tbName, routeInfo);
    // TODO: do your business here
    // ...
    ShardRouteContext.clearContext();
}

2.Get log information

<logger name="org.hellojavaer.ddr.sql" additivity="false">
    <priority value="trace"/>
    <appender-ref ref="console"/>
</logger>
<logger name="org.hellojavaer.ddr.ds" additivity="false">
    <priority value="trace"/>
    <appender-ref ref="console"/>
</logger>

3.Dynamic adjust load balance for datasources

1. config 'DataSourceMonitorServer' for 'DefaultReadWriteDataSourceManager'
<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>
    .....
</bean>

2.launch jconsole to control load balance