当前位置:网站首页>ShardingSphere之未分片表配置实战(六)
ShardingSphere之未分片表配置实战(六)
2022-07-31 00:40:00 【融极】
概述
场景
在实际工作中,有些表需要参与分片操作,有些表则无需分片,那么这种场景下,sharding-jdbc该如何配置呢?
SpringBoot环境搭建
application.properties配置
涉及未分配表的配置如下,只需要指定default-data-source-name即可。
# 默认数据库,不分片的表都在这里
spring.shardingsphere.datasource.mx.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.mx.driver-class-name=com.mysql.cj.jdbc.Driver
# course_db数据库无需单独建立,也可以是和其他的分片数据库相同,比如可以是edu_db_1
spring.shardingsphere.datasource.mx.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.mx.username=root
spring.shardingsphere.datasource.mx.password=123456
# 默认数据源,未分片的表默认执行库
spring.shardingsphere.sharding.default-data-source-name=mx
全量配置如下
# 配置分片策略
# 配置数据源
spring.shardingsphere.datasource.names=m0,m1,m2,mx
# 默认数据库,不分片的表都在这里
spring.shardingsphere.datasource.mx.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.mx.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.mx.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.mx.username=root
spring.shardingsphere.datasource.mx.password=123456
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
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=123456
# 默认数据源,未分片的表默认执行库
spring.shardingsphere.sharding.default-data-source-name=mx
# 指定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}
# 指定t_user表分布情况,配置表在哪个数据库中,表名称都是什么
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m0.t_user
# 指定t_user主键user_id生成策略SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE
# 指定表分片策略
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
# 解决一个Course实体类不能对应两张表的问题
spring.main.allow-bean-definition-overriding=true
表结构
CREATE TABLE `t_study` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`course_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
测试代码
package com.study.shardingjdbcdemo.entity;
import lombok.Data;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Table(name = "t_study")
public class Study {
@Id
private Long id;
private String name;
private String courseName;
}
package com.study.shardingjdbcdemo.mapper;
import com.study.shardingjdbcdemo.entity.Study;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
@Repository
public interface StudyMapper extends Mapper<Study> {
}
@Test
public void defaultDb() {
Study study = new Study();
study.setName("xm");
study.setCourseName("mysql");
studyMapper.insert(study);
}
参考
边栏推荐
- SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
- .NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
- 【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
- MySQL数据库约束,表的设计
- Common network status codes
- 【多线程】
- MySQL中substring与substr区别
- what is jira
- MySQL的触发器
- ELK deployment script---pro test available
猜你喜欢
随机推荐
Detailed explanation of 9 common reasons for MySQL index failure
WEB Security Basics - - - Vulnerability Scanner
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
IOT跨平台组件设计方案
不用Swagger,那我用啥?
牛客网刷题训练(四)
Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
MySQL triggers
binglog日志追踪:数据备份并备份追踪
MySQL master-slave replication and read-write separation script - pro test available
ABC 261 F - Sorting Color Balls(逆序对)
How to solve the error of joiplay simulator
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
pytorch bilinear interpolation
[In-depth and easy-to-follow FPGA learning 14----------Test case design 2]
Homework: iptables prevent nmap scan and binlog
[In-depth and easy-to-follow FPGA learning 15---------- Timing analysis basics]
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
MySQL database advanced articles