话不多说,大家可能就是卡在配置文件和依赖库上了,各种失败都是这两个问题引起的
下面是配置文件,application.yml
spring:
shardingsphere:
props:
# 是否显示sql
sql-show: false
check:
table:
metadata:
enabled: true
datasource:
names: ds0
ds0:
url: jdbc:mysql://192.169.133.103:3312/xxxx?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=true
username: xxxx
password: xxxxx
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
rules:
sharding:
key-generators:
snowflake:
type: SNOWFLAKE
sharding-algorithms:
ota-strategy-inline:
props:
strategy: standard
# 自定义标准分配算法
algorithmClassName: com.school.facesharding.algorithm.OTAStrategyShardingAlgorithm
type: CLASS_BASED
tables:
#逻辑表 下面是节点表,分表后还有数据在原来的表,所有查询节点需要加上原来的表
orders:
actual-data-nodes: ds0.orders_202$->{308..312}
key-generate-strategy:
column: id
key-generator-name: snowflake
# 配置分表策略
table-strategy:
#分片策略 以创建时候分表,实现类计算
standard:
sharding-column: createtime
#对应下面的分表策略类
sharding-algorithm-name: ota-strategy-inline
# OTA升级策略表水平分表 ======================================================
pom.xml文件,这个成功与否跟springboot版本关系非常大,我的springboot版本是:2.6.13
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--shardingsphere end-->