springboot + sharding-jdbc + myb
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会有分表规则不生交往的情况
4.1.1会有自定义主键生成SPI的问题
配置:
# shardingjdbc分片策略
# 配置数据源,给数据源起名称
spring.shardingsphere.datasource.names=m1
# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true
#配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
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
#指定表分布情况,配置表在哪个数据库里面,表名称都是什么 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
参考: https://www.jianshu.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";
}
}
创建目录:META/services
在其下建文件:org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
内容写上上面自定义的MyShardingKeyGenerator的全类名
同时在pom文件里资源配置里,各个环境都需加上
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->
<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踩坑实记
环境概况:
组件 版本
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.)
解决方案:根据提示加入配置spring.main.allow-bean-definition-overriding=true
实际执行的sql与逻辑sql相同,没有起到分表作用
解决方案:将sharding-jdbc-spring-boot-starter的版本号改为4.0.0-RC1就正常了(RC2和RC3均异常,很无奈,不知道是不是我这边环境有问题)
sql中含有now()函数调用,程序报错
解决方案:service生成时间戳传给dao,sql中直接参数替换,剔除now函数(看来是组件还不支持这个函数解析)
报sql表达式错误,表中含有字段“desc”,因为desc同样为mysql关键字,导致insert时,sql解析出问题,影响了最终参数设置,程序报错。(update/select时都不会影响,唯有insert时出错)
解决方案:将参数改为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