- 前言
一般mysql单表存储数据量小于500万左右或者容量小于2GB的时候,它的读写性能是最好的。但是当业务发展到一定程度,数据增长到一定程度的时候,我们就会发现为什么查询速度越来越慢了,甚至还会出现数据库挂了等其他一系列问题。那么此时我们就需要采取一些方案来解决这些问题,一般如下:
1.分库分表
2.读写分离
3.利用NoSql
这里我暂时只记录分库分表。
- 调研
一般业界用的比较多的是阿里的MyCat以及当当的Sharding-Jdbc。
MyCat比较适合大公司。他是架在应用层和数据库之间,做了一层代理和转发。对于业务上来说,并不需要关心数据怎么分库分表。MyCat单独的一个应用会专门维护这些数据。他回去拦截应用的sql并进行处理再转发出去。对于业务层几乎不需要改动,但是多了一层转发的链路。
Sharding-Jdbc比较适合中小型公司。虽然他只支持Mysql,但是他十分轻量,支持任何基于java的ORM框架以及三方的数据库连接池。他是以jar包的形式,因此非常灵活,性能高。虽然不用像Mycat需要多一层转发,但是每个项目都需要依赖这个jar包,同时在扩展数据库的时候避免不了部分数据的迁移。
- 选择
由于Sharding-Jdbc已经进入了Apache孵化器,其名为ShardingSphere。因此我这里直接用了ShardingSphere,可以更方便的进行运用。
官方文档:https://shardingsphere.apache.org/document/current/cn/overview/
- 操作
- 依赖jar包
如上文所有,每个项目都需要依赖相关jar包:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!-- for spring boot -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!-- for spring namespace -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.0.0-RC1</version>
</dependency>
- 配置
由于Apache进行了封装,因此只需要简单配置即可使用:
spring.shardingsphere.datasource.names=bill_0,bill_1
spring.shardingsphere.datasource.bill_0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.bill_0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.bill_0.url=jdbc:mysql://127.0.0.1:3306/bill_0?characterEncoding=utf8&useSSL=false
spring.shardingsphere.datasource.bill_0.username=root
spring.shardingsphere.datasource.bill_0.password=123456
spring.shardingsphere.datasource.bill_1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.bill_1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.bill_1.url=jdbc:mysql://127.0.0.1:3306/bill_1?characterEncoding=utf8&useSSL=false
spring.shardingsphere.datasource.bill_1.username=root
spring.shardingsphere.datasource.bill_1.password=123456
#spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id
#spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=bill_$->{id % 2}
spring.shardingsphere.sharding.default-database-strategy.complex.sharding-columns=user_id
spring.shardingsphere.sharding.default-database-strategy.complex.algorithm-class-name=com.hongkj.algorithm.CustomDataBaseAlgorithm
spring.shardingsphere.sharding.tables.bill_asset_user.actual-data-nodes=bill_$->{0..1}.bill_asset_user_$->{0..1}
#spring.shardingsphere.sharding.tables.bill_asset_user.table-strategy.inline.sharding-column=id
#spring.shardingsphere.sharding.tables.bill_asset_user.table-strategy.inline.algorithm-expression=bill_asset_user_$->{id % 2}
spring.shardingsphere.sharding.tables.bill_asset_user.table-strategy.complex.sharding-columns=balance_type
spring.shardingsphere.sharding.tables.bill_asset_user.table-strategy.complex.algorithm-class-name=com.hongkj.algorithm.CustomTableAlgorithm
#spring.shardingsphere.sharding.tables.bill_asset_user.key-generator.column=order_no
spring.shardingsphere.sharding.binding-tables=bill_asset_user
#用于单分片键的标准分片场景
#spring.shardingsphere.sharding.tables.bill_asset_user.database-strategy.standard.sharding-column= #分片列名称
#spring.shardingsphere.sharding.tables.bill_asset_user.database-strategy.standard.precise-algorithm-class-name= #精确分片算法类名称,用于=和IN。该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
#spring.shardingsphere.sharding.tables.bill_asset_user.database-strategy.standard.range-algorithm-class-name=
#利用雪花算法自增id
spring.shardingsphere.sharding.tables.bill_asset_user.key-generator.column=id
spring.shardingsphere.sharding.tables.bill_asset_user.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.bill_asset_user.key-generator.props.worker.id=123
spring.shardingsphere.sharding.tables.bill_asset_user.key-generator.props.max.tolerate.time.difference.milliseconds=0
#spring.shardingsphere.sharding.tables.bill_asset_user.key-generator.props.<property-name>= #属性配置, 注意:使用SNOWFLAKE算法,需要配置worker.id与max.tolerate.time.difference.milliseconds属性
spring.shardingsphere.props.sql.show=true
- 策略
一共提供了5种分片策略,如下:
1.StandardShardingStrategy--标准分片策略
2.ComplexShardingStrategy--复合分片策略(可由开发者自己实现算法)
3.InlineShardingStrategy-Inline表达式分片策略(最常见的就是取模)
4.HintShardingStrategy--通过Hint而非SQL解析的方式分片的策略
5.NoneShardingStrategy--不分片策略
我选择的最灵活的复合分片策略,这样可以自由的自己定义算法。具体实现如下:
public class CustomAlgorithm implements ComplexKeysShardingAlgorithm<String> {
[@Override](https://my.oschina.net/u/1162528)
public Collection<String> doSharding(Collection<String> collection, ComplexKeysShardingValue<String> complexKeysShardingValue) {
String userId = complexKeysShardingValue.getColumnNameAndShardingValuesMap().get("user_id").toArray()[0].toString();
for (String s : collection) {
int hash = userId.hashCode() % 2;
if(s.endsWith(hash + "")){
List<String> list = new ArrayList<>();
list.add(s);
return list;
}
}
throw new UnsupportedOperationException();
}
}
上面这个简单的算法,就已经实现了根据不同的用户id来取模,从而来选择不同的库或者表。
- 之后
这里只是先暂时实现了一个demo,但是分库分表很明显只是刚刚开始。接下去分布式锁,数据迁移等分库分表的相关问题都会蜂拥而至。接下去一点点记录吧!
未完,待续。。。若有问题,欢迎一起讨论,一起学习进步!