ShardingSphere實現分庫分表
有關分庫分表前面寫了四篇博客:
2、分庫分表(2) --- ShardingSphere(理論)
3、分庫分表(3) ---SpringBoot + ShardingSphere實現讀寫分離
4、分庫分表(4) ---SpringBoot + ShardingSphere 實現分表
這篇博客通過ShardingSphere實現分庫分表
,並在文章最下方附上項目Github地址
。
一、項目概述
1、技術架構
項目總體技術選型
SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4 + MySQL + lombok(插件)
2、項目說明
場景
在實際開發中,如果表的數據過大我們需要把一張表拆分成多張表,也可以垂直切分把一個庫拆分成多個庫,這里就是通過ShardingSphere實現分庫分表
功能。
3、數據庫設計
分庫
ds一個庫分為 ds0庫 和 ds1庫。
分表
tab_user一張表分為tab_user0表 和 tab_user1表。
如圖
ds0庫
ds1庫
具體的創建表SQL也會放到GitHub項目里
二、核心代碼
說明
完整的代碼會放到GitHub上,這里只放一些核心代碼。
1、application.properties
server.port=8084
#指定mybatis信息
mybatis.config-location=classpath:mybatis-config.xml
#打印sql
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.datasource.names=ds0,ds1
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root
#根據年齡分庫
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{age % 2}
#根據id分表
spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes=ds$->{0..1}.tab_user$->{0..1}
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression=tab_user$->{id % 2}
Sharding-JDBC可以通過Java
,YAML
,Spring命名空間
和Spring Boot Starter
四種方式配置,開發者可根據場景選擇適合的配置方式。具體可以看官網。
2、UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* 模擬插入數據
*/
List<User> userList = Lists.newArrayList();
/**
* 初始化插入數據
*/
@PostConstruct
private void getData() {
userList.add(new User(1L,"小小", "女", 3));
userList.add(new User(2L,"爸爸", "男", 30));
userList.add(new User(3L,"媽媽", "女", 28));
userList.add(new User(4L,"爺爺", "男", 64));
userList.add(new User(5L,"奶奶", "女", 62));
}
/**
* @Description: 批量保存用戶
*/
@PostMapping("save-user")
public Object saveUser() {
return userService.insertForeach(userList);
}
/**
* @Description: 獲取用戶列表
*/
@GetMapping("list-user")
public Object listUser() {
return userService.list();
}
三、測試驗證
1、批量插入數據
請求接口
localhost:8084/save-user
我們可以從商品接口代碼中可以看出,它會批量插入5條數據。我們先看控制台輸出SQL語句
我們可以從SQL語句可以看出 ds0 和 ds1 庫中都插入了數據。
我們再來看數據庫
ds0.tab_user0
ds0.tab_user1
ds1.tab_user0
ds1.tab_user1
完成分庫分表插入數據。
2、獲取數據
這里獲取列表接口的SQL,這里對SQL做了order排序操作,具體ShardingSphere分表實現order操作的原理可以看上面一篇博客。
select * from tab_user order by age <!--根據年齡排序-->
請求接口結果

我們可以看出雖然已經分庫分表,但依然可以將多表數據聚合在一起並可以支持按age排序。
注意
ShardingSphere並不支持CASE WHEN
、HAVING
、UNION (ALL)
,有限支持子查詢
。這個官網有詳細說明。
Github地址
:https://github.com/yudiandemingzi/spring-boot-sharding-sphere
參考
我相信,無論今后的道路多么坎坷,只要抓住今天,遲早會在奮斗中嘗到人生的甘甜。抓住人生中的一分一秒,勝過虛度中的一月一年!(20)