使用sharding

网友投稿 660 2022-11-14

使用sharding

使用sharding

前面的文章使用sharding-jdbc实现水平分表中详细记录了如何使用sharding-jdbc实现水平分表,即根据相应的策略,将一部分数据存入到表1中,一部分数据存入到表2中,逻辑上为同一张表,分表操作全部交由sharding-jdbc进行处理。

可能根据需要,还需要将一张表的数据拆分存入到多个数据库中,甚至多个数据库的多个表中,使用sharding-jdbc同样可以实现。

重复的篇幅则不再赘述,下面重点记录升级的过程。

分库分表策略:将id为偶数的存入到库1中,奇数存入到库2中,在每个库中,再根据学生的性别分别存到到表1和表2中。

新建两个数据库sharding_db1和sharding_db2,在两个数据库中在分别创建结构相同的两张表,student_1和student_2。

CREATE TABLE `NewTable` (

`ID` bigint(20) NOT NULL ,

`NAME` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL ,

`AGE` int(11) NOT NULL ,

`GENDER` int(1) NOT NULL ,

PRIMARY KEY (`ID`)

);

相比前面文章中,将gender性别字段设置成了int类型,方便根据性别再进行分表。

修改配置文件

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

# 配置Sharding-JDBC的分片策略

# 配置数据源,给数据源起名g1,g2...此处可配置多数据源

spring.shardingsphere.datasource.names=g1,g2

# 配置数据源具体内容:连接池,驱动,地址,用户名,密码

spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource

spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver

spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/sharding_db1?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

spring.shardingsphere.datasource.g1.username=root

spring.shardingsphere.datasource.g1.password=123456

spring.shardingsphere.datasource.g2.type=com.alibaba.druid.pool.DruidDataSourchttp://e

spring.shardingsphere.datasource.g2.driver-class-name=com.mysql.cj.jdbc.Driver

spring.shardingsphere.datasource.g2.url=jdbc:mysql://localhost:3306/sharding_db2?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

spring.shardingsphere.datasource.g2.username=root

spring.shardingsphere.datasource.g2.password=123456

# 配置数据库的分布,表的分布

spring.shardingsphere.sharding.tables.student.actual-data-nodes=g$->{1..2}.student_$->{1..2}

# 指定student表 主键gid 生成策略为 SNOWFLAKE

spring.shardingsphere.sharding.tables.student.key-generator.column=id

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

# 指定数据库分片策略 约定id值是偶数添加到sharding_db1中,奇数添加到sharding_db2中

spring.shardingsphere.sharding.tables.student.database-strategy.inline.sharding-column=id

spring.shardingsphere.sharding.tables.student.database-strategy.inline.algorithm-expression=g$->{id % 2 + 1}

# 指定表分片策略 约定gender值是0添加到student_1表,如果gender是1添加到student_2表

spring.shardingsphere.sharding.tables.shttp://tudent.table-strategy.inline.sharding-column=gender

spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_$->{gender % 2 + 1}

# 打开sql输出日志

spring.shardingsphere.props.sql.show=true

配置多个数据源时,使用逗号隔开,分别配置其属性。除了配置表分片策略,还需配置库分配策略。

测http://试类

@SpringBootTest

class ShardingJdbcDemoApplicationTests {

@Autowired

private StudentMapper studentMapper;

aDYtlHWAMq @Test

public void test01() {

for (int i = 0; i < 15; i++) {

Student student = new Student();

student.setName("wuwl");

studeaDYtlHWAMqnt.setAge(27);

student.setGender(i%2);

studentMapper.insert(student);

}

}

}

运行效果:

看样子是成功了,查看数据库数据。

sharding_db1.student_1:

sharding_db1.student_2:

sharding_db2.student_1:

sharding_db2.student_2:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SQL锁机制
下一篇:年终总结汇报大纲
相关文章

 发表评论

暂时没有评论,来抢沙发吧~