Sharding-Sphere 3.X 与spring与mybatis集成(分库分表)demo

版权声明:欢迎查看,随便复制! https://blog.csdn.net/hunhun1122/article/details/87369234

最近在弄这个sharding-sphere,公司内部分库分表是在此业务代码上进行逻辑分库分表,但是这种总是不好,也调研了几款分库分表中间件、mycat、网易cetus、阿里DRDS、这几种就是背景强大,大公司经过大量的实战,成熟度很高,而框架sharding-sphere比较轻量级,最近比较火,它是以jar包形式提供服务,可以无缝连接ORM框架,并不需要额外的部署,不需要依赖,运维可以不需要改动,很多人都把sharding-sphere当成增强版的jdbc驱动,迁移代码其实没那么复杂。对于巨头公司,公司内部都会有自己研发的组件,中间件,来供整个公司使用,对于中小型公司,需要使用开源的中间件来支撑公司业务发展。

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/hunhun1122/shirdingjdbc-spring

启动步骤:
1、需要一个tomcat
2、启动成功后直接访问http://ip:port/SSM/demo/test 
3、访问类是在ShardingDemoController这个类里

猜你喜欢

转载自blog.csdn.net/hunhun1122/article/details/87369234
今日推荐