MYSQLg高级-------分库分表之核心Sharding-JDBC(二)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: MYSQLg高级-------分库分表之核心Sharding-JDBC(二)

3.Sharding-JDBC 实现水平分库


1 、需求分析


2 、创建数据库和表

create database edu_db_1;
create database edu_db_2;
use edu_db_1;
create table course_1 (
   `cid` bigint(20) primary key,
   `cname` varchar(50) not null,
   `user_id` bigint(20) not null,
   `status` varchar(10) not null
);
create table course_2 (
   `cid` bigint(20) primary key,
   `cname` varchar(50) not null,
   `user_id` bigint(20) not null,
   `status` varchar(10) not null
);
use edu_db_2;
create table course_1 (
   `cid` bigint(20) primary key,
   `cname` varchar(50) not null,
   `user_id` bigint(20) not null,
   `status` varchar(10) not null
);
create table course_2 (
   `cid` bigint(20) primary key,
   `cname` varchar(50) not null,
   `user_id` bigint(20) not null,
   `status` varchar(10) not null
);


3 、在 SpringBoot 配置文件配置数据库分片规则

shardingjdbc分片策略

# sharding-jdbc 水平分库分表策略
# 配置数据源,给数据源起别名
# 水平分库需要配置多个数据库
spring.shardingsphere.datasource.names=m1,m2
# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true
# 配置第一个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
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=root
# 配置第二个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
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=root
# 指定数据库分布的情况和数据表分布的情况
# m1 m2 (库)  course_1 course_2 (表)
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
# 指定分库策略    约定 user_id 值偶数添加到 m1 库,如果 user_id 是奇数添加到 m2 库
# 默认写法(所有的表的user_id)
#spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
#spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=m$->{user_id % 2 + 1}
# 指定只有course表的user_id
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}
# 指定分表策略    约定 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}
# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true


4 、编写测试方法


//======================测试水平分库=====================
//添加操作
@Test
public void addCourseDb() {
Course course = new Course();
course.setCname("javademo1");
//分库根据user_id
course.setUserId(111L);
course.setCstatus("Normal1");
courseMapper.insert(course);
}
//查询操作
@Test
public void findCourseDb() {
QueryWrapper wrapper = new QueryWrapper<>();
//设置userid值
wrapper.eq("user_id",100L);
//设置cid值
wrapper.eq("cid",465162909769531393L);
Course course = courseMapper.selectOne(wrapper);
System.out.println(course);
}


Sharding-JDBC 实现垂直分库


1 、需求分析


2 、创建数据库和表

create database user_db;
use user_db;
create table t_user(
   `user_id` bigint(20) primary key,
   `username` varchar(100) not null,
   `ustatus` varchar(100) not null
);


3 、编写操作代码

( 1 )创建user实体类和mapper


@Data
@TableName(value = "t_user") //指定数据库 表
public class User {
    private Long userId;
    private String username;
    private String ustatus;
}
@Repository
public interface UserMapper extends BaseMapper<User> {
}


( 2 )配置垂直分库策略

# sharding-jdbc 水平分库分表策略
# 配置数据源,给数据源起别名
# 水平分库需要配置多个数据库
# m0为用户数据库
spring.shardingsphere.datasource.names=m1,m2,m0
# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true
# 配置第一个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
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=root
# 配置第二个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
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=root
# 配置user数据源的具体内容,包含连接池,驱动,地址,用户名,密码
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=root
# 配置user_db数据库里面t_user  专库专表
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m$->{0}.t_user
# 配置主键的生成策略
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
# 指定course表分布的情况,配置表在哪个数据库里,表的名称都是什么  course 表规则(暂时这么理解)  m1.course_$->{1..2}=m1.course_1,m1.course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.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
#这部分理解起来比较复杂我这边就啰嗦下: course_$->{cid % 2 + 1}    course_也是相当于对应表空间m1.course_1
#cid 是他的主键的iD cid % 2 + 1  举例说明下: 当cid =(偶数)4/2余0 0+1=1 那么他的数据就在  course_1 中 上面指定了2个 1.course_$->{1..2}
#其余的结果就都匹配上面的 2 我理解的就是 不满足条件1 就都是条件2的东西
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1}
# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true


( 3 )编写测试代码


//注入user的mapper
 @Autowired
    private UserMapper userMapper;
//======================测试垂直分库==================
//todo ==================================垂直分库==========================================
    @Test
    public void addDict() {
        User dict = new User();
        dict.setUsername("Normal");
        dict.setUstatus("启用11");
        userMapper.insert(dict);
    }
    @Test
    public void deleteDict() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", 765723049042378753L);
      //  userMapper.delete(wrapper);
        final User user = userMapper.selectOne(wrapper);
        System.out.println(user);
    }
}

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
8月前
|
SQL 算法 搜索推荐
Mqsql使用Sharding-JDBC案例实战 2
Mqsql使用Sharding-JDBC案例实战
64 0
|
SQL 关系型数据库 MySQL
ShardingSphere-Sharding-Proxy(安装和分表配置)| 学习笔记
快速学习ShardingSphere-Sharding-Proxy(安装和分表配置)。
883 0
ShardingSphere-Sharding-Proxy(安装和分表配置)| 学习笔记
|
8天前
|
负载均衡 算法 Java
Sharding-JDBC如何实现读写分离
通过以上步骤,Sharding-JDBC能够实现数据库的读写分离,从而提高应用程序的读取性能。欢迎关注威哥爱编程,一起学习成长。
|
8月前
|
算法 Java 关系型数据库
Mqsql使用Sharding-JDBC案例实战 1
Mqsql使用Sharding-JDBC案例实战
24 0
|
8天前
|
监控 负载均衡 关系型数据库
MySQL技能完整学习列表13、MySQL高级特性——1、分区表(Partitioning)——2、复制(Replication)——3、集群(Clustering)
MySQL技能完整学习列表13、MySQL高级特性——1、分区表(Partitioning)——2、复制(Replication)——3、集群(Clustering)
56 0
|
8天前
|
关系型数据库 MySQL Java
(详解与使用)Sharding-JDBC通过mysql主从复制来进行项目优化
(详解与使用)Sharding-JDBC通过mysql主从复制来进行项目优化
38 0
|
10月前
|
算法 druid Java
MYSQLg高级-------分库分表之核心Sharding-JDBC(一)
MYSQLg高级-------分库分表之核心Sharding-JDBC(一)
138 0
|
10月前
|
存储 数据库
MYSQLg高级-------分库分表之核心Sharding-JDBC(三)
MYSQLg高级-------分库分表之核心Sharding-JDBC(三)
62 0
|
10月前
|
SQL 监控 关系型数据库
MYSQLg高级------Sharding-JDBC 实现读写分离
MYSQLg高级------Sharding-JDBC 实现读写分离
110 0
|
10月前
|
Java 数据库
MYSQLg高级-------分库分表之核心Sharding-Proxy 简介(三)
MYSQLg高级-------分库分表之核心Sharding-Proxy 简介(三)
103 0