分佈式中間件sharding的使用心得與配置

背景:目前分庫分表是比較 熱門的話題,因爲考慮使用分佈式架構,一般屬於訪問量,數據量比較大系統,在這樣的系統中不可避免,爲了給系統的性能提高與高可用採用分庫分表的方式去解決數據層面的壓力。博主本次接受sharding的基礎使用。
1:介紹下sharding對分庫分表的解決方案。
shardingjdbc這個組件,主要是基於代碼的層面來控制分庫分表。他自己內部去實現sql改寫,路由,合併查詢結果,重要的是,sharding自身去整合了一套分佈式事務,XA與柔性事務,柔性事務的性能會比XA要好,具體這兩者的區別,,大家可以去百度。關於分佈式事務也是大家非常感興趣的點,後續會詳細的去介紹分佈式事務的幾種方式,應用之間,與庫之間。
2:大家感性的是這個怎麼來使用。
配置方面,這個是spring boot的配置方式,可以名稱空間的配置方式,與編程的配置方式。

spring.shardingsphere.datasource.names=swallow-bird0,swallow-bird1

spring.shardingsphere.datasource.swallow-bird0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.swallow-bird0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.swallow-bird0.jdbc-url=jdbc:mysql://*******:3306/swallow_bird0?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.swallow-bird0.username=root
spring.shardingsphere.datasource.swallow-bird0.password=aB...?967426
spring.shardingsphere.datasource.swallow-bird0.maxActive=10
spring.shardingsphere.datasource.swallow-bird0.minimum-idle=5
spring.shardingsphere.datasource.swallow-bird0.maximum-pool-size=15
spring.shardingsphere.datasource.swallow-bird0.auto-commit=true
spring.shardingsphere.datasource.swallow-bird0.idle-timeout=30000
spring.shardingsphere.datasource.swallow-bird0.pool-name=HikariCP
spring.shardingsphere.datasource.swallow-bird0.max-lifetime=1800000
spring.shardingsphere.datasource.swallow-bird0.connection-timeout=30000
spring.shardingsphere.datasource.swallow-bird0.connection-test-query=SELECT 1


spring.shardingsphere.datasource.swallow-bird1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.swallow-bird1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.swallow-bird1.jdbc-url=jdbc:mysql://*******:3306/swallow_bird1?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.swallow-bird1.username=root
spring.shardingsphere.datasource.swallow-bird1.password=aB...?967426
spring.shardingsphere.datasource.swallow-bird1.maxActive=20
spring.shardingsphere.datasource.swallow-bird1.minimum-idle=5
spring.shardingsphere.datasource.swallow-bird1.maximum-pool-size=15
spring.shardingsphere.datasource.swallow-bird1.auto-commit=true
spring.shardingsphere.datasource.swallow-bird1.idle-timeout=30000
spring.shardingsphere.datasource.swallow-bird1.pool-name=HikariCP
spring.shardingsphere.datasource.swallow-bird1.max-lifetime=1800000
spring.shardingsphere.datasource.swallow-bird1.connection-timeout=30000
spring.shardingsphere.datasource.swallow-bird01.connection-test-query=SELECT 1


#客戶表分表
spring.shardingsphere.sharding.tables.customer.actual-data-nodes=swallow-bird$->{0..1}.customer_$->{0..1}
spring.shardingsphere.sharding.tables.customer.database-strategy.standard.sharding-column=customer_id
spring.shardingsphere.sharding.tables.customer.database-strategy.standard.precise-algorithm-class-name=com.swallowBirds.sass.businessSystem.config.sharding.CustomSublibrary
spring.shardingsphere.sharding.tables.customer.table-strategy.standard.sharding-column=customer_id
spring.shardingsphere.sharding.tables.customer.table-strategy.standard.precise-algorithm-class-name=com.swallowBirds.sass.businessSystem.config.sharding.CustomSublibrarTB
#好友分表
spring.shardingsphere.sharding.tables.friend.actual-data-nodes=swallow-bird$->{0..1}.friend_$->{0..1}
spring.shardingsphere.sharding.tables.friend.database-strategy.standard.sharding-column=customer_id
spring.shardingsphere.sharding.tables.friend.database-strategy.standard.precise-algorithm-class-name=com.swallowBirds.sass.businessSystem.config.sharding.CustomSublibrary
spring.shardingsphere.sharding.tables.friend.table-strategy.standard.sharding-column=customer_id
spring.shardingsphere.sharding.tables.friend.table-strategy.standard.precise-algorithm-class-name=com.swallowBirds.sass.businessSystem.config.sharding.CustomSublibrarTB

