分布式中间件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/。
该组件使用,麻烦一点点就是,自己去设计分库分别策略,与配置分库分别的数据源。希望给大家能带来帮助

发布了10 篇原创文章 · 获赞 10 · 访问量 1844

猜你喜欢

转载自blog.csdn.net/weixin_35997672/article/details/100662151