作者:小傅哥
博客:https://bugstack.cn
面试问:“你为什么要自研,研路由组市面不是不用有吗,怎么回答?” 可以从以下3个点解答;
接着问,你们为什么分库分表?
我们分库分表用的非常熟。但不能为了等到系统到了200万数据,才拆。那么工作量会非常大 我们的做法是,因为有成熟方案,所以前期就分库分表了。但,为了解释服务器空间。所以把分库分表的库,用服务器虚拟出来机器安装。这样即不过多的占用服务器资源,也方便后续数据量真的上来了,好拆分。
同时,Xxx系统,是瞬时峰值较高的系统,历史数据不一定多。所以我们希望,用户可以快速的检索到个人数据,做最优响应。因为大家都知道,Xxx这东西,push发完,基本就1~3分钟结束,10分钟人都没了。所以我们这也是做了分库分表的理由。
不过用和不用是一方面,会和不会是另外一方面。不能因为不会所以不用,不用是因为不同场景的所需。像 Apache ShardingSphere 在很多大的场景还是非常好用的,所以我们需要学习积累。积累不同的思想和设计,以及积累技术的运用。
本文的宗旨在于通过简单干净实践的方式教会读者,快速 Easy 的使用上 sharding-jdbc 这个笨重的大家伙!—— 这篇文章并不复杂,但市面上的案例,还真的很少有能拿过来就能运行起来的!
之所以说"笨重",是因为 Apache ShardingSphere 不只是简单意义上的路由组件,而是一款分布式 SQL 事务和查询引擎,可通过数据分片、弹性伸缩、加密等能力对任意数据库进行增强。同时它又在迭代过程中,衍生出了很多的版本,以及对应了不同的使用方式。并在 ShardingSphere 5.3 以后又做了不小的架构调整。所以很多伙伴在使用的时候,经常是找了一上午的资料,到下午下班还没对接上。
本文涉及的工程:
分库分表的本质是数据的散列,分摊数据库资源压力。如把原本在一台机器上的数据库存放1000万数据,分摊到n台机上,拆分这1000万的数据和后续的增量。让每个数据库资源来分摊原本需要一台数据库所提供的服务。
图片
<!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/shardingsphere-jdbc-core --><dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core</artifactId> <version>5.4.1</version></dependency> <!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml --><dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.33</version></dependency>
图片
sharding/sharding-jdbc-dev.yaml
# https://shardingsphere.apache.org/index_zh.htmlmode: # 运行模式类型。可选配置:内存模式 Memory、单机模式 Standalone、集群模式 Cluster - 目前为单机模式 type: StandalonedataSources: ds_0: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://127.0.0.1:13306/xfg_dev_tech_db_00?useUnicode=true&characterEncoding=utf8&autoRecnotallow=true&zeroDateTimeBehavior=convertToNull&serverTimeznotallow=UTC&useSSL=true username: root password: 123456 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 15 minPoolSize: 5 ds_1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://127.0.0.1:13306/xfg_dev_tech_db_01?useUnicode=true&characterEncoding=utf8&autoRecnotallow=true&zeroDateTimeBehavior=convertToNull&serverTimeznotallow=UTC&useSSL=true username: root password: 123456 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 15 minPoolSize: 5rules: - !SHARDING # 库的路由 defaultDatabaseStrategy: standard: shardingColumn: user_id shardingAlgorithmName: database_inline # 表的路由 tables: user_order: actualDataNodes: ds_$->{ 0..1}.user_order_$->{ 0..3} tableStrategy: standard: shardingColumn: user_id shardingAlgorithmName: user_order_inline # 路由算法 shardingAlgorithms: # 库-路由算法 2是两个库,库的数量。库的数量用哈希模2来计算。 database_inline: type: INLINE props: algorithm-expression: ds_$->{ Math.abs(user_id.hashCode()) % 2} # 表-路由算法 4是一个库里,表的数量。4 - 1 为了获得 011 这样的二进制值。不推荐 user_order_$->{ Math.abs(user_id.hashCode()) % 2} 作为表的路由 user_order_inline: type: INLINE props: algorithm-expression: user_order_$->{ (user_id.hashCode() ^ (user_id.hashCode()) >>> 16) & (4 - 1)}props: # 是否在日志中打印 SQL。 # 打印 SQL 可以帮助开发者快速定位系统问题。日志内容包含:逻辑 SQL,真实 SQL 和 SQL 解析结果。 # 如果开启配置,日志将使用 Topic ShardingSphere-SQL,日志级别是 INFO。false sql-show: true # 是否在日志中打印简单风格的 SQL。false sql-simple: true # 用于设置任务处理线程池的大小。每个 ShardingSphereDataSource 使用一个独立的线程池,同一个 JVM 的不同数据源不共享线程池。 executor-size: 20 # 查询请求在每个数据库实例中所能使用的最大连接数。1 max-connections-size-per-query: 1 # 在程序启动和更新时,是否检查分片元数据的结构一致性。 check-table-metadata-enabled: false # 在程序启动和更新时,是否检查重复表。false check-duplicate-table-enabled: false
与这个对比,如果你使用的路由功能并不那么大,其实自研会更加简单。当然你也可以想办法,简化 sharding-jdbc 的配置。
application-dev.yml
spring: datasource: driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver url: jdbc:shardingsphere:classpath:sharding/sharding-jdbc-dev.yaml
图片
@Testpublic void test_insert() { for (int i = 0; i < 1000; i++) { UserOrderPO userOrderPO = UserOrderPO.builder() .userName("小傅哥") .userId("xfg_" + RandomStringUtils.randomAlphabetic(6)) .userMobile("+86 13521408***") .sku("13811216") .skuName("《手写MyBatis:渐进式源码实践》") .orderId(RandomStringUtils.randomNumeric(11)) .quantity(1) .unitPrice(BigDecimal.valueOf(128)) .discountAmount(BigDecimal.valueOf(50)) .tax(BigDecimal.ZERO) .totalAmount(BigDecimal.valueOf(78)) .orderDate(new Date()) .orderStatus(0) .isDelete(0) .uuid(UUID.randomUUID().toString().replace("-", "")) .ipv4("127.0.0.1") .ipv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334".getBytes()) .extData("{ \"device\": { \"machine\": \"IPhone 14 Pro\", \"location\": \"shanghai\"}}") .build(); userOrderDao.insert(userOrderPO); }}
@Testpublic void test_selectByUserId() { List<UserOrderPO> list = userOrderDao.selectByUserId("xfg_PrmgwQ"); log.info("测试结果:{ }", JSON.toJSONString(list));}
@Testpublic void test_idx() { for (int i = 0; i < 50; i++) { String user_id = "xfg_" + RandomStringUtils.randomAlphabetic(6); log.info("测试结果 { }", (user_id.hashCode() ^ (user_id.hashCode()) >>> 16) & 3); }}
(责任编辑:休闲)