文章目录
一、前言
分库分表的技术现在相对来说已经很成熟了,现在比较流行的就是量种方式,mycat是一种解耦方式代理实现的分库分表中间件,代码侵入性低,但是运维维护的话相对于sharding比较麻烦,还有就是mycat的冗余字段表、事务、分页问题等需要特殊的处理,所以相对讲sharding的使用更简单些,但是sharding的话对于依赖包的版本匹配则更严格,也会面临升级的问题,但是这些相对来说可控。本篇是基于2.3.0.RELEASE和sharding的分库分表事件总结。
二、实战
1.org.apache.shardingsphere和io.shardingsphere的关系:
io.shardingsphere下面的sharding-jdbc-spring-boot-starter的到了3.x以后现在已经不维护了,所以使用springboot高版本的sharding最好是使用org.apache.shardingsphere里面的sharding-jdbc-spring-boot-starter,因为sharding是2018年加入到Apache基金会孵化器。后面的版本迭代也会在org.apache.shardingsphere里面进行发布。
官网开发手册地址:https://shardingsphere.apache.org/document/current/cn/overview/
2.相关依赖包:
这次实战使用的是spring-boot-starter-parent 2.3.0.RELEASE、mysql8、com.alibaba、mybatis-spring-boot-starter、com.github.pagehelper,这些组件版本的版本号非常重要,我在此过程中就是踩了很多坑,然后最终才配置完成启动了起来成功运行。
3.分库分表配置:
这是一个比较重要的环节,因为分库的逻辑和分表的逻辑在这里实现,直接上干货配置:
spring.shardingsphere.datasource.names=doc0,doc1
spring.shardingsphere.datasource.doc0.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.doc0.driver-class-name= com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.doc0.url=
spring.shardingsphere.datasource.doc0.username=
spring.shardingsphere.datasource.doc0.password=
#数据源2
spring.shardingsphere.datasource.doc1.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.doc1.driver-class-name= com.mysql.cj.jdbc.Driver
#数据库链接账号
spring.shardings