亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java ShardingJDBC實戰(zhàn)演練

 更新時間:2021年11月08日 10:55:47   作者:小胖子——鑫  
Sharding-JDBC 采用在 JDBC 協(xié)議層擴展分庫分表,是一個以 jar 形式提供服務的輕量級組件,其核心思路是小而美地完成最核心的事情

一、背景

最近在公司手頭上的項目單表達到了五千萬的規(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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論