ShardingSphere之未分片表配置实战(六)

概述

场景

在实际工作中,有些表需要参与分片操作,有些表则无需分片,那么这种场景下,sharding-jdbc该如何配置呢?

SpringBoot环境搭建

参考ShardingSphere之水平分表实战(三)

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);
    }

参考

Sharding-jdbc设置defaultDatasource无效问题解决和源码分析解决记录

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
ShardingSphere5.2是一个开源的分布式数据库中间件,可以用于在分布式系统中进行数据分片处理。在ShardingSphere5.2中,我们可以配置自定义的分片算法来实现自定义的数据分片策略。 首先,我们需要创建一个自定义的分片算法类。这个类需要实现ShardingAlgorithm接口,并重写其中的方法。ShardingAlgorithm接口包含了分片算法的两个核心方法:doSharding和init。 在doSharding方法中,我们可以编写自己的分片逻辑。我们可以根据业务需求来判断数据应该分配到哪个分片上,然后返回对应的分片信息。在这个方法中,我们可以使用一些参数来获取到分片规则、分片键值和分片配置等信息。 在init方法中,我们可以进行一些初始化操作。比如,可以读取和解析配置文件,获取到分片的规则和配置信息,并进行一些初始化设置。 接下来,我们需要在ShardingSphere的配置文件中指定使用我们的自定义分片算法。我们可以在配置文件中通过配置项指定自定义分片算法的类名。同时,我们还需要配置其他相关的分片规则、分布式数据源等信息。 最后,我们将整个配置文件加载到ShardingSphere中,并启动分片功能。当系统需要进行数据分片时,ShardingSphere会自动调用我们的自定义分片算法来进行数据分片处理。 总结来说,ShardingSphere5.2配置自定义分片算法的步骤包括:创建自定义的分片算法类并实现ShardingAlgorithm接口、编写自己的分片逻辑、在配置文件中指定自定义分片算法、加载配置文件到ShardingSphere并启动分片功能。通过这些步骤,我们就可以实现自定义的数据分片策略。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

融极

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值