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,經測試
4.0.0-RC2會有分表規(guī)則不生交往的情況
4.1.1會有自定義主鍵生成SPI的問題
配置:
# shardingjdbc分片策略
# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名稱
spring.shardingsphere.datasource.names=m1
# 一個實體類對應兩張表概耻,覆蓋
spring.main.allow-bean-definition-overriding=true
#配置數(shù)據(jù)源具體內容母剥,包含連接池十嘿,驅動箭养,地址旁壮,用戶名和密碼
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
#指定表分布情況婿牍,配置表在哪個數(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.reibang.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出錯:{}", e.getMessage());
}
return null;
}
@Override
public String getType() {
return "MyShardingKey";
}
}
創(chuàng)建目錄:META/services
在其下建文件:org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
內容寫上上面自定義的MyShardingKeyGenerator的全類名
同時在pom文件里資源配置里,各個環(huán)境都需加上
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 資源根目錄排除各環(huán)境的配置等脂,使用單獨的資源目錄來指定 -->
<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踩坑實記
環(huán)境概況:
組件 版本
springboot 2.1.1.RELEASE
mybatis-plus-boot-starter 3.0.7.1
sharding-jdbc-spring-boot-starter 4.0.0-RC1
踩坑記錄:
報錯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
實際執(zhí)行的sql與邏輯sql相同俏蛮,沒有起到分表作用
解決方案:將sharding-jdbc-spring-boot-starter的版本號改為4.0.0-RC1就正常了(RC2和RC3均異常,很無奈上遥,不知道是不是我這邊環(huán)境有問題)
sql中含有now()函數(shù)調用搏屑,程序報錯
解決方案:service生成時間戳傳給dao,sql中直接參數(shù)替換粉楚,剔除now函數(shù)(看來是組件還不支持這個函數(shù)解析)
報sql表達式錯誤辣恋,表中含有字段“desc”,因為desc同樣為mysql關鍵字,導致insert時模软,sql解析出問題伟骨,影響了最終參數(shù)設置,程序報錯燃异。(update/select時都不會影響携狭,唯有insert時出錯)
解決方案:將參數(shù)改為description,避開mysql關鍵字
報錯缺少antlr4-runtime
解決方案:加入依賴
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.1</version>
</dependency>
報錯沒有收到mysql server回應回俐,感覺就是沒連上mysql
解決方案:忘了…(我記得我把maven依賴整體了一下逛腿,無用的依賴剔除掉稀并,貌似就可以了)
寫了幾個demo,如有需要可以自行獲鹊ツ:https://github.com/naget/sharding
原文鏈接:https://blog.csdn.net/qq_33240946/article/details/103506983