這裏的配置是自定義分庫分表策略的方式

在策略方式中建議大家使用枚舉,並且定義每個分表分庫的版本。這樣的設計,是當你的表數據達到一定量的時候,可以選擇提升版本,來把數據優化存儲。具體的代碼博主實現是

public enum DbAndTableEnum {

    /**
     * 用戶信息表 UD+db+table+01+yyMMddHHmmssSSS+機器id+序列號id
     * 例如:UD000000011902261230103345300002 共 2+6+2+15+2+5=32位
     */

    //客戶表--
    CUSTOMER("customer", "customer_id", "01", "01", "YH", 2,2, 4, 2, 4, "客戶表"),
    CUSTOMER_DATA("customer_data", "customer_id", "01", "01", "KZ", 2,2, 4, 2, 4, "客戶表資料表"),
    CUSTOMER_AUTHORITY("customer_authority", "customer_id", "01", "01", "CA", 2,2, 4, 2, 4, "客戶權限表"),

    /**分片表名*/
    private String tableName;
    /**分片鍵*/
    private String shardingKey;
    /**系統標識*/
    private String bizType;
    /**主鍵規則版本*/
    private String idVersion;
    /**表名字母前綴*/
    private String charsPrefix;
    /**分片鍵值中純數字起始下標索引,第一位是0,第二位是1,依次類推*/
    private int numberStartIndex;
    /**數據庫索引位開始下標索引*/
    private int dbIndexBegin;
    /**表索引位開始下標索引*/
    private int tbIndexBegin;
    /**分佈所在庫數量*/
    private int dbCount;
    /**分佈所在表數量-所有庫中表數量總計*/
    private int tbCount;
    /**描述*/
    private String desc;

    DbAndTableEnum(String tableName, String shardingKey, String bizType, String idVersion, String charsPrefix,
                   int numberStartIndex, int dbIndexBegin, int tbIndexBegin, int dbCount, int tbCount, String desc) {
        this.tableName = tableName;
        this.shardingKey = shardingKey;
        this.bizType = bizType;
        this.idVersion = idVersion;
        this.charsPrefix = charsPrefix;
        this.numberStartIndex = numberStartIndex;
        this.dbIndexBegin = dbIndexBegin;
        this.tbIndexBegin = tbIndexBegin;
        this.dbCount = dbCount;
        this.tbCount = tbCount;
        this.desc = desc;
    }

    public String getTableName() {
        return tableName;
    }

    public String getShardingKey() {
        return shardingKey;
    }

    public String getBizType() {
        return bizType;
    }

    public String getIdVersion() {
        return idVersion;
    }

    public String getCharsPrefix() {
        return charsPrefix;
    }

    public int getNumberStartIndex() {
        return numberStartIndex;
    }

    public int getDbIndexBegin() {
        return dbIndexBegin;
    }

    public int getTbIndexBegin() {
        return tbIndexBegin;
    }

    public int getDbCount() {
        return dbCount;
    }

    public int getTbCount() {
        return tbCount;
    }

    public String getDesc() {
        return desc;
    }
}

引入依賴`

			<dependency>
				<groupId>io.shardingsphere</groupId>
				<artifactId>sharding-transaction-spring-boot-starter</artifactId>
				<version>4.0.0-RC1</version>
			</dependency>
			

現在版本更新到4.0.0-RC2了,不過使用方式差不不大。建議大家使用的,多參照官網
https://shardingsphere.apache.org/document/current/cn/overview/。
該組件使用,麻煩一點點就是,自己去設計分庫分別策略,與配置分庫分別的數據源。希望給大家能帶來幫助

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章