wordpress老站开启多站点,凡客科技,网上推广怎么做,68网页设计欣赏SpringBoot整合Sharding-Jdbc
Sharding-Jdbc
sharding-jdbc是客户端代理的数据库中间件#xff1b;它和MyCat最大的不同是sharding-jdbc支持库内分表。
整合
数据库环境
在两台不同的主机上分别都创建了sharding_order数据库#xff0c;库中都有t_order_1和t_order_2两张…SpringBoot整合Sharding-Jdbc
Sharding-Jdbc
sharding-jdbc是客户端代理的数据库中间件它和MyCat最大的不同是sharding-jdbc支持库内分表。
整合
数据库环境
在两台不同的主机上分别都创建了sharding_order数据库库中都有t_order_1和t_order_2两张表。
sharding-jdbc依赖如下 dependencygroupIdorg.apache.shardingsphere/groupIdartifactIdsharding-jdbc-spring-boot-starter/artifactIdversion4.1.1/version/dependencyapplication.properties配置文件
在application.properties配置文件中进行数据源配置、分库分表规则配置
#shardingsphere数据源配置
spring.shardingsphere.datasource.namesds0,ds1#数据源配置
spring.shardingsphere.dataSource.ds0.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.dataSource.ds0.driver-class-namecom.mysql.jdbc.Driver
spring.shardingsphere.dataSource.ds0.jdbcUrljdbc:mysql://192.168.200.215:3306/sharding_order?serverTimezoneUTCuseSSLfalseuseUnicodetruecharacterEncodingUTF-8
spring.shardingsphere.dataSource.ds0.usernameroot
spring.shardingsphere.dataSource.ds0.passwordrootspring.shardingsphere.dataSource.ds1.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.dataSource.ds1.driver-class-namecom.mysql.jdbc.Driver
spring.shardingsphere.dataSource.ds1.jdbcUrljdbc:mysql://192.168.200.225:3306/sharding_order?serverTimezoneUTCuseSSLfalseuseUnicodetruecharacterEncodingUTF-8
spring.shardingsphere.dataSource.ds1.usernameroot
spring.shardingsphere.dataSource.ds1.passwordroot#shardingsphere数据节点分片规则
spring.shardingsphere.sharding.tables.t_order.actual-data-nodesds$-{0..1}.t_order_$-{1..2}#数据源分片规则决定数据落在哪个库
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-columnuser_id
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expressionds$-{user_id % 2}#表分片规则决定数据落在哪个表
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-columnid
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expressiont_order_$-{id % 2 1}#逻辑表mybatis中查询的时候需要使用逻辑表查询不能使用具体的分片表
spring.shardingsphere.sharding.tables.t_order.logic-tablet_order测试 /*** 测试分库分表*/Testpublic void test4() {Order order new Order();order.setId(5);order.setUserId(2);order.setOrderStatus(1);order.setOrderAmount(BigDecimal.valueOf(3000));orderMapper.insert(order);}数据落在了215这台主机上的sharding_order数据库中的t_order_2表中证明分库分表成功了。
分布式全局id设置
由于有两张order表t_order_1和t_order2而且在单表中我们往往会设置主键自增那么就会出现表中的id重复的问题sharding-jdbc支持使用通过雪花算法生成uuid作为主键来解决这个问题。
application.properties文件
#表分片规则
#标准表达式
spring.shardingsphere.sharding.tables.t_order.table-strategy.standard.sharding-columnid
spring.shardingsphere.sharding.tables.t_order.table-strategy.standard.precise-algorithm-class-namecom.sharding.jdbc.sharding.MySharding#id生成规则,分布式全局id
spring.shardingsphere.sharding.tables.t_order.key-generator.columnid
spring.shardingsphere.sharding.tables.t_order.key-generator.typeUUID分片规则
/*** author DELL* version 1.0* description精确分片规则* date 2023/11/26 13:34:36*/
public class MySharding implements PreciseShardingAlgorithmString {/**** param collection表名* param preciseShardingValue生成的uuid、逻辑表、字段名* return*/Overridepublic String doSharding(CollectionString collection, PreciseShardingValueString preciseShardingValue) {String id preciseShardingValue.getValue();int mode id.hashCode() % collection.size();String[] strings collection.toArray(new String[0]);System.out.println(strings Arrays.toString(strings));return strings[Math.abs(mode)];}
}测试生成分布式id
这里要注意的是由于这里的id是sharding-jdbc帮我们生成的所以调用mapper中方法的时候不能指定id否则不会自动生成。 /*** 测试生成uuid*/Testpublic void test5() {Order order new Order();order.setUserId(3);order.setOrderStatus(1);order.setOrderAmount(BigDecimal.valueOf(3000));orderMapper.insert(order);}如果报了这个错大概率就是sql中指定了id。
Sharding value must implements Comparable.uui的是有可能重复的这里可以指定使用雪花算法生成uuid。雪花算法配置如下
spring.shardingsphere.sharding.tables.t_order.key-generator.columnid
spring.shardingsphere.sharding.tables.t_order.key-generator.typeSNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.key-generator.props.worker.id345分片规则中的String需要改成Long。
参考 Sharding-JDBC官网-配置手册ShardingSphere报Sharding value must implements Comparable.的解决过程