当前位置:网站首页>框架整合(二)- 使用Apache ShardingSphere实现数据分片
框架整合(二)- 使用Apache ShardingSphere实现数据分片
2022-08-04 17:24:00 【InfoQ】
序
解决方案
- 实现分库分表的的解决方案
- Apache ShardingShpere(本博文选择的方案)
- Mycat
具体实现(分表)
- 创建数据库(ds0)和表t_order_0,t_order_1
CREATE TABLE `t_order_0` (
`order_id` bigint(20) unsigned NOT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `t_order_1` (
`order_id` bigint(20) unsigned NOT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

- 创建springboot项目并结合mybatis-plus
- 引入项目依赖shardingsphere(其他基础依赖略)
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.0.0-alpha</version>
</dependency>
- 在application.properties文件中加入shardingsphere配置
- 配置中注释掉的配置是分库的相关配置
- 着重注意分片相关配置后面的值设置规则
- 当前分表策略(表字段order_id),分表算法配置为取模等
# 配置真实数据源
spring.shardingsphere.datasource.names=ds0
spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.common.username=root
spring.shardingsphere.datasource.common.password= root
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/ds0?serverTimezone=UTC&useSSL=false
#spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC&useSSL=false
# 配置 t_order 表规则
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds0.t_order_$->{0..1}
# 配置分库策略
#spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column=user_id
#spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name=database-inline
# 配置分表策略
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=table-inline
# 配置 分片算法
#spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
#spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression=t_order_$->{order_id % 2}
# 分布式序列策略配置
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake
# 分布式序列算法配置
spring.shardingsphere.rules.sharding.key-generators.snowflake.type =SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id =123
spring.shardingsphere.props.sql-show=true
- 创建order实体类
@TableName("t_order")
@Data
public class Order {
@TableId(value = "order_id", type = IdType.ID_WORKER)
private Long orderId;
private Integer userId;
}
- 使用sql操作分表有许多禁用条件,可查看官方文档进行排查点击跳转使用规范页
测试
- 批量插入20条数据
@Autowired
private OrderService orderService;
@Test
public void insert() {
List<Order> orderList =new ArrayList<Order>();
for (int i = 0; i < 20; i++) {
Order orderEntity = new Order();
// orderEntity.setOrderId(System.currentTimeMillis());
orderEntity.setUserId(new Random().nextInt(999));
orderList.add(orderEntity);
}
orderService.saveBatch(orderList);
}
- 查看数据库,2个表(通过分片配置算法取模)分别插入了数据
- 查询一条order数据()
@Autowired
private OrderMapper orderMapper;
@Test
public void getOrder(){
Order orderId = orderMapper.getOrderId(1401545531558088708L);
System.out.println(orderId);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xf.mapper.OrderMapper">
<select id="getOrderId" resultType="com.xf.entity.Order" parameterType="long">
select * from t_order where order_id = #{id}
</select>
</mapper>
- 查看控制台打印的查询sql
- 查询结果
边栏推荐
- 机器学习(十三):支持向量机(SVM)
- 罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
- R语言ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、使用ggarrange函数将表格数据和可视化图像组合起来(表格数据在可视化图像下方)
- 树莓派安装samba用来共享文件
- WPF 修改 ItemContainerStyle 鼠标移动到未选中项效果和选中项背景
- 《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
- JVM内存和垃圾回收-08.方法区
- What does the product system of a digital financial enterprise look like?
- 移动平台助力推进智慧型科研院所信息化建设
- Digital-intelligent supply chain management system for chemical manufacturing industry: build a smart supply system and empower enterprises to improve production efficiency
猜你喜欢

Learning to Explore - Setting the Foreground Color for Fonts

第一章 对象和封装

谷歌开发者社区推荐:《Jetpack Compose 从入门到实战》新书上架,带你踏上 Compose 开发之旅~

小程序笔记3

力拓信创生态,博睿数据多款产品获得东方通与达梦数据库产品兼容互认证明

localhost,127.0.0.1,本机IP

【LeetCode每日一题】——540.有序数组中的单一元素

mysql学习笔记——利用动态SQL和Session变量实现一个公式或者计算器

信息系统项目管理师必背核心考点(六十)项目集管理

Boost library study notes (1) Installation and configuration
随机推荐
Boost库学习笔记(一)安装与配置
】 【 LeetCode daily one problem - 540. The order of a single element of the array
在VMD上可视化hdf5格式的分子轨迹文件
机器学习(十):朴素贝叶斯
Flutter实战-请求封装(四)之gzip报文压缩
codeforces每日5题(均1600)-第二十八天
NLP未来,路在何方?从学术前沿和业界热点谈起
吃透Chisel语言.32.Chisel进阶之硬件生成器(一)——Chisel中的参数化
设置表头颜色
R语言计算时间序列数据的逐次差分(successive differences):使用diff函数计算时间序列数据的逐次差分值
hi, 请问下这是什么问题, 我看官网的example就是mysql的, 咋提示不支持?
西西成语接龙小助手
机器学习(十九):梯度提升回归(GBR)
mysql学习笔记——利用动态SQL和Session变量实现一个公式或者计算器
提高图片清晰度的快速方法?
《机器学习理论到应用》电子书免费下载
全球电子产品需求萎靡:三星越南工厂大幅压缩产能,减少工人工作日
Digital-intelligent supply chain management system for chemical manufacturing industry: build a smart supply system and empower enterprises to improve production efficiency
机器学习(十八):随机搜索和XGBoost
Catering Supply Chain Management System


