使用shardingsphere 4.x 和 springboot3.x 遇到了 不生效问题
框架 | 版本 |
---|---|
Springboot | 3.1.0 |
Sharding-JDBC | 4.1.1 |
Mybatis | 3.5.3.1 |
控制台启动 抛出异常
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-07-28T08:19:56.199+08:00 ERROR 20600 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
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
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
看的出来 意思就是没有配置数据源
但是我在配置文件中配置了
配置一定是没有问题的 **(因为我改用springboot2)
shardingsphere
是可以正常 运行成功并且sql会被拦截
网上也没有类似的问题 即使我在启动类加了 也无法启动成功
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
回到刚才抛出的异常
***************************
APPLICATION FAILED TO START
***************************
Description:
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
提示没有数据源
既然没有数据源那就自己配置一个
启动成功
但是会发现 控制台并没有shardingsphere 的控制输出 也就意味着没有被sharding-jdbc 接管
也尝试过 吧sharding 改成 5.x的版本已经不行
然后以下是我的 application.yml配置
server:
port: 8085
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
# 参数配置,显示sql
props:
sql:
show: true
# 配置数据源
datasource:
# 给每个数据源取别名,下面的ds1,ds1任意取名字
names: ds0,ds1
# 给master-ds1每个数据源配置数据库连接信息
ds0:
# 配置druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.31.105:3307/taskDown?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: POIASD520.x
maxPoolSize: 100
minPoolSize: 5
# 配置默认数据源ds0
sharding:
# 默认数据源,主要用于写,注意一定要配置读写分离 ,注意:如果不配置,那么就会把三个节点都当做从slave节点,新增,修改和删除会出错。
default-data-source-name: ds0
# 配置分表的规则
tables:
# user 逻辑表名 逻辑业务 使用user 最终会被改写成物理中对应的表
user:
# 数据节点:数据源$->{0..N}.逻辑表名$->{0开头..N结尾}
# actual-data-nodes: ds$->{0..1}.user$->{1..2}
actual-data-nodes: ds$->{0}.user$->{1..2}
# 拆分库策略,也就是什么样子的数据放入放到哪个数据库中。
database-strategy:
# inline:
# sharding-column: age # 分片字段(分片键)
# algorithm-expression: ds1 # 分片算法表达式
standard:
sharding-column: age
precise-algorithm-class-name: com.example.algorithm.MyPreciseDsShardingAlgorithm
range-algorithm-class-name: com.example.algorithm.MyRangeDsShardingAlgorithm
# 拆分表策略,也就是什么样子的数据放入放到哪个数据表中。
table-strategy:
# inline:
# sharding-column: age # 分片字段(分片键)
# algorithm-expression: user$->{age % 2 + 1} # 分片算法表达式
standard:
sharding-column: age
precise-algorithm-class-name: com.example.algorithm.MyPreciseTableShardingAlgorithm
range-algorithm-class-name: com.example.algorithm.MyRangeTableShardingAlgorithm
# sharding-column: age
# precise-algorithm-class-name: com.project.com.PreciseModuloAlgorithm
# #精确分片算法类名称,用于=和IN。。该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
# range-algorithm-class-name: com.project.com.component.RangeModuloAlgorithm
# #范围分片算法类名称,用于BETWEEN,可选。该类需实现RangeShardingAlgorithm接口并提供无参数的构造器
# datasource:
# driver-class-name: com.mysql.cj.jdbc.Driver
# password: POIASD520.x
# username: root
# url: jdbc:mysql://192.168.31.105:3307/taskDown?useUnicode=true&characterEncoding=UTF-8
# hikari:
# idle-timeout: 180000
# max-lifetime: 180000
# connection-timeout: 30000
# connection-test-query: SELECT 1
# auto-commit: true
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.bean
pom 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.10-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>hg</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hg</name>
<description>hg</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--依赖lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 依赖mybatis和mysql驱动 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!--sharding jdbc -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!--依赖数据源druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>