SpringBoot集成Sharding-jdbc实现分库分表

一、Sharding-jdbc介绍

1、简介

Sharding-jdbc是当当网开源的一款客户端代理中间件。Sharding-jdbc包含分库分片和读写分离功能。对应用的代码没有侵入型,几乎没有任何改动,兼容主流orm框架,主流数据库连接池。目前属于Apache的孵化项目ShardingSphere。 

Sharding-jdbc定位为轻量级Java框架,在Java的JDBC层提供的额外服务。 它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。
适用于任何基于JDBC的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。
支持任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, Druid, HikariCP等。
支持任意实现JDBC规范的数据库。目前支持MySQL,Oracle,SQLServer,PostgreSQL以及任何遵循SQL92标准的数据库。

2、方案架构

二、Sharding-jdbc分库分表

1、创建两个数据库land1、land2,分别包含t_user0,t_user1两张表

DROP TABLE IF EXISTS `t_user0`;
CREATE TABLE `t_user0`  (
  `id` bigint(20) NOT NULL,
  `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名称',
  `city_id` int(12) NULL DEFAULT NULL COMMENT '城市',
  `sex` tinyint(1) NULL DEFAULT NULL COMMENT '性别',
  `phone` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '电话',
  `email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
  `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
  `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8;

DROP TABLE IF EXISTS `t_user0`;
CREATE TABLE `t_user0`  (
  `id` bigint(20) NOT NULL,
  `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名称',
  `city_id` int(12) NULL DEFAULT NULL COMMENT '城市',
  `sex` tinyint(1) NULL DEFAULT NULL COMMENT '性别',
  `phone` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '电话',
  `email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
  `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
  `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8;

2、Springboot项目添加Maven依赖,Mybatisplus、Druid、Sharding-jdbc依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.2</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.6</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.48</version>
</dependency>

<!-- sharding-sphere -->
<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-namespace</artifactId>
    <version>3.1.0</version>
</dependency>

3、在application.xml中添加sharding-jdbc的分库分表配置

server:
  port: 8800

spring:
  application:
    name: service-hi
  main:
    allow-bean-definition-overriding: true   #允许覆盖注册

eureka:
  instance:
    prefer-ip-address: true   #开启显示IP地址
    instance-id: ${spring.cloud.client.ip-address}:${server.port}   #eureka页面显示IP地址:端口号
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

sharding:
  jdbc:
    datasource:
      names: ds0,ds1  #配置两个数据源
      ds0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/land1?characterEncoding=utf-8&useUnicode=true&useSSL=true
        username: root
        password: root
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/land2?characterEncoding=utf-8&useUnicode=true&useSSL=true
        username: root
        password: root
config:
sharding:
default-data-source-name: ds0 # 未配置分片规则的表将通过默认数据源定位
default-database-strategy: # 默认数据库分片策略,同分库策略
inline:
sharding-column: city_id
algorithm-expression: ds${city_id % 2}
default-table-strategy: # 默认表分片策略,同分表策略
inline:
shardingColumn: sex
algorithm-expression: t_user${sex % 2}
tables:
t_user: #t_user表
key-generator-column-name: id #主键
actual-data-nodes: ds${0..1}.t_user${0..1} #数据节点
database-strategy: #分库策略
inline:
sharding-column: city_id
algorithm-expression: ds${city_id % 2}
table-strategy: #分表策略
inline:
shardingColumn: sex
algorithm-expression: t_user${sex % 2}
t_address: #t_address表
key-generator-column-name: id
actual-data-nodes: ds${0..1}.t_address
database-strategy:
inline:
shardingColumn: lit
algorithm-expression: ds${lit % 2}
props:
sql.show: true #是否显示sharding-jdbc的sql路由
mybatis-plus: 
  type-aliases-package: com.landcode.service.hi.model
  mapper-locations: classpath:mapper/*.xml

4、注意在mapper.xml sql配置文件中,表名使用user,而不是user0,user1。sharding-jdbc会根据配置的规则自动在对应的表执行sql

  <select id="selectUserList" resultType="com.landcode.service.hi.model.User">
    select
    <include refid="Base_Column_List" />
    from t_user
  </select>

三、测试

1、查询用户

使用postman查询user id为2的用户

 

从sql路由可以看出,查询了所有数据源,所有的user表。 

2、插入用户

使用postman插入city id为1,user id为5的用户。
根据分库策略,写入到的库应该是:land city id % 2,所以应该 land1
根据分表策略,写入到的表应该是:user id%2,所以应该是 user1

 

posted @ 2020-03-06 11:38  Alan6  阅读(3427)  评论(0编辑  收藏  举报