Springboot整合ShardingSphere实现分库分表,垂直拆分、水平拆分、公共表的处理

最后

针对以上面试题,小编已经把面试题+答案整理好了

最新大厂必问微服务面试题汇总:SpringCloud、Boot、Dubbo

最新大厂必问微服务面试题汇总:SpringCloud、Boot、Dubbo

最新大厂必问微服务面试题汇总:SpringCloud、Boot、Dubbo

面试专题

image

除了以上面试题+答案,小编同时还整理了微服务相关的实战文档也可以分享给大家学习

image

image

image

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

mapper层


@Repository

public interface CourseMapper extends BaseMapper<Course> {

}

启动类加注解


@MapperScan("com.lzq.mapper")

配置 Sharding-JDBC 分片策略

在配置文件中配置


# shardingjdbc 分片策略

# 配置数据源,给数据源起名称

spring.shardingsphere.datasource.names=m1



# 一个实体类对应两张表,覆盖

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/sharding?serverTimezone=GMT%2B8

spring.shardingsphere.datasource.m1.username=root

spring.shardingsphere.datasource.m1.password=root

#指定 course 表分布情况,配置表在哪个数据库里面,表名称都是什么 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

spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1}

# 打开 sql 输出日志

spring.shardingsphere.props.sql.show=true

测试


@SpringBootTest

class ShardingJdbcApplicationTests {

    @Autowired

    private CourseMapper courseMapper;

    @Test

    void add() {

        Course course = new Course();

        course.setCname("java");

        course.setUserId(10l);

        course.setCstatus("normal");

        courseMapper.insert(course);

    }

    @Test

    public void find(){

        QueryWrapper<Course> wrapper = new QueryWrapper<>();

        wrapper.eq("cid",714887289653690369l);

        Course course = courseMapper.selectOne(wrapper);

        System.out.println(course);

    }

}

springboot版本不同可能会报错,由于一个实体类对应两个数据库,在配置文件添加

spring.main.allow-bean-definition-overriding=true

水平分库

创建两个数据库,每个数据库里创建两个表分别为course_1 和 course_2

约定分片规则:userid为偶数加入第一个数据库,cid为偶数放入course_1表

修改之前代码:配置分片规则,在配置文件中加入


# 配置数据源,给数据源起名称,

# 水平分库,配置两个数据源

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 

 

# 指定表分片策略 约定 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} 

 

# 指定数据库分片策略 约定 user_id 是偶数添加 m1,是奇数添加 m2

#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}

#写法二 对具体表有固定规则

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}

测试同上

垂直分库

专库专用,查询用户信息时去用户数据库查用户表

创建数据库user_db,创建用户信息表t_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> {

}

配置文件添加


# 配置数据源,给数据源起名称

spring.shardingsphere.datasource.names=m1,m2,m0

.....

#配置第三个数据源具体内容,包含连接池,驱动,地址,用户名和密码

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 

 

# 指定 course 表里面主键 cid 生成策略 SNOWFLAKE

spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id 

spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE 



最后总结

搞定算法,面试字节再不怕,有需要文章中分享的这些二叉树、链表、字符串、栈和队列等等各大面试高频知识点及解析

最后再分享一份终极手撕架构的大礼包(学习笔记):分布式+微服务+开源框架+性能优化

image

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

搞定算法,面试字节再不怕,有需要文章中分享的这些二叉树、链表、字符串、栈和队列等等各大面试高频知识点及解析

最后再分享一份终极手撕架构的大礼包(学习笔记):分布式+微服务+开源框架+性能优化

[外链图片转存中…(img-7x2PbZln-1715468251779)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot整合ShardingSphere,实现数据库分库分表的步骤如下: 1. 引入ShardingSphere的相关依赖: ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>${shardingsphere.version}</version> </dependency> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${shardingsphere.version}</version> </dependency> ``` 2. 配置ShardingSphere的数据源 在application.yml中进行配置,示例代码: ```yaml spring: shardingsphere: datasource: names: ds0, ds1 ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/test0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8 username: root password: root ds1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/test1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8 username: root password: root sharding: tables: order: actualDataNodes: ds$->{0..1}.order_$->{0..1} tableStrategy: inline: shardingColumn: order_id algorithmExpression: order_$->{order_id % 2} keyGenerateStrategy: column: order_id keyGeneratorName: snowflake default-key-generator: type: SNOWFLAKE worker-id: 123 ``` 其中,`names`字段指定数据源的名称,`ds0`和`ds1`则为具体的数据源配置,`actualDataNodes`字段指定了数据的实际节点,`tableStrategy`字段指定了分策略,`keyGenerateStrategy`字段指定了键生成策略。 3. 配置ShardingSphere的规则 ```yaml spring: shardingsphere: sharding: default-database-strategy: inline: shardingColumn: user_id algorithmExpression: ds$->{user_id % 2} ``` 其中,`default-database-strategy`字段指定了分库策略,`inline`示使用取模算法进行分库,`shardingColumn`字段指定了分库的列名。 4. 在代码中使用 在代码中使用时,只需要正常使用JPA或Mybatis等ORM框架即可。ShardingSphere会自动根据配置进行数据分片。 以上就是Spring Boot整合ShardingSphere实现数据库分库分表的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值