行业知识
shardingsphere5.1.1分表分库yaml配置 自定义策略
前言
通过阅读官方稳定给出示例 https://shardingsphere.apache.org/document一、基本配置示例
spring: sharding: datasource: names: ds0, ds1 ds0: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db0 username: root password: root ds1: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db1 username: root password: root sharding: default-database-strategy: inline: sharding-column: user_id algorithm-expression: ds$->{user_id % 2} tables: user: actual-data-nodes: ds$->{0..1}.user_$->{0..1} database-strategy: inline: sharding-column: user_id algorithm-expression: ds$->{user_id % 2} table-strategy: inline: sharding-column: id algorithm-expression: user_$->{id % 2} 在该配置中,有两个数据源ds0和ds1,分别对应两个数据库db0和db1。在ShardingSphere中,通过配置actual-data-nodes属性来指定数据分片的具体情况。在这里,我们指定了user表在ds0和ds1这两个数据源中的分片情况。其中,actual-data-nodes中的$->{0..1}表示分片,即分为两个分片。
在分片策略中,我们采用了取模算法。对于user表,我们根据user_id字段的值对数据进行分片,具体的算法表达式为ds$->{user_id % 2}。这样就将user表的数据分为了两个数据源。
对于具体的分片表,我们还需要设置table-strategy,以实现分表的功能。在这里,我们根据id字段的值对数据进行分表,具体的算法表达式为user_$->{id % 2}。
二、自定义策略
官网策略规则(混合规则)
rules:- !SHARDING tables: # 数据分片规则配置 (+): # 逻辑表名称 actualDataNodes (?): # 由数据源名 + 表名组成(参考 Inline 语法规则) databaseStrategy (?): # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一 standard: # 用于单分片键的标准分片场景 shardingColumn: # 分片列名称 shardingAlgorithmName: # 分片算法名称 complex: # 用于多分片键的复合分片场景 shardingColumns: # 分片列名称,多个列以逗号分隔 shardingAlgorithmName: # 分片算法名称 hint: # Hint 分片策略 shardingAlgorithmName: # 分片算法名称 none: # 不分片 tableStrategy: # 分表策略,同分库策略 keyGenerateStrategy: # 分布式序列策略 column: # 自增列名称,缺省表示不使用自增主键生成器 keyGeneratorName: # 分布式序列算法名称 auditStrategy: # 分片审计策略 auditorNames: # 分片审计算法名称 - - allowHintDisable: true # 是否禁用分片审计hint defaultDatabaseStrategy: standard: shardingColumn: # 分片列名称 shardingAlgorithmName: # 分片算法名称 # 分片算法配置 shardingAlgorithms: (+): # 分片算法名称 type: # 分片算法类型 props: # 分片算法属性配置 # ...https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/ keyGenerators: (+): # 分布式序列算法名称 type: # 分布式序列算法类型 props: # 分布式序列算法属性配置- !READWRITE_SPLITTING dataSources: : # 读写分离逻辑数据源名称 dynamicStrategy: # 读写分离类型 autoAwareDataSourceName: # 数据库发现逻辑数据源名称 : # 读写分离逻辑数据源名称 dynamicStrategy: # 读写分离类型 autoAwareDataSourceName: # 数据库发现逻辑数据源名称- !DB_DISCOVERY dataSources: : dataSourceNames: # 数据源名称列表 - ds_0 - ds_1 - ds_2 discoveryHeartbeatName: # 检测心跳名称 discoveryTypeName: # 数据库发现类型名称 : dataSourceNames: # 数据源名称列表 - ds_3 - ds_4 - ds_5 discoveryHeartbeatName: # 检测心跳名称 discoveryTypeName: # 数据库发现类型名称 discoveryHeartbeats: : # 心跳名称 props: keep-alive-cron: # cron 表达式,如:'0/5 * * * * ?' discoveryTypes: : # 数据库发现类型名称 type: # 数据库发现类型,如:MySQL.MGR props: group-name: # 数据库发现类型必要参数,如 MGR 的 group-name- !ENCRYPT encryptors: (+): # 加解密算法名称 type: # 加解密算法类型 props: # 加解密算法属性配置 (+): # 加解密算法名称 type: # 加解密算法类型 tables: : # 加密表名称 columns: (+): # 加密列名称 plainColumn (?): # 原文列名称 cipherColumn: # 密文列名称 encryptorName: # 密文列加密算法名称 assistedQueryColumn (?): # 查询辅助列名称 assistedQueryEncryptorName: # 查询辅助列加密算法名称 likeQueryColumn (?): # 模糊查询列名称 likeQueryEncryptorName: # 模糊查询列加密算法名称 queryWithCipherColumn(?): # 该表是否使用加密列进行查询
spring: shardingsphere: mode: # 运行模式类型。可选配置:内存模式 Memory、单机模式 Standalone、集群模式 Cluster type: Memory props: #是否在日志中打印 SQL。 #打印 SQL 可以帮助开发者快速定位系统问题。日志内容包含:逻辑 SQL,真实 SQL 和 SQL 解析结果。 #如果开启配置,日志将使用 Topic ShardingSphere-SQL,日志级别是 INFO。 false sql-show: true #是否在日志中打印简单风格的 SQL。false sql-simple: false #用于设置任务处理线程池的大小。每个 ShardingSphereDataSource 使用一个独立的线程池,同一个 JVM 的不同数据源不共享线程池。 executor-size: 20 #次查询请求在每个数据库实例中所能使用的最大连接数。1 max-connections-size-per-query: 1 #在程序启动和更新时,是否检查分片元数据的结构一致性。 check-table-metadata-enabled: false #在程序启动和更新时,是否检查重复表。false check-duplicate-table-enabled: false datasource: names: ds0, ds1 ds0: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db0 username: root password: root ds1: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db1 username: root password: root rules: sharding: key-generators: # shardingsphere-jdbc-core-spring-boot-starter需要加载此项配置 snowflake: type: SNOWFLAKE props: worker-id: 123 #分片算法配置 sharding-algorithms: ## 分片算法名称 fenpian-db-1: #对应 Override getType type: UserDatabaseAlgorithm # 分片算法属性配置 props: strategy: STANDARD algorithm-class-name: xxxx.xxx.xxx.UserDatabaseAlgorithm fenpian-table-1: #对应 Override getType type: UserDataTableAlgorithm props: strategy: STANDARD algorithm-class-name: xxxxx.xxxx.xxx.UserDataTableAlgorithm tables: # 逻辑表名称 sys_user: # 由数据源名 + 表名组成(参考 Inline 语法规则) actual-data-nodes: ds${0..3}.sys_user${0..255} # 分表策略,缺省表示使用默认分库策略,以下的分片策略只能选其一 table-strategy: #standard: # 用于单分片键的标准分片场景 standard: #数据库的键 shardingColumn: xxxx sharding-algorithm-name: fenpian-table-1 # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一 database-strategy: #standard: # 用于单分片键的标准分片场景 standard: shardingColumn: xxxx # 此处使用的就是我们在sharding-algorithms里面定义的规则 sharding-algorithm-name: fenpian-db-1 public class UserDatabaseAlgorithm implements StandardShardingAlgorithm { @Override public String doSharding(Collection databaseNames, PreciseShardingValue shardingValue) { for (String databaseName : databaseNames) { if (databaseName.endsWith(String.valueOf(shardingValue.getValue() % 2))) { return databaseName; } } throw new IllegalArgumentException(); } @Override public String getType() { return "UserDatabaseAlgorithm"; }} public class UserDataTableAlgorithm implements StandardShardingAlgorithm { @Override public String doSharding(Collection databaseNames, PreciseShardingValue shardingValue) { for (String databaseName : databaseNames) { if (databaseName.endsWith(String.valueOf(shardingValue.getValue() % 2))) { return databaseName; } } throw new IllegalArgumentException(); } @Override public String getType() { return "UserDataTableAlgorithm"; }} SPI机制注入
org.apache.shardingsphere.sharding.spi.ShardingAlgorithm
xxxx.xxx.xxx.UserDatabaseAlgorithmxxxxx.xxxx.xxx.UserDataTableAlgorithm
搞完收工!!!!!
三、ShardingAlgorithm实现类扩展
在ShardingSphere中,ShardingAlgorithm是一个非常重要的概念,是分片规则中实现分片算法的核心接口。ShardingAlgorithm接口定义了如何根据分片键的值计算分片后的数据所在位置。ShardingSphere提供了多种ShardingAlgorithm的实现类,下面是常见的几种实现类及其区别:
PreciseShardingAlgorithm
PreciseShardingAlgorithm是ShardingSphere提供的最基本的分片算法接口,用于实现基于精确值的分片算法。PreciseShardingAlgorithm只能用于处理精确的分片键,即单个值,无法处理范围查询。使用PreciseShardingAlgorithm进行分片时,只需要在规则配置中指定算法名称,即可实现数据的分片。
用于将数据按照一定规则分散到不同的数据库和表中。它的实现可以用于以下场景:数据库扩展:当一个单一数据库无法满足应用程序的性能需求时,可以使用分库分表技术,将数据分散到多个数据库和表中,以提高性能。
数据隔离:某些业务需要数据隔离,例如多租户应用程序中,每个租户的数据需要独立存储,这时可以使用分库分表技术。
数据容错:当某个数据库或表发生故障时,分库分表可以通过备份或者替换该节点,从而提高应用程序的容错性。
数据负载均衡:使用分库分表可以将数据分散到不同的节点上,从而实现数据的负载均衡,提高应用程序的性能和稳定性。
RangeShardingAlgorithm
RangeShardingAlgorithm是ShardingSphere提供的另一种常见的分片算法接口,用于实现基于范围的分片算法。RangeShardingAlgorithm能够处理范围查询,支持处理>、>=、
上一篇:云服务器怎么防止攻击
下一篇:服务器怎么查找目录的内容?