Java ShardingJDBC實戰(zhàn)演練
一、背景
最近在公司手頭上的項目單表達到了五千萬的規(guī)模,而且日增長量每天就有10w左右,一個月就有大概300w的數(shù)據(jù),這樣一直下去過幾個月以后表的數(shù)據(jù)很容易就上億了,這樣不利于管理以及在大表的情況下,對于表的DDL效率也會相對下降,和幾個同事商量了下,于是乎開始做分表的技術優(yōu)化。
二、優(yōu)化事項
(1)首先先確定使用場景,當前表的使用場景更多的是根據(jù)一個具體的標識值去查詢,范圍查詢的場景頻率相對低下,在這這種情況下考慮想標識值作為分片鍵去進行分表。 具體的算法為:通過標識值通過算法算出具體的時間季度,按季節(jié)進行拆分進行拆分,也就是一年
record_delivery_log
4個表record_order_log_202101,record_order_log_202102,record_order_log_202103,record_order_log_202104
拆分前單表數(shù)據(jù)量為 5000w
拆分后單表的數(shù)據(jù)量變成1200w,能夠容忍將來4~ 5倍的增長量,符合預期范圍。
(2)調(diào)研了對應的分庫分表中間件,目前Sharing-jdbc是最主流的中間件,而且社區(qū)和文檔較完善,故采用Sharing-jdbc作為分表的中間件。
三、具體實戰(zhàn)
在這里因為公司項目不好復用的原因,用一個模擬項目來模擬這次改造。
(1)參照sharing-jdbc文檔對項目進行改造
引入sharing-jdbc對應的pom。
<dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId> <version>5.0.0-beta</version> </dependency>
對應的配置文件
#端口 server.port=8080 # 數(shù)據(jù)源ds0 spring.shardingsphere.datasource.name=ds0 # 數(shù)據(jù)源ds0的配置 spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds0.driverClassName=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/world1?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8 spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password=123456 # 分片規(guī)則,這里只分表,所以僅指定表的分片規(guī)則 spring.shardingsphere.rules.sharding.tables.record_order_log.actual-data-nodes=ds0.record_order_log_$->{2021..2031}0$->{1..4} # 指定數(shù)據(jù)庫的分片鍵,只有一個庫所以還是用分表的分片鍵 spring.shardingsphere.rules.sharding.tables.record_order_log.database-strategy.standard.sharding-column=order_delivery_id spring.shardingsphere.rules.sharding.tables.record_order_log.database-strategy.standard.sharding-algorithm-name=database-inline # 指定分表的分片鍵 spring.shardingsphere.rules.sharding.tables.record_order_log.table-strategy.standard.sharding-column=order_delivery_id spring.shardingsphere.rules.sharding.tables.record_order_log.table-strategy.standard.sharding-algorithm-name=table-inline # Omit t_order_item table rule configuration ... # ... # 分片規(guī)則(默認取模) spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds0 spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=CLASS_BASED spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.strategy=STANDARD spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithmClassName=com.cus.shd.sharingjdbc.config.OrderDeliveryIdShardingAlgorithm spring.shardingsphere.props.sql.show=true #mybatis-plus?? mybatis-plus.mapper-locations=classpath:mappers/*.xml mybatis-plus.type-aliases-package=com.cus.shd.sharingjdbc.model mybatis-plus.configuration.map-underscore-to-camel-case=true # sql?? mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #本地數(shù)據(jù)庫鏈接,忽略了springboot自動加載后失效 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/world1?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8 spring.datasource.username=root spring.datasource.password=123456
注意好分表鍵設置時候的表名。
(2)自定義分片鍵策略,根據(jù)order_delivery_id按季度進行存儲
package com.cus.shd.sharingjdbc.config; import org.apache.commons.lang.StringUtils; import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm; import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue; import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue; import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.Collection; /** * @author ASUS * @Description 自定義分片策略 * @Date 2021/11/6 22:20 **/ public class OrderDeliveryIdShardingAlgorithm implements StandardShardingAlgorithm<Long> { @Override public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) { String orderDeliveryId = shardingValue.getValue().toString(); orderDeliveryId = orderDeliveryId.substring(0,orderDeliveryId.length() - 4); // 將時間戳轉(zhuǎn)為當前時間 LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(Long.valueOf(orderDeliveryId)/1000, 0, ZoneOffset.ofHours(8)); String availableTargetName; int month = localDateTime.getMonthValue(); LocalDateTime nowTime = LocalDateTime.now(); int year = nowTime.getYear(); if(month >= 1 && month < 3){ availableTargetName = "01"; }else if(month >= 3 && month < 6){ availableTargetName = "02"; }else if(month >= 6 && month < 9){ availableTargetName = "03"; }else { availableTargetName = "04"; } if(StringUtils.isEmpty(availableTargetName)){ return null; } return String.format("%s_%s%s",shardingValue.getLogicTableName(),year,availableTargetName); } @Override public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> shardingValue) { return availableTargetNames; } @Override public void init() { } @Override public String getType() { return "ORDER_DELIVERY_ID"; } }
(3)模擬提供兩個接口,一個按id查詢,一個插入接口。(修改的場景暫時沒有,所以不考慮)
新增的時候做了模擬插入,能夠根據(jù)分片算法將數(shù)據(jù)存儲到對應的表,達到效果。
查詢同理。
(4)sharing-jdbc 不會自動的進行創(chuàng)建表,所以需在后臺維護一個定時任務,到了一定的季度點就要進行建表操作。(需確保生產(chǎn)環(huán)境的應用程序?qū)臄?shù)據(jù)庫賬號是否有建表權限)
<update id="createNewTable" parameterType="String"> CREATE TABLE ${tableName} SELECT * FROM record_order_log WHERE 1=2 </update>
四、遇到的問題
1、引入sharing-jdbc包的時候報錯了。這里debug到源碼發(fā)現(xiàn)是mybatisPlus的自動啟動器(MybatisPlusAutoConfiguration)有指定單一數(shù)據(jù)源類(spring中數(shù)據(jù)源不能有多個實現(xiàn)類)的時候才會啟動,因為sharing的引入造成了多數(shù)據(jù)源(多datasource),所以這個就不會啟動了,導致了實例化mapper的時候報錯了。解決方案是在SpringBoot的啟動類的注解加上
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,DruidDataSourceAutoConfigure.class})
忽略掉SpringBoot數(shù)據(jù)源自動裝配以及Druid數(shù)據(jù)源的自動裝配,把所有的數(shù)據(jù)源實例化交給sharing-jdbc
2、部分項目存在歷史遺留的問題,如果是mybatis或者hibernate的情況下,不想徹底引入sharding-jdbc數(shù)據(jù)源的話,個人覺得可以使用多數(shù)據(jù)源的形式來進行改造,去擴展需要使用分表的一些數(shù)據(jù)庫操作,切換對應的sharding數(shù)據(jù)源進行數(shù)據(jù)庫操作。具體可以參考switchDataSource目錄下的一些切換數(shù)據(jù)源的代碼。
3、給自己的疑問
忽略了DataSourceAutoConfiguration.class后,sharing-jdbc是如何整合mybatis-plus的?
答:其實也不難,相當于數(shù)據(jù)源這個對象原本由SpringBoot自帶的數(shù)據(jù)源自動注入進行注入,現(xiàn)在換成了Sharding的自動裝配(ShardingSphereAutoConfiguration)來進行注入,相當于換了整個數(shù)據(jù)源的一套東西,用的也是sharding整套的東西。
所以在改造的時候需要檢查一下是否對舊的項目存在影響。
五、項目源碼地址
cus-sharding-jdbc: sharding-jdbc springboot實戰(zhàn)
到此這篇關于Java ShardingJDBC實戰(zhàn)演練的文章就介紹到這了,更多相關Java ShardingJDBC內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java JVM原理與調(diào)優(yōu)_動力節(jié)點Java學院整理
JVM是Java Virtual Machine(Java虛擬機)的縮寫,JVM是一種用于計算設備的規(guī)范,它是一個虛構出來的計算機,是通過在實際的計算機上仿真模擬各種計算機功能來實現(xiàn)的。下面通過本文給大家介紹jvm原理與調(diào)優(yōu)相關知識,感興趣的朋友一起學習吧2017-04-04半小時實現(xiàn)Java手擼網(wǎng)絡爬蟲框架(附完整源碼)
最近在做一個搜索相關的項目,需要爬取網(wǎng)絡上的一些鏈接存儲到索引庫中,自己寫了一個簡單的網(wǎng)絡爬蟲,感興趣的可以了解一下2021-06-06oracle+mybatis-plus+springboot實現(xiàn)分頁查詢的實例
本文主要介紹了oracle+mybatis-plus+springboot實現(xiàn)分頁查詢,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Java之SpringBoot實現(xiàn)基本增刪改查(前后端分離版)
這篇文章主要介紹了Java中SpringBoot如何實現(xiàn)基本的增刪改查,前后端分離版,沒有和前端進行聯(lián)系,感興趣的小伙伴可以借鑒閱讀本文2023-03-03使用ServletInputStream在攔截器或過濾器中應用后重寫
這篇文章主要介紹了使用ServletInputStream在攔截器或過濾器中應用后重寫,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10