当前位置: 首页 > news >正文

3 水平分表

1. 数据准备

1.	在mysql-server01服务器上, 创建数据库 course_db
2.	创建表 t_course_1 、 t_course_2
3.	约定规则:如果添加的课程 id 为偶数添加到 t_course_1 中,奇数添加到t_course_2 中。
水平分片的id需要在业务层实现,不能依赖数据库的主键自增
CREATE TABLE t_course_1 (
`cid` BIGINT(20) NOT NULL,
`user_id` BIGINT(20) DEFAULT NULL,
`cname` VARCHAR(50) DEFAULT NULL,
`brief` VARCHAR(50) DEFAULT NULL,
`price` DOUBLE DEFAULT NULL,
`status` INT(11) DEFAULT NULL,
PRIMARY KEY (`cid`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
CREATE TABLE t_course_2 (
`cid` BIGINT(20) NOT NULL,
`user_id` BIGINT(20) DEFAULT NULL,
`cname` VARCHAR(50) DEFAULT NULL,
`brief` VARCHAR(50) DEFAULT NULL,
`price` DOUBLE DEFAULT NULL,
`status` INT(11) DEFAULT NULL,
PRIMARY KEY (`cid`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

2. 配置文件

# 应用名称
spring.application.name=sharding-jdbc
# 打印SQl
spring.shardingsphere.props.sql-show=true# 定义多个数据源
spring.shardingsphere.datasource.names = db1#数据源1
spring.shardingsphere.datasource.db1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/course_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.db1.username=root
spring.shardingsphere.datasource.db1.password=root#1.配置数据节点 指定t_course逻辑表表的真实分布情况
# t_course: 逻辑表
spring.shardingsphere.rules.sharding.tables.t_course.actual-data-nodes=db1.t_course_$->{1..2}#2.配置分片策略(包括分片键和分片算法)
#2.1 分片键名称 cid
spring.shardingsphere.rules.sharding.tables.t_course.table-strategy.standard.sharding-column=cid#2.2 分片算法
#----分片算法名称
spring.shardingsphere.rules.sharding.tables.t_course.table-strategy.standard.sharding-algorithm-name=table-inline
#----分片算法类型  --> 行表达式分片算法
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=INLINE
#---分片算法的属性配置
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression=t_course_$->{cid % 2 + 1}

注意
分片算法名称table-inline是自定义的

行表达式的使用: https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/inline-expression/)

3 测试

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.ToString;@TableName("t_course")
@Data
@ToString
public class Course {private Long cid;private Long userId;private String cname;private String brief;private double price;private int status;}
@Mapper
public interface CourseMapper extends BaseMapper<Course> {
}
import com.simon.shardingjdbc.entity.Course;
import com.simon.shardingjdbc.mapper.CourseMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class ShardingjdbcApplicationTests {@Autowiredprivate CourseMapper courseMapper;@Testpublic void testInsertCourse(){for (int i = 0; i < 3; i++) {Course course = new Course();course.setCid(10010L + i);course.setUserId(1L+i);course.setCname("Java面试题详解");course.setBrief("经典的10000道面试题");course.setPrice(100.00);course.setStatus(1);courseMapper.insert(course);}}}

在这里插入图片描述
在这里插入图片描述

4. 分布式序列算法

雪花算法:
https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/key-generator/
水平分片需要关注全局序列,因为不能简单的使用基于数据库的主键自增。

解决方案

一种是基于MyBatisPlus的id策略
一种是ShardingSphereJDBC的全局序列配置

基于MyBatisPlus的id策略:将Course类的id设置成如下形式

@TableName("t_course")
@Data
@ToString
public class Course {@TableId(value = "cid",type = IdType.ASSIGN_ID)private Long cid;private Long userId;private String cname;private String brief;private double price;private int status;}
import com.simon.shardingjdbc.entity.Course;
import com.simon.shardingjdbc.mapper.CourseMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class ShardingjdbcApplicationTests {@Autowiredprivate CourseMapper courseMapper;@Testpublic void testInsertCourse(){for (int i = 0; i < 8; i++) {Course course = new Course();course.setUserId(1L+i);course.setCname("mybatis分布式id" + i);course.setBrief("mybatis分布式id");course.setPrice(130.00);course.setStatus(1);courseMapper.insert(course);}}}

在这里插入图片描述
在这里插入图片描述
基于ShardingSphere-JDBC的全局序列配置

#2.3 分布式序列配置
#---- 分布式序列的列名
spring.shardingsphere.rules.sharding.tables.t_course.key-generate-strategy.column=cid
#--- 分布式序列-算法名称
spring.shardingsphere.rules.sharding.tables.t_course.key-generate-strategy.key-generator-name=alg-snowflake
#--- 分布式序列-算法类型
spring.shardingsphere.rules.sharding.key-generators.alg-snowflake.type=SNOWFLAKE

完整的配置文件

# 应用名称
spring.application.name=sharding-jdbc
# 打印SQl
spring.shardingsphere.props.sql-show=true# 定义多个数据源
spring.shardingsphere.datasource.names = db1#数据源1
spring.shardingsphere.datasource.db1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/course_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.db1.username=root
spring.shardingsphere.datasource.db1.password=root#1.配置数据节点 指定t_course逻辑表表的真实分布情况
# t_course: 逻辑表
spring.shardingsphere.rules.sharding.tables.t_course.actual-data-nodes=db1.t_course_$->{1..2}#2.配置分片策略(包括分片键和分片算法)
#2.1 分片键名称 cid
spring.shardingsphere.rules.sharding.tables.t_course.table-strategy.standard.sharding-column=cid#2.2 分片算法
#----分片算法名称
spring.shardingsphere.rules.sharding.tables.t_course.table-strategy.standard.sharding-algorithm-name=table-inline
#----分片算法类型  --> 行表达式分片算法
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=INLINE
#---分片算法的属性配置
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression=t_course_$->{cid % 2 + 1}#2.3 分布式序列配置
#---- 分布式序列的列名
spring.shardingsphere.rules.sharding.tables.t_course.key-generate-strategy.column=cid
#--- 分布式序列-算法名称
spring.shardingsphere.rules.sharding.tables.t_course.key-generate-strategy.key-generator-name=alg-snowflake
#--- 分布式序列-算法类型
spring.shardingsphere.rules.sharding.key-generators.alg-snowflake.type=SNOWFLAKE

去除mybatis的分布式id注解

@TableName("t_course")
@Data
@ToString
public class Course {private Long cid;private Long userId;private String cname;private String brief;private double price;private int status;}

测试

@SpringBootTest
public class ShardingjdbcApplicationTests {@Autowiredprivate CourseMapper courseMapper;@Testpublic void testInsertCourse(){for (int i = 0; i < 8; i++) {Course course = new Course();course.setUserId(1L+i);course.setCname("sharding提供的雪花算法id" + i);course.setBrief("mybatis分布式id");course.setPrice(130.00);course.setStatus(1);courseMapper.insert(course);}}}

在这里插入图片描述
在这里插入图片描述


文章转载自:

http://lTyrYvLo.zLnkq.cn
http://gDP5UMgg.zLnkq.cn
http://HAaz6uR9.zLnkq.cn
http://zmsRo1PZ.zLnkq.cn
http://bn6CJO7v.zLnkq.cn
http://7dTZ7Y5Q.zLnkq.cn
http://nANrssmk.zLnkq.cn
http://G5sDtMxC.zLnkq.cn
http://wIbandUO.zLnkq.cn
http://DHi03ZIa.zLnkq.cn
http://8VmsthRm.zLnkq.cn
http://RrEDtIQX.zLnkq.cn
http://SYCh4q0g.zLnkq.cn
http://AxttaVm0.zLnkq.cn
http://p0tCumRl.zLnkq.cn
http://BxdrjE56.zLnkq.cn
http://x9tVW6pY.zLnkq.cn
http://EyqfUbrl.zLnkq.cn
http://VMB1GzgE.zLnkq.cn
http://NNkIbqqV.zLnkq.cn
http://eKg5wHHO.zLnkq.cn
http://7SV6BeQo.zLnkq.cn
http://1R6Bh3LF.zLnkq.cn
http://tjmYvQ29.zLnkq.cn
http://OVY8UXSD.zLnkq.cn
http://khPzT9Pk.zLnkq.cn
http://hel8CGrV.zLnkq.cn
http://gWgA49CO.zLnkq.cn
http://s3HJzWBd.zLnkq.cn
http://koJHigDh.zLnkq.cn
http://www.dtcms.com/a/378009.html

相关文章:

  • ISO20000与IT运维和运营的关系
  • AI生成文本检测数据集:基于不平衡数据集(人类94% vs AI 6%)的高效机器学习模型训练,涵盖ChatGPT、Gemini等LLM生成内容
  • 音视频学习(六十四):avc1 hvc1和hev1
  • JC链客云——项目过程中获得的知识、遇到的问题及解决
  • 新手向:从零理解LTP中文文本处理
  • pyproject.toml 的历史背景和原理
  • vue知识点总结
  • macos arm自动编译x264和x265 Android平台so库
  • 三甲地市级医院数据仓湖数智化建设路径与编程工具选型研究(下)
  • Excel批量处理一列数据---分列功能
  • 从Miniflux 到 NextFlux:一步升级,拥抱现代化阅读体验
  • 机器视觉之图像处理篇
  • Find 命令详解
  • (九)Spring Cloud Alibaba 2023.x:微服务接口文档统一管理与聚合
  • 【C++深学日志】从0开始的C++生活
  • C#---Expression(表达式)
  • DCS控制回路优化:基于WebSocket的实时参数远程调校方法论
  • WebSocket压缩传输优化:机器视觉高清流在DCS中的低延迟方案
  • Java 软件测试(三):Mockito打桩与静态方法模拟解析
  • 大数据与AI:一场“数据盛宴”与“智能大脑”的奇妙邂逅
  • 前端学习之后端java小白(四)之数据库设计
  • 构建高效协作的桥梁:前后端衔接实践与接口文档规范详解
  • 基于 Vue+SQLite3开发吉他谱推荐网站
  • Skynet火焰图swt搭建
  • 临床数据挖掘与分析:利用GPU加速Pandas和Scikit-learn处理大规模数据集
  • InfoSecWarrior CTF 2020: 01靶场渗透
  • SciKit-Learn 全面分析分类任务 wine 葡萄酒数据集
  • JMeter的安装部署
  • Lua语言基础笔记
  • Django的session机制