1. 程式人生 > >sharding-JDBC 實現分庫

sharding-JDBC 實現分庫

names core pub doc odata 需求 object blog lib

需求

按照業務線Id分庫,每個業務線一個庫。

sql

-- 建立112業務線庫
create database sharding_112 ;

-- 在112業務庫裏建立 t_order表
CREATE TABLE `t_order` (
  `order_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `business_id` int(4) DEFAULT NULL,
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

-- 建立113業務線庫
create database sharding_113 ;

-- 在113業務庫裏建立 t_order表
CREATE TABLE `t_order` (
  `order_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `business_id` int(4) DEFAULT NULL,
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

pom.xml(使用sharding-jdbc 2.0)

<!-- https://mvnrepository.com/artifact/io.shardingjdbc/sharding-jdbc-core -->
<dependency>
    <groupId>io.shardingjdbc</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>2.0.0.M2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.dangdang/sharding-jdbc-config-spring -->
<dependency> <groupId>com.dangdang</groupId> <artifactId>sharding-jdbc-config-spring</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>io.shardingjdbc</groupId> <artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>2.0.0.M2</version> </dependency>

Spring相關配置

<bean id="ds_112" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
      destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="${jdbc.url_112}"></property>
    <property name="username" value="${jdbc.username_112}"></property>
    <property name="password" value="${jdbc.password_112}"></property>
    <property name="maxActive" value="100"/>
    <property name="initialSize" value="10"/>
    <property name="maxWait" value="60000"/>
    <property name="minIdle" value="5"/>
</bean>

<bean id="ds_113" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
      destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="${jdbc.url_113}"></property>
    <property name="username" value="${jdbc.username_113}"></property>
    <property name="password" value="${jdbc.password_113}"></property>
    <property name="maxActive" value="100"/>
    <property name="initialSize" value="10"/>
    <property name="maxWait" value="60000"/>
    <property name="minIdle" value="5"/>
</bean>

<sharding:standard-strategy id="databaseShardingStrategy" sharding-column="business_id" precise-algorithm-class="com.boothsun.util.sharding.PreciseModuloDatabaseShardingAlgorithm" />

<sharding:data-source id="shardingDataSource">
    <sharding:sharding-rule data-source-names="ds_112,ds_113">
        <sharding:table-rules>
            <sharding:table-rule logic-table="t_order" database-strategy-ref="databaseShardingStrategy"  />
        </sharding:table-rules>
    </sharding:sharding-rule>
</sharding:data-source>

sharding-jdbc 相關標簽含義 參見官方文檔:配置手冊

官方demo:github

分庫規則類

/**
 * 精確匹配
 */
public final class PreciseModuloDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {

    private static  final Map<Integer,String> dataSourceMap = new HashMap<>();

    static {
        dataSourceMap.put(112,"ds_112");
        dataSourceMap.put(113,"ds_113");
    }

    @Override
    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {
        return dataSourceMap.get(shardingValue.getValue());
    }
}

單測類

@Autowired
OrderMapper orderMapper ;

/**
 * 測試插入
 * @throws Exception
 */
@Test
public void insertSelective() throws Exception {

    Order order = new Order();
    order.setOrderId(1231);
    order.setUserId(222);
    order.setBusinessId(113);

    Boolean result = orderMapper.insert(order) > 0;

    System.out.println(result?"插入成功":"插入失敗");
}

/**
 * 測試 in 的查詢操作
 * @throws Exception
 */
@Test
public void selectByExample2() throws Exception {

    List<Integer> values = new ArrayList<>();
    values.add(112);
    values.add(113);

    OrderExample example = new OrderExample() ;
    example.createCriteria().andBusinessIdIn(values);

    List<Order> orderList = orderMapper.selectByExample(example) ;

    System.out.println(JSONObject.toJSONString(orderList));
}

/**
 * 測試between的查詢操作
 * @throws Exception
 */
@Test
public void selectByExample3() throws Exception {

    OrderExample example = new OrderExample() ;
    example.createCriteria().andBusinessIdBetween(112,113);

    List<Order> orderList = orderMapper.selectByExample(example) ;

    System.out.println(JSONObject.toJSONString(orderList));
}

sharding-JDBC 實現分庫