Sharding-Sphere 3.X 與spring與mybatis集成(分庫分表)demo
阿新 • • 發佈:2018-05-28
sharding jdbc 分庫分表最近在弄這個sharding-sphere,公司內部分庫分表是在此業務代碼上進行邏輯分庫分表,但是這種總是不好,也調研了幾款分庫分表中間件、mycat、網易cetus、阿裏DRDS、這幾種就是背景強大,大公司經過大量的實戰,成熟度很高,而框架sharding-sphere比較輕量級,最近比較火,它是以jar包形式提供服務,可以無縫連接ORM框架,並不需要額外的部署,不需要依賴,運維可以不需要改動,很多人都把sharding-sphere當成增強版的jdbc驅動,遷移代碼其實沒那麽復雜。對於巨頭公司,公司內部都會有自己研發的組件,中間件,來供整個公司使用,對於中小型公司,需要使用開源的中間件來支撐公司業務發展。
sharding-sphere-examples網址:
https://github.com/growup818/spring-learning-examples
sharding-sphere文檔:
http://shardingsphere.io/document/cn/features/
個人建議,還是以官方文檔為主,官方文檔例子都很全,文檔也很清楚,運行也都沒問題,不要去搜網上的demo。但是有一點建議一下,可以看著官網的examples,根據公司的代碼框架寫個簡單的demo什麽的,可以運行。
sql腳本:
/* Navicat MySQL Data Transfer Source Server : 10.1.90.25-dev Source Server Version : 50720 Source Host : 10.1.90.25:3307 Source Database : separate_entity_1 Target Server Type : MYSQL Target Server Version : 50720 File Encoding : 65001 Date: 2018-05-28 18:04:42 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_order_0 -- ---------------------------- DROP TABLE IF EXISTS `t_order_0`; CREATE TABLE `t_order_0` ( `order_id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `status` varchar(50) DEFAULT NULL, PRIMARY KEY (`order_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_order_1 -- ---------------------------- DROP TABLE IF EXISTS `t_order_1`; CREATE TABLE `t_order_1` ( `order_id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `status` varchar(50) DEFAULT NULL, PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_order_item_0 -- ---------------------------- DROP TABLE IF EXISTS `t_order_item_0`; CREATE TABLE `t_order_item_0` ( `order_item_id` bigint(20) NOT NULL , `order_id` bigint(20) DEFAULT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`order_item_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_order_item_1 -- ---------------------------- DROP TABLE IF EXISTS `t_order_item_1`; CREATE TABLE `t_order_item_1` ( `order_item_id` bigint(20) NOT NULL, `order_id` bigint(20) DEFAULT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`order_item_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
database.properties 配置文件
sharding.jdbc.datasource.names=separate_entity_0,separate_entity_1 sharding.jdbc.datasource.separate_entity_0.url=jdbc:mysql://127.0.0.1:3307/separate_entity_0 sharding.jdbc.datasource.separate_entity_0.username=root sharding.jdbc.datasource.separate_entity_0.password= sharding.jdbc.datasource.separate_entity_1.url=jdbc:mysql://127.0.0.1:3307/separate_entity_1 sharding.jdbc.datasource.separate_entity_1.username=root sharding.jdbc.datasource.separate_entity_1.password= sharding.jdbc.datasource.actual.data.nodes.order=separate_entity_$->{0..1}.t_order_$->{0..1} sharding.jdbc.datasource.actual.data.nodes.orderitem=separate_entity_$->{0..1}.t_order_item_$->{0..1}
``
sharding-databases.xml 配置文件:
<!-- 可能不同的業務表有不同的業務規則,會出現好多個分庫,分表策略-->
<bean id="preciseModuloDatabaseShardingAlgorithm" class="com.sharding.demo.algorithm.DatabaseShardingAlgorithm" />
<bean id="preciseModuloTableShardingAlgorithm" class="com.sharding.demo.algorithm.TableShardingAlgorithm" />
<!-- 可能不同的業務表有不同的業務規則,會出現好多個分庫,分表策略,這塊指定了-->
<sharding:standard-strategy id="databaseShardingStrategy" sharding-column="user_id" precise-algorithm-ref="preciseModuloDatabaseShardingAlgorithm" />
<sharding:standard-strategy id="tableShardingStrategy" sharding-column="order_id" precise-algorithm-ref="preciseModuloTableShardingAlgorithm" />
<sharding:data-source id="shardingDataSource">
<sharding:sharding-rule data-source-names="separate_entity_0,separate_entity_1">
<sharding:table-rules>
<sharding:table-rule logic-table="t_order"
actual-data-nodes="${sharding.jdbc.datasource.actual.data.nodes.order}"
database-strategy-ref="databaseShardingStrategy" table-strategy-ref="tableShardingStrategy"
generate-key-column-name="order_id" />
<sharding:table-rule logic-table="t_order_item"
actual-data-nodes="${sharding.jdbc.datasource.actual.data.nodes.orderitem}"
database-strategy-ref="databaseShardingStrategy"
table-strategy-ref="tableShardingStrategy"
generate-key-column-name="order_item_id" />
</sharding:table-rules>
</sharding:sharding-rule>
</sharding:data-source>
分庫分表:
public final class DatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {
@Override
public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}
}
public final class TableShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
@Override
public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Long> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}
}
具體的demo在githup網址上:
demo:https://github.com/growup818/spring-learning-examples
啟動步驟:
1、需要一個tomcat
2、啟動成功後直接訪問http://ip:port/SSM/demo/test
3、訪問類是在ShardingDemoController這個類裏
Sharding-Sphere 3.X 與spring與mybatis集成(分庫分表)demo