个人做网站模版是否有人买,网络设计专业包括哪些,学校网站手机站的建设,网站建设服务器的选择方案有目录 一 什么是ShardingSphere分库分表
二 代码实现
1.导入相关依赖
2.配置相关参数
3.创建学生类以及mapper接口
4.实现 StandardShardingAlgorithm接口自定义分片算法 唐洋洋我知道你在看!!!嘿嘿
一 什么是ShardingSphere分库分表
我们平时在设计数据库的时候#xf…目录 一 什么是ShardingSphere分库分表
二 代码实现
1.导入相关依赖
2.配置相关参数
3.创建学生类以及mapper接口
4.实现 StandardShardingAlgorithm接口自定义分片算法 唐洋洋我知道你在看!!!嘿嘿
一 什么是ShardingSphere分库分表
我们平时在设计数据库的时候一般都是一个数据库里面有很多张表比如用户表那么所有的用户信息都会存在这一张表里面。但是当数据量特别大的时候如果你只用一个数据库一张用户表来存这样就会到这这个数据库访问压力过大。所以分库分表就是把大量用户数据分成多个数据库的多张表来存这里以学生信息表为例 可以看到我创建了两个数据库每个数据库分别有两张表来存储学生信息这样相当于一共有四张学生表了这样就能减少每个数据库的访问压力。
既然分库分表的意思明白了那么我们查询或者插入学生表的时候该怎么操作呢比如我现在要存入一个学生信息我该往哪个表存呢那么这里肯定是通过计算出来的而且是某种算法整体要平均。
所以这就是我们ShardingSphere的作用。
二 代码实现
1.导入相关依赖
!-- shardingsphere依赖--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.23/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.1/version/dependencydependencygroupIdorg.apache.shardingsphere/groupIdartifactIdshardingsphere-jdbc-core/artifactIdversion5.3.2/versionexclusionsexclusionartifactIdsnakeyaml/artifactIdgroupIdorg.yaml/groupId/exclusion/exclusions/dependency!-- 坑爹的版本冲突 --dependencygroupIdorg.yaml/groupIdartifactIdsnakeyaml/artifactIdversion1.26/version/dependency
2.配置相关参数
application.yml参数 spring:datasource:driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriverurl: jdbc:shardingsphere:classpath:shardingsphere-config.yaml shardingsphere-config.yaml参数 dataSources:student_0:dataSourceClassName: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.mysql.cj.jdbc.DriverjdbcUrl: jdbc:mysql://localhost:3306/student_0?useUnicodetruecharacterEncodingUTF-8rewriteBatchedStatementstrueallowMultiQueriestrueserverTimezoneAsia/Shanghaiusername: rootpassword: 1234student_1:dataSourceClassName: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.mysql.cj.jdbc.DriverjdbcUrl: jdbc:mysql://localhost:3306/student_1?useUnicodetruecharacterEncodingUTF-8rewriteBatchedStatementstrueallowMultiQueriestrueserverTimezoneAsia/Shanghaiusername: rootpassword: 1234
rules:- !SHARDING #分片规则tables:tb_student: #逻辑表名actualDataNodes: student_${0..1}.t_student_${0..3}databaseStrategy: # 分库策略standard: # 用于单分片键的标准分片场景shardingColumn: id # 分片列名称shardingAlgorithmName: db_hash_mode_algorithm # 分片算法名称tableStrategy: # 分表策略同分库策略standard: # 用于单分片键的标准分片场景shardingColumn: id # 分片列名称shardingAlgorithmName: tb_student_hash_mode_algorithm # 分片算法名称# 分片算法配置shardingAlgorithms:db_hash_mode_algorithm:type: CLASS_BASED # 分片算法类型 自定义props: # 分片算法属性配置db_sharding-count: 2table_sharding-count: 4strategy: standard #标准的分片算法algorithmClassName: com.feisi.shardingsphere.MyStandardShardingAlgorithmtb_student_hash_mode_algorithm:type: HASH_MODprops:sharding-count: 4
props:sql-show: true3.创建学生类以及mapper接口 学生类
package com.feisi.shardingsphere.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
import org.springframework.stereotype.Component;Data
TableName(tb_student)
Builder
Component
public class Student {TableId(type IdType.INPUT)private Long id; //分片键private String name;private Integer age;
}
学生mapper接口
package com.feisi.shardingsphere.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.feisi.shardingsphere.pojo.Student;
import org.apache.ibatis.annotations.Mapper;Mapper
public interface StudentMapper extends BaseMapperStudent {
}4.实现 StandardShardingAlgorithm接口自定义分片算法
package com.feisi.shardingsphere;import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.sharding.algorithm.sharding.ShardingAutoTableAlgorithmUtil;
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 org.apache.shardingsphere.sharding.exception.algorithm.sharding.ShardingAlgorithmInitializationException;import java.util.Collection;
import java.util.Optional;
import java.util.Properties;public class MyStandardShardingAlgorithm implements StandardShardingAlgorithmComparable? {// 数据库的数量配置参数名private static final String DB_SHARDING_COUNT_KEY db_sharding-count;// 表的数量配置参数名private static final String TABLE_SHARDING_COUNT_KEY table_sharding-count;private int dbShardingCount;private int tableShardingCount;Overridepublic void init(final Properties props) {dbShardingCount getDbShardingCount(props);tableShardingCount getTableShardingCount(props);}private int getTableShardingCount(Properties props) {ShardingSpherePreconditions.checkState(props.containsKey(TABLE_SHARDING_COUNT_KEY), () - new ShardingAlgorithmInitializationException(getType(), Sharding count cannot be null.));return Integer.parseInt(props.getProperty(TABLE_SHARDING_COUNT_KEY));}private int getDbShardingCount(Properties props) {ShardingSpherePreconditions.checkState(props.containsKey(DB_SHARDING_COUNT_KEY), () - new ShardingAlgorithmInitializationException(getType(), Sharding count cannot be null.));return Integer.parseInt(props.getProperty(DB_SHARDING_COUNT_KEY));}Override//availableTargetNames: ds_${0..1}: ds_0 ds_1public String doSharding(CollectionString availableTargetNames, PreciseShardingValueComparable? shardingValue) {//id进行hash 数据库数量 2 表的数量 4
// 这里使用哈希取余的算法// hash % 4 0..3 /2 0,1 /2 0 2,3/2 1String suffix String.valueOf(hashShardingValue(shardingValue.getValue()) % tableShardingCount / dbShardingCount);return ShardingAutoTableAlgorithmUtil.findMatchedTargetName(availableTargetNames, suffix, shardingValue.getDataNodeInfo()).orElse(null);}Overridepublic CollectionString doSharding(CollectionString availableTargetNames, RangeShardingValueComparable? shardingValue) {return availableTargetNames;}//对分片键的值进行hash算法private long hashShardingValue(final Object shardingValue) {return Math.abs((long) shardingValue.hashCode());}Overridepublic String getType() {return CLASS_BASED;}}这里自定义分片算法在dosharding方法里面分片算法的意思就是根据你的分片建通过算法确定你的学生信息存储到哪个数据库中的哪张表里面去。
这里使用的是哈希取余算法先根据你的分片键这里的分片键就是id计算出它的哈希值然后 哈希值%你的数据表的数量要存的哪块表 要存的哪块表%你的数据库的数量要存在哪个库里面。
为什么公式是这样的因为我们的每个库从0开始每个库对应下面的表也是从0开始。所以这个公式就能算出来。
比如一个id的哈希值是448488114
我们的库数量是2个01
表数量是4个0123
那么448488114%42存在表2里面
但是表2在数据库1下面所以你的算法不能算错如果算到0库那么就找不到这个张表了。
2/21(数据库1)
这样就完成了最后算到你这个学生信息应该存到数据库1下面的数据表2中。