最近业务关系需要用到分库分表,所以学习了sharding-jdbc,与诸君共勉。
ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(计划中)这3款相互独立的产品组成。 他们均提供标准化的数据分片、分布式事务和数据库治理功能,可适用于如Java同构、异构语言、容器、云原生等各种多样化的应用场景。
ShardingSphere定位为关系型数据库中间件,旨在充分合理地在分布式的场景下利用关系型数据库的计算和存储能力,而并非实现一个全新的关系型数据库。 它与NoSQL和NewSQL是并存而非互斥的关系。NoSQL和NewSQL作为新技术探索的前沿,放眼未来,拥抱变化,是非常值得推荐的。反之,也可以用另一种思路看待问题,放眼未来,关注不变的东西,进而抓住事物本质。 关系型数据库当今依然占有巨大市场,是各个公司核心业务的基石,未来也难于撼动,我们目前阶段更加关注在原有基础上的增量,而非颠覆。
以上引入的是概览 :: ShardingSphere
详细了解每块内容可以去看下,不多做阐述。
maven配置的jar包:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
使用sharding-jdbc,最主要的就是properties文件的配置,通过配置项来做到不同的分库分表,读写分离等操作的;
主要来理解理解sharding-jdbc的配置文件,
# 数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
针对分库操作而言,还需要分主数据源和从数据源
例如:
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=123456
对于分表分库而言需要:
绑定表所在节点
spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
绑定其他表所在节点
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
分表配置:
# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user_${0..3}
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}
读写分离配置:
# 读写分离配置
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=slave
# 分库配置
spring.shardingsphere.sharding.default-database-strategy.standard.sharding-column=id
spring.shardingsphere.sharding.default-database-strategy.standard.precise-algorithm-class-name=com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm
分库配置需要注意一点的是需要写配置类,就是自定义分片算法:
/**
* 自定义分片算法
*
* @author yinjihuan
*
*/
public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
private static List<ShardingRangeConfig> configs = new ArrayList<>();
static {
ShardingRangeConfig config = new ShardingRangeConfig();
config.setStart(1);
config.setEnd(30);
config.setDatasourceList(Arrays.asList("ds0", "ds1"));
configs.add(config);
config = new ShardingRangeConfig();
config.setStart(31);
config.setEnd(60);
config.setDatasourceList(Arrays.asList("dss0", "dss1"));
configs.add(config);
}
@Override
public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
Optional<ShardingRangeConfig> configOptional = configs.stream().filter(
c -> shardingValue.getValue() >= c.getStart() && shardingValue.getValue() <= c.getEnd()).findFirst();
if (configOptional.isPresent()) {
ShardingRangeConfig rangeConfig = configOptional.get();
for (String ds : rangeConfig.getDatasourceList()) {
if (ds.endsWith(shardingValue.getValue() % 2 + "")) {
System.err.println(ds);
return ds;
}
}
}
throw new IllegalArgumentException();
}
}
主要就是通过算法,定义数据写入对应的数据库中。
其他配置项就没啥了,和spring cloud配置是一样的了