当前位置:网站首页>ShardingSphere之水平分库实战(四)
ShardingSphere之水平分库实战(四)
2022-07-31 00:40:00 【融极】
概述
场景
创建两个数据库
环境搭建
建库、建表

CREATE TABLE `course_1` (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NOT NULL,
`user_id` bigint(20) NOT NULL,
`cstatus` varchar(10) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
springboot环境搭建
配置分片规则
# 配置分片策略
# 配置数据源
spring.shardingsphere.datasource.names=m1,m2
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=123456
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=123456
# 指定course表分布情况,配置表在哪个数据库中,表名称都是什么
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{
1..2}.course_$->{
1..2}
# 指定course主键cid生成策略SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
# 指定表分片策略, cid是偶数添加到course_1表中,cid是奇数添加到course_2
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{
cid % 2 + 1}
# 指定库的中course表的分片策略
spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{
user_id % 2 + 1}
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
# 解决一个Course实体类不能对应两张表的问题
spring.main.allow-bean-definition-overriding=true
编写测试代码
@Test
void addFk() {
for (int i = 0; i < 10; i++) {
Course course = new Course();
course.setCname("java");
course.setUserId(101L);
course.setCstatus("normal");
courseMapper.insertSelective(course);
}
}
@Test
public void findCourseDb() {
Example example = new Example(Course.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("userId", 100L);
criteria.andEqualTo("cid", 760185728628948993L);
Course course = courseMapper.selectOneByExample(example);
System.out.println(course);
}
边栏推荐
- How to Repair Word File Corruption
- Oracle has a weird temporary table space shortage problem
- Shell programming of conditional statements
- xss的绕过
- Restricted character bypass
- Strict Mode for Databases
- 【唐宇迪 深度学习-3D点云实战系列】学习笔记
- .NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
- Jetpack Compose学习(8)——State及remeber
- DNS解析过程【访问网站】
猜你喜欢

限制字符绕过

Bypass of xss

什么是Promise?Promise的原理是什么?Promise怎么用?

Niuke.com question brushing training (4)

Asser uses ant sword to log in

How to solve the error of joiplay simulator

【多线程】

Jmeter参数传递方式(token传递,接口关联等)

Shell programming of conditional statements

Thesis understanding: "Designing and training of a dual CNN for image denoising"
随机推荐
WEB安全基础 - - -漏洞扫描器
Asser uses ant sword to log in
SWM32系列教程6-Systick和PWM
不用Swagger,那我用啥?
MySQL筑基篇之增删改查
How to adjust Chinese in joiplay simulator
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
【愚公系列】2022年07月 Go教学课程 016-运算符之逻辑运算符和其他运算符
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
过滤器(Filter)
WMware Tools installation failed segmentation fault solution
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes
How to install joiplay emulator rtp
binglog log tracking: data backup and backup tracking
xss靶机训练【实现弹窗即成功】
Error in go mode tidy go warning “all” matched no packages
Shell programming of conditional statements
分布式系统的一致性与共识(1)-综述
Error occurred while trying to proxy request项目突然起不来了
【深入浅出玩转FPGA学习14----------测试用例设计2】