springboot shardingjdbc與druid數(shù)據(jù)源沖突問(wèn)題及解決
首先看錯(cuò)誤信息
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的驅(qū)動(dòng)包啥的都沒(méi)問(wèn)題,于是直接點(diǎn)進(jìn)DruidDataSourceAutoConfigure查看源碼
如上,標(biāo)紅表明druid是根據(jù)spring.datasource.druid找jdbc屬性的,如果not found,則根據(jù)spring.datasource找jdbc屬性,一般而言這是不會(huì)出現(xiàn)錯(cuò)誤的。
但是我這里使用了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
就很顯然了,他根據(jù)spring.datasource.druid或者spring.datasource確實(shí)找不到,因?yàn)槲业慕Y(jié)構(gòu)是spring.shardingsphere.datasource。
解決方式1
如果我們用的jar包是druid-spring-boot-starter,則在啟動(dòng)類上排除druid自動(dòng)配置
@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>
即可~
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Maven添加Tomcat插件實(shí)現(xiàn)熱部署代碼實(shí)例
這篇文章主要介紹了Maven添加Tomcat插件實(shí)現(xiàn)熱部署代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java實(shí)現(xiàn)非法訪問(wèn)異常示例
創(chuàng)建ExceptionTest類,在該類的main()方法中,使用反射獲得String類的所有域,不要使用setAccessible方法修改這些域的可見(jiàn)性,然后通過(guò)反射獲得私有域中與”hash”相匹配的int值,并使用try-catch語(yǔ)句塊捕獲非法訪問(wèn)異常2014-02-02SpringBoot整合Mybatis與thymleft實(shí)現(xiàn)增刪改查功能詳解
MybatisPlus是國(guó)產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動(dòng)。本文將整合MybatisPlus實(shí)現(xiàn)增刪改查功能,感興趣的可以了解一下2022-12-12idea解決springboot中的依賴版本沖突問(wèn)題
這篇文章主要介紹了idea解決springboot中的依賴版本沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Springboot使用RestTemplate調(diào)用第三方接口的操作代碼
這篇文章主要介紹了Springboot使用RestTemplate調(diào)用第三方接口,我只演示了最常使用的請(qǐng)求方式get、post的簡(jiǎn)單使用方法,當(dāng)然RestTemplate的功能還有很多,感興趣的朋友可以參考RestTemplate源碼2022-12-12Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解
這篇文章主要為大家介紹了Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08