首先新建web项目,我这采用的是springboot2.5.7,在web项目中引入分库分表需要的jar包:


<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
    <version>5.0.0-alpha</version>
</dependency>


然后在application.yml中配置分库分表策略,项目源码见下面链接:

链接:https://pan.baidu.com/s/1OW7v1V9WTwB09M8FQ-Ea7Q?pwd=4kx6 
提取码:4kx6

application.yml 中涉及到的库和表需要自己手动创建;

启动项目后打开网页:http://localhost:8798/swagger-ui.html  可以进行方法的调用查看结果;

       以上的操作只是写值的时候写入到了不同的库和表,查询的时候并不能根据条件在所有的库和表中获得所有的数据,所以使用shardingsphere-proxy创建代理库来实现,以下操作我在本机windows环境

1、下载:shardingsphere-proxy,地址:下载 :: ShardingSphere

     

项目中引入springcloudcontext 项目中引入分库分表_bc

2、解压后修改conf下面的文件server.yaml、config-sharding.yaml如下:

 server.yaml内容,根据需要修改数据库账号密码:

# 使用navicate for mysql连接的时候的账号和密码
 authentication:
   users:
     root:
       password: rootprops:
   max-connections-size-per-query: 1
   acceptor-size: 16  # The default value is available processors count * 2.
   executor-size: 16  # Infinite by default.
   proxy-frontend-flush-threshold: 128  # The default value is 128.
   proxy-transaction-type: LOCAL
   proxy-opentracing-enabled: false
   proxy-hint-enabled: false
   query-with-cipher-column: true
   # true 表示展示SQL日志
   sql-show: true
   check-table-metadata-enabled: false

config-sharding.yaml,以下是使用t_user表进行验证:

# 使用navicate for mysql的时候显示的数据库的名称
 schemaName: sharding-db1dataSourceCommon:
   connectionTimeoutMilliseconds: 30000
   idleTimeoutMilliseconds: 60000
   maxLifetimeMilliseconds: 1800000
   maxPoolSize: 50
   minPoolSize: 1
   maintenanceIntervalMilliseconds: 30000dataSources:
   db0:
     url: jdbc:mysql://127.0.0.1:3306/shardingsphere0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
     username: root
     password: 'root'
   db1:
     url: jdbc:mysql://127.0.0.1:3306/shardingsphere1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
     username: root
     password: 'root'rules:
   - !SHARDING
     tables:
       t_user:
         actualDataNodes: db${0..1}.t_user${0..3}
         tableStrategy:
           standard:
             shardingColumn: user_id
             shardingAlgorithmName: userTableInline
         databaseStrategy:
           standard:
             shardingColumn: user_id
             shardingAlgorithmName: userDbInline    shardingAlgorithms:
       userTableInline:
         type: INLINE
         props:
           algorithm-expression: t_user${user_id % 4}
       userDbInline:
         type: INLINE
         props:
           algorithm-expression: db${user_id % 2}


 

 3、启动sharedingsphere-proxy,在bin目录下执行start.bat 3316,3316这个端口是我随机取的,为了和mysql默认端口区分,据说默认端口是3307没验证过

项目中引入springcloudcontext 项目中引入分库分表_java_02

4、使用mysql工具链接这个代理库,连上后创建表、新增、查询等在shardingshpere-proxy控制台中可以看到是对多库多表进行的操作,实际我们操作时是在代理库中进行的单库单表操作;

项目中引入springcloudcontext 项目中引入分库分表_java_03

项目中引入springcloudcontext 项目中引入分库分表_mysql_04

        有了shardingshpere-proxy生成的代理库后其实就不再需要shardingsphere,进行项目的开发时直接连代理库就可以,解决了shardingsphere中排序、分页查询等老大难问题;

        分库分表虽然解决了单机容量的问题,但是也带来了很多问题,例如jdbc业务侵入大,业务的sql不再是单纯的在机器上运行,对大量的跨纬度join,聚合,子查询。排序等功能很难支持。大大弱化了数据库的功能。所以一般能不分就不分,能使用分布式数据库就使用分布式数据库库,或者加上缓存,es等方式优先解决