SpringBoot2 整合sharding-jdbc 启动报错,坑多多
原文地址:https://www.520mwx.com/view/93143
1、查看项目是否引入自动配置的数据源(比如druid-spring-boot-starter),如果有,先删除,sharding启动时会自动创建数据源,两者冲突。如果还是想用druid链接池。引入:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
或者在启动类上,排出
DruidDataSourceAutoConfigure,
后者在java类中配置一个sharding-DataSource,加上@Primary注解,反正就是别让他自己创建就行,
2、报错:
Caused by: java.sql.SQLFeatureNotSupportedException: getCatalog
at org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationConnection.getCatalog(AbstractUnsupportedOperationConnection.java:91)
at org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource.isTableExist(EncryptDataSource.java:88)
查看是否使用了flywaydb,如果有,删除掉,这个github上有人提了issues,可还是没有解决,参考这个,https://github.com/apache/shardingsphere/issues/2488
3、使用JPA-Hibernate的时候,id不要用数据库自增的方式,可以使用hutool工具包提供的Idutil生成器
由于需要主从同步,分库就没做,这里贴一下分表的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | spring.shardingsphere.datasource.names=framework spring.shardingsphere.datasource.framework.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.framework.driver- class -name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.framework.url= spring.shardingsphere.datasource.framework.username= spring.shardingsphere.datasource.framework.password= #article spring.shardingsphere.sharding.tables.article.actual-data-nodes=framework.article$->{ 0 .. 4 } spring.shardingsphere.sharding.tables.article.table-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.article.table-strategy.inline.algorithm-expression=article$->{id % 5 } spring.shardingsphere.sharding.tables.article_item.actual-data-nodes=framework.article$->{ 0 .. 4 } spring.shardingsphere.sharding.tables.article_item.table-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.article_item.table-strategy.inline.algorithm-expression=article$->{id % 5 } #articles_attribute spring.shardingsphere.sharding.tables.articles_attribute.actual-data-nodes=framework.articles_attribute$->{ 0 .. 4 } spring.shardingsphere.sharding.tables.articles_attribute.table-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.articles_attribute.table-strategy.inline.algorithm-expression=articles_attribute$->{id % 5 } spring.shardingsphere.sharding.tables.articles_attribute_item.actual-data-nodes=framework.articles_attribute$->{ 0 .. 4 } spring.shardingsphere.sharding.tables.articles_attribute_item.table-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.articles_attribute_item.table-strategy.inline.algorithm-expression=articles_attribute$->{id % 5 } spring.shardingsphere.props.sql.show= true |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】