Likefr 2023-07-28 08:46 采纳率: 33.3%
浏览 62
已结题

Shardingsphere 整合 SpringBoot3.x 出现 无法启动 的问题

使用shardingsphere 4.x 和 springboot3.x 遇到了 不生效问题

框架版本
Springboot3.1.0
Sharding-JDBC4.1.1
Mybatis3.5.3.1

控制台启动 抛出异常

img

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

看的出来 意思就是没有配置数据源

但是我在配置文件中配置了

img

配置一定是没有问题的 **(因为我改用springboot2) shardingsphere 是可以正常 运行成功并且sql会被拦截

网上也没有类似的问题 即使我在启动类加了 也无法启动成功

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

img

回到刚才抛出的异常

***************************
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

提示没有数据源
既然没有数据源那就自己配置一个

img

启动成功

img

但是会发现 控制台并没有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>

为什么shardingsphere 不兼容 springboot3.x呢 已经折腾好几天了

  • 写回答

5条回答 默认 最新

  • 兴奋的大母鸡 2023-07-28 09:13
    关注

    我得复现你的问题才能帮你排查,你能把一份不包含敏感信息的demo发我一份嘛?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 8月5日
  • 已采纳回答 7月28日
  • 修改了问题 7月28日
  • 创建了问题 7月28日

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示