Sharding-jdbc错误:Failed to configure a DataSource

目录
  1. 问题
  2. 解决

问题

Spring Boot项目集成Sharding-jdbc, 配置的是单库分表,正常添加了pom、配置文件。其中sharding-sphere使用了当前最新的版本: 4.1.1

1
2
3
4
5
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
spring:
shardingsphere:
datasource:
names: master
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxx:3306/xxx
username: xx
password: xx
sharding:
tables:
st:
key-generator:
column: id
type: SNOWFLAKE
actual-data-nodes: master.st_$->{0..3}
table-strategy:
inline:
sharding-column: st_id
algorithm-expression: st_$->{st_id % 4}

启动后报数据源相关错误
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

Sharding-jdbc url attribute is not specified

解决

参考网上一般的解决方案 升级POM版本 没有效果,看Issues( https://github.com/apache/shardingsphere/issues/3831 )里有提到排除相关boot-starter。

尝试将原先使用的pom

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>xxx</version>
</dependency>

改为

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>

成功启动。