SpringBoot 项目配置 Sharding-JDBC 报错:No implementation class load from SPI 'org.apache.shardingsphere.sharding.spi.ShardingAlgorithm' with type 'AUTO-INTERVAL' 的解决方案
该错误提示是因为 Sharding-JDBC 在加载 Sharding 算法时,没有找到对应的实现类。具体解决方案如下:
-
确认 Sharding-JDBC 的版本是否正确。需要使用 Sharding-JDBC 4.0 及以上版本。
-
确认依赖是否正确,需要引入 sharding-jdbc-core 和 sharding-jdbc-spring-boot-starter 两个依赖。例如:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
- 确认 Sharding-JDBC 的配置是否正确。例如:
spring:
shardingsphere:
datasource:
names: ds0,ds1
ds0:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test0?useSSL=false
username: root
password: root
ds1:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test1?useSSL=false
username: root
password: root
sharding:
tables:
user:
actual-data-nodes: ds$->{0..1}.user_$->{0..2}
table-strategy:
inline:
sharding-column: user_id
algorithm-expression: user_$->{user_id.longValue() % 3}
key-generator:
type: SNOWFLAKE
column: user_id
default-database-strategy:
inline:
sharding-column: user_id
algorithm-expression: ds$->{user_id.longValue() % 2}
sharding-algorithms:
inline:
type: INLINE
snowflake:
type: SNOWFLAKE
tddl:
type: TDDL
hint:
type: HINT
standard:
type: STANDARD
props:
sql:
show: true
- 确认 Sharding-JDBC 的扩展点是否齐全。需要引入所有 Sharding-JDBC 的 SPI 扩展点,例如:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core-spi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-transaction-spi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.1.1</version>
</dependency>
以上是解决该错误的一些基本方案,如果还没有解决问题,可以参考 Sharding-JDBC 的官方文档或者在 Sharding-JDBC 的社区提问。
原文地址: https://www.cveoy.top/t/topic/nTJP 著作权归作者所有。请勿转载和采集!