springboot shardingjdbc与druid数据源冲突解决

首先看错误信息

cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]: Invocation of init method failed; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

如上所示,DruidDataSourceAutoConfigure Failed to determine a suitable driver class,即druid找不到mysql driver,然而mysql的驱动包啥的都没问题,于是直接点进DruidDataSourceAutoConfigure查看源码

如上,标红表明druid是根据spring.datasource.druid找jdbc属性的,如果not found,则根据spring.datasource找jdbc属性,一般而言这是不会出现错误的。但是我这里使用了shardingjdbc,配置如下:

spring:
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://xxx:3306/ds0
        username: xxx
        password: xxx
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://xxx:3306/ds1
        username: xxx
        password: xxx
    sharding:
      tables:
        user:
          actual-data-nodes: ds${0..1}.user${0..1}
          database-strategy:
            inline:
              sharding-column: id
              algorithm-expression: ds${id % 2}
          table-strategy:
            inline:
              sharding-column: id
              algorithm-expression: user$->{id % 2}
          key-generator:
            type: SNOWFLAKE
            column: order_id
    props:
      sql:
        show: true
      executor:
        size: 12

就很显然了,他根据spring.datasource.druid或者spring.datasource确实找不到,因为我的结构是spring.shardingsphere.datasource。

解决方式1:

如果我们用的jar包是druid-spring-boot-starter,则在启动类上排除druid自动配置

@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})

解决方式2:

不用

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.22</version>
</dependency>

改为

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.22</version>
</dependency>

即可

 

  • 14
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Spring Boot是一个用于构建独立的、可执行的Spring应用程序的框架,简化了Spring应用程序的配置和部署。JPA(Java Persistence API)是一种用于管理Java对象和关系数据库之间映射的规范。Druid是阿里巴巴开源的关系型数据库连接池。 在Spring Boot中配置多数据源需要以下几步: 1. 引入相关依赖:需要引入Spring Boot、Spring Data JPA和Druid的相关依赖。 2. 配置数据源:在application.properties或application.yml文件中配置多个数据源的连接信息,并指定每个数据源的名称和相关属性。 3. 配置数据源连接池:使用@ConfigurationProperties注解创建多个数据源的连接池对象,并指定数据源的名称以及相关属性。 4. 配置实体管理器工厂:为每个数据源配置对应的实体管理器工厂,用于处理JPA实体与数据库之间的映射关系。 5. 配置事务管理器:为每个数据源配置对应的事务管理器,用于处理事务操作。 6. 配置数据源路由:创建动态数据源,根据传入的数据源名称选择对应的数据源进行操作。 7. 配置JPA的Repository:创建接口继承JpaRepository,用于定义数据访问方法。 通过以上步骤配置好多数据源后,就可以在Spring Boot应用程序中使用多个数据源进行数据库的操作。可以根据需要在Service或Controller中使用@PersistenceContext注解指定具体的数据源,或者使用@Primary注解指定默认的数据源。 总结:通过Spring Boot的自动配置和Druid的连接池,可以很方便地实现多数据源的配置。使用JPA进行数据操作,能够有效地减少开发人员编写SQL语句的工作量,提高开发效率。通过合理的配置,可以根据需要选择不同的数据源进行操作,实现灵活的数据访问。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值