原文地址: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生成器
由於需要主從同步,分庫就沒做,這里貼一下分表的配置
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
