桥下红药

通往财富的捷径,就是找到你的牧场和羊群
  1. 首页
  2. Java
  3. 正文

Shardingsphere + Springboot 分表

2022年8月3日

之前写的一个小说采集框架,章节数据多大8千万条,时隔几个月又对采集器重构了下,添加了可选分表功能。 记录下  Shardingsphere JDBC 5.x  配置教程。

# build.gradle.kts

implementation("org.apache.shardingsphere:shardingsphere-jdbc-core-spring-boot-starter:5.1.2"){
     // 如果外部已经依赖了 okhttp 可以排除 防止版本不一致冲突
     //exclude("com.squareup.okhttp3","okhttp")
}

runtimeOnly("mysql:mysql-connector-java")

// Mybatis 可以省略
// implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2")
// implementation("com.baomidou:mybatis-plus-generator:3.5.2")
// implementation("com.baomidou:mybatis-plus-boot-starter:3.5.2")

# application.properties

spring.shardingsphere.enabled=true
spring.shardingsphere.mode.type=Memory
spring.shardingsphere.datasource.names=master
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://127.0.0.1:3306/bookstore_test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=root

spring.shardingsphere.rules.sharding.tables.chapter.actual-data-nodes=master.chapter_$->{1..2}
spring.shardingsphere.rules.sharding.tables.chapter.table-strategy.standard.sharding-column=id

spring.shardingsphere.rules.sharding.tables.chapter.table-strategy.standard.sharding-algorithm-name=table-chapter-inline

# 给分表的ID字段设置自动创建的算法
spring.shardingsphere.rules.sharding.tables.chapter.key-generate-strategy.column=id
spring.shardingsphere.rules.sharding.tables.chapter.key-generate-strategy.key-generator-name=snowflake

spring.shardingsphere.rules.sharding.sharding-algorithms.table-chapter-inline.type=INLINE

spring.shardingsphere.rules.sharding.sharding-algorithms.table-chapter-inline.props.algorithm-expression=chapter_$->{id % 2+1}

spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE

 

1、依赖 shardingsphere  必须 spring.shardingsphere.enabled=false 才能使用 Springboot 的数据源配置。

2、shardingsphere  开启的状态下,如果不想分表只需要把分表配置取消即可。

标签: 暂无
最后更新:2022年8月3日
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

COPYRIGHT © 2022 桥下红药. ALL RIGHTS RESERVED.

皖ICP备15003861号-1