pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
這里采用的版本是4.0.0-RC1,經(jīng)測試
4.0.0-RC2會有分表規(guī)則不生交往的情況
4.1.1會有自定義主鍵生成SPI的問題
配置:
# shardingjdbc分片策略
# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名稱
spring.shardingsphere.datasource.names=m1
# 一個(gè)實(shí)體類對應(yīng)兩張表,覆蓋
spring.main.allow-bean-definition-overriding=true
#配置數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動,地址,用戶名和密碼
spring.shardingsphere.datasource.m1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.jdbc-url=jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
spring.shardingsphere.datasource.m1.username=uuu
spring.shardingsphere.datasource.m1.password=ppp
#指定表分布情況,配置表在哪個(gè)數(shù)據(jù)庫里面,表名稱都是什么 m1.course_1 , m1.course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{1..64}
# 指定表里面主鍵生成策略SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=id
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
# 指定分片策略
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=corp_id
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course$->{corp_id % 64 + 1}
# 打開sql輸出日志
spring.shardingsphere.props.sql.show=true
參考: http://www.lxweimin.com/p/a58c3b1abb9a
druid:
spring.shardingsphere.datasource.names=m1
spring.main.allow-bean-definition-overriding=true
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.shardingsphere.datasource.m1.username=rrrr
spring.shardingsphere.datasource.m1.password=___rM11xI2Pemzr6EerD4xY1A==___
spring.shardingsphere.datasource.m1.connection-properties=password=___rM11xI2Pemzr6EerD4xY1A==___
spring.shardingsphere.datasource.m1.password-callback-class-name=cn.xxx.ddd.callback.DBPasswordCallback
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course$->{1..64}
spring.shardingsphere.sharding.tables.course.key-generator.column=id
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=corp_id
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course$->{corp_id % 64 + 1}
spring.shardingsphere.props.sql.show=true
自定義主鍵生成類:
spring.shardingsphere.sharding.tables.t_chat_group.key-generator.column=id
#spring.shardingsphere.sharding.tables.t_chat_group.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_chat_group.key-generator.type=MyShardingKey
@Data
@Slf4j
public class MyShardingKeyGenerator implements ShardingKeyGenerator {
private Properties properties;
@Override
public Comparable<?> generateKey() {
try {
return SnowflakeIdUtils.generateId();
} catch (Exception e) {
log.error("生成雪花id出錯(cuò):{}", e.getMessage());
}
return null;
}
@Override
public String getType() {
return "MyShardingKey";
}
}
創(chuàng)建目錄:META/services
在其下建文件:org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
內(nèi)容寫上上面自定義的MyShardingKeyGenerator的全類名
同時(shí)在pom文件里資源配置里,各個(gè)環(huán)境都需加上
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 資源根目錄排除各環(huán)境的配置,使用單獨(dú)的資源目錄來指定 -->
<includes>
<include>*/*</include>
<include>META-INF/dubbo/com.alibaba.dubbo.rpc.Filter</include>
<include>META-INF/app.properties</include>
<include>META-INF/services/*</include>
<include>static/**</include>
</includes>
<!-- 是否替換資源中的屬性 -->
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
shardingsphere踩坑實(shí)記
環(huán)境概況:
組件 版本
springboot 2.1.1.RELEASE
mybatis-plus-boot-starter 3.0.7.1
sharding-jdbc-spring-boot-starter 4.0.0-RC1
踩坑記錄:
報(bào)錯(cuò)datasource不能完成注冊(The bean ‘dataSource’, defined in class path resource [io/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered.)
解決方案:根據(jù)提示加入配置spring.main.allow-bean-definition-overriding=true
實(shí)際執(zhí)行的sql與邏輯sql相同,沒有起到分表作用
解決方案:將sharding-jdbc-spring-boot-starter的版本號改為4.0.0-RC1就正常了(RC2和RC3均異常,很無奈,不知道是不是我這邊環(huán)境有問題)
sql中含有now()函數(shù)調(diào)用,程序報(bào)錯(cuò)
解決方案:service生成時(shí)間戳傳給dao,sql中直接參數(shù)替換,剔除now函數(shù)(看來是組件還不支持這個(gè)函數(shù)解析)
報(bào)sql表達(dá)式錯(cuò)誤,表中含有字段“desc”,因?yàn)閐esc同樣為mysql關(guān)鍵字,導(dǎo)致insert時(shí),sql解析出問題,影響了最終參數(shù)設(shè)置,程序報(bào)錯(cuò)。(update/select時(shí)都不會影響,唯有insert時(shí)出錯(cuò))
解決方案:將參數(shù)改為description,避開mysql關(guān)鍵字
報(bào)錯(cuò)缺少antlr4-runtime
解決方案:加入依賴
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.1</version>
</dependency>
報(bào)錯(cuò)沒有收到mysql server回應(yīng),感覺就是沒連上mysql
解決方案:忘了…(我記得我把maven依賴整體了一下,無用的依賴剔除掉,貌似就可以了)
寫了幾個(gè)demo,如有需要可以自行獲取:https://github.com/naget/sharding
原文鏈接:https://blog.csdn.net/qq_33240946/article/details/103506983