时间:2023-01-24 10:49:30 | 栏目:JAVA代码 | 点击:次
最近公司由于业务需要,对日益增加的数据量越来越无法容忍,遂作出分库分表的决定,考察了几个技术方案后,决定使用shardingsphere做分表中间件。
使用maven拉取jar包:
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc</artifactId>
<version>3.0.0.M3</version>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>3.0.0.M3</version>
</dependency>
分表配置:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sharding="http://shardingsphere.io/schema/shardingsphere/sharding"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://shardingsphere.io/schema/shardingsphere/sharding
http://shardingsphere.io/schema/shardingsphere/sharding/sharding.xsd"
default-autowire="byName">
<!-- 分表算法 -->
<bean id="tableShardingAlgorithm" class="pxf.commom.support.sharding.tableShardingAlgorithm" />
<sharding:complex-strategy id="tableStrategy"
sharding-columns="uid"
algorithm-ref="tableShardingAlgorithm" />
<!-- ds_0为数据源,如果做分库,可配置多个数据源;不分表的表不用在此做配置-->
<sharding:data-source id="dataSource">
<sharding:sharding-rule data-source-names="ds_0" default-data-source-name="ds_0" >
<sharding:table-rules>
<sharding:table-rule
logic-table="test_table"
actual-data-nodes="ds_0.test_table_$->{0..128}"
table-strategy-ref="tableStrategy" />
</sharding:table-rules>
</sharding:sharding-rule>
</sharding:data-source>
</beans>
1). 用于分表的列在sql中不能为空,所以像insert之类的语句需要做下非空判断;
2). sqlmap中LONGVARCHER字段不能使用,会报序列化异常,可改为VARCHAR类型;
3). union语法不支持,可改为OR查询(shardingjdbc连OR也不支持,所以建议使用shardingsphere)。