ShardingSphere学习

1 基本概念

1.1 ShardingSphere概述

官网:https://shardingsphere.apache.org/index_zh.html

ShardingSphere学习

1.2 分库分表概述

分库分表是为了解决由于数据量过大而导致数据库性能降低的问题,将原来独立的数据库拆分成若干数据库组成 ,将数据大表拆分成若干数据表组成,使得单一数据库、单一数据表的数据量变小,从而达到提升数据库性能的目的。
● 水平分表
将一个表的数据按一定规则拆分到多个表结构相同的表中。

ShardingSphere学习

● 垂直分表
将一个表按照字段拆分成多个表,每个表存储其中一部分字段。

ShardingSphere学习

● 水平分库
同一个表的数据按一定规则拆到不同的数据库中,每个库可以放在不同的服务器上。

ShardingSphere学习

● 垂直分库
按照业务将表分布到不同的数据库上,达到专库专用。

ShardingSphere学习

2 简单使用

第一步:创建数据库,执行脚本;
第二步:创建springboot工程,引入依赖;

    <!-- sharding-jdbc依赖 -->
    <dependency>
        <groupid>org.apache.shardingsphere</groupid>
        <artifactid>sharding-jdbc-spring-boot-starter</artifactid>
        <version>4.0.0-RC1</version>
    </dependency>

第三步:编写测试相关代码;
第四步:编写配置文件,测试。

2.1 水平分表

策略:cid为偶数数据添加到course_db的course_0表,cid为奇数数据添加到course_db的course_1表。

ShardingSphere学习

配置:

sharding-jdbc 水平分表

一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

配置数据源,给数据源起名
spring.shardingsphere.datasource.names=ds

配置数据
spring.shardingsphere.datasource.ds.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds.username=root
spring.shardingsphere.datasource.ds.password=1234

指定course表分布情况,配置表在哪个数据库里面,表名称都是什么  ds.course_0  ds.course_1
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds.course_$->{0..1}

指定表分片策略
cid为偶数数据添加到ds(course_db)的course_0表,cid为奇数数据添加到ds(course_db)的course_1表
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2}

指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.2 垂直分表

可直接手动拆分表实现。

2.3 水平分库

策略:sid是偶数数据添加到course_db_0的course表,sid是奇数数据添加到course_db_1的course表。

ShardingSphere学习
ShardingSphere学习

配置:

sharding-jdbc 水平分库

一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

配置数据源,给数据源起名
spring.shardingsphere.datasource.names=ds0,ds1

配置第一个数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/course_db_0?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=1234

配置第二个数据源
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/course_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=1234

指定course表分布情况,配置表在哪个数据库里面,表名称都是什么  ds0.course  d1.course
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds$->{0..1}.course

指定数据库分片策略
sid是偶数数据添加到ds0(course_db_0)的course表,sid是奇数数据添加到ds1(course_db_1)的course表
spring.shardingsphere.sharding.tables.course.database-strategy.inline..sharding-column=cid
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=ds$->{cid % 2}

#spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=cid
#spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{cid % 2}

指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.4 垂直分库

策略:操作course表调用course_db,操作student表调用student_db。

ShardingSphere学习
ShardingSphere学习

配置:

sharding-jdbc 垂直分库

一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

配置数据源,给数据源起名
spring.shardingsphere.datasource.names=m0,m1

配置第一个数据源
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=1234

配置第二个数据源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/student_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=1234

操作course表调用m0(course_db),操作student表调用m1(student_db)
配置student表分布情况
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m0.course

指定student表里面主键sid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

配置student表分布情况
spring.shardingsphere.sharding.tables.student.actual-data-nodes=m1.student

指定student表里面主键sid生成策略
spring.shardingsphere.sharding.tables.student.key-generator.column=sid
spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE

打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.5 广播表

策略:添加、删除dictionary表数据时,同时操作course_db中dictionary表与student_db中dictionary表。

ShardingSphere学习
ShardingSphere学习

配置:

sharding-jdbc 操作广播表

一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

配置数据源,给数据源起名
spring.shardingsphere.datasource.names=m0,m1

配置第一个数据源
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=1234

配置第二个数据源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/student_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=1234

配置course表分布情况
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m0.course

指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

配置student表分布情况
spring.shardingsphere.sharding.tables.student.actual-data-nodes=m1.student

指定student表里面主键sid生成策略
spring.shardingsphere.sharding.tables.student.key-generator.column=sid
spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE

配置广播表
添加、删除dictionary表数据时,同时操作m0(course_db)中dictionary表与m1(student_db)中dictionary表
spring.shardingsphere.sharding.broadcast-tables=dictionary
spring.shardingsphere.sharding.tables.dictionary.key-generator.column=id
spring.shardingsphere.sharding.tables.dictionary.key-generator.type=SNOWFLAKE

打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.6 读写分离

主从复制通过MySQL自身配置实现,Sharding-JDBC通过对SQL语义的分析,将写操作与读操作分别路由至主库与从库实现读写分离。
策略:写操作course_db(127.0.0.1)从库中course表,读操作course_db(192.168.1.107)主库中course表。

ShardingSphere学习
ShardingSphere学习

配置:

sharding-jdbc 读写分离

一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

配置数据源,给数据源起名
spring.shardingsphere.datasource.names=s0,s1

配置主库数据源
spring.shardingsphere.datasource.s0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.s0.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.s0.username=root
spring.shardingsphere.datasource.s0.password=1234

配置从库数据源
spring.shardingsphere.datasource.s1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.s1.url=jdbc:mysql://192.168.1.107:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.s1.username=root
spring.shardingsphere.datasource.s1.password=root

主库从库逻辑数据源定义
写操作s0(course_db)主库中course表,读操作s1(course_db)从库中course表
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=s0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=s1

配置course表分布情况
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds0.course

指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

打开sql输出日志
spring.shardingsphere.props.sql.show=true

Original: https://www.cnblogs.com/zhanglei-code/p/15450980.html
Author: zhanglei-code
Title: ShardingSphere学习

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/569531/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Java连载151-JUnit简介以及HashMap初步分析

    一、配置JUnit环境 JUnit是一个集成测试单元框架,我们先下载软件包,来配置环境 <span class="hljs-keyword">pac…

    Java 2023年6月13日
    095
  • JAXB java类与xml互转

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了…

    Java 2023年6月5日
    078
  • 设计模式 10 装饰器模式

    装饰器模式(Decorator Pattern)属于 结构型模式 装饰,顾名思义,就是在原有基础上增添东西以显示更好的效果。 生活中非常多这样的例子, 衣服饰品、 珠宝首饰、 房子…

    Java 2023年6月6日
    069
  • mybatis plus通过java代码进行权限等全局控制

    在mapper.xml中调用java静态方法,并且传递一些参数 在静态方法中进行sql拼接,可以用于用户权限管理、数据权限管理等等 一、静态方法 拼接sql,可以调用缓存中的用户权…

    Java 2023年6月16日
    066
  • 黑喵桌面音乐播放器汉化版

    安装方法:解压CharaColle,运行Regpatch。exe 注册游戏(XP 系统需要装.net framework 2.0)即可启动游戏。启动比较慢,请大家耐心下 使用说明:…

    Java 2023年5月29日
    067
  • 基于Python来获取用户手机设备使用情况

    前言 本博客为模式识别作业的记录,实现批感知器算法、Ho Kashyap算法和MSE多类扩展方法,可参考教材[ 1 ] \color{#0000FF}{[1]}[1 ]。所用数据如…

    Java 2023年6月7日
    085
  • Oracle表主键作为外键都用在哪些表查询

    Oracle外键关联查询 Oracle中,如果设置了外键,删除数据时,必须将外键关联一并删除,但是如果对项目不是很熟悉时,我们无法判断到底都在哪些表中有外键关联,以下提供了一个查询…

    Java 2023年6月13日
    064
  • Android 开创java世界(JNI Invocation API)

    在Android的世界中,由名称为app_process的C++本地应用程序(路径为:framework/base/cmds/app_process/app_main.cpp)调用…

    Java 2023年5月29日
    085
  • Android RTL 语言适配

    使用 start/end 代替 left/right 属性值。 官方给出的需要替换的属性值列表如下: Android 对 RTL 的支持,是从 Android 4.2 版本开始的。…

    Java 2023年6月7日
    0103
  • nginx反向代理400绕过学习

    昨天出了grafana的LFI payload 当然payload的方法有很多,本文重点不在于payload。重点在于grafana或其他业务基本上都是通过nginx去进行反向代理…

    Java 2023年5月30日
    071
  • Elasticsearch 入门实战(1)–简介

    Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎;本文主要介绍其基本概念。 1、概述 Elasticsearch 是一个分布式、高扩展、高实时的搜…

    Java 2023年6月16日
    074
  • 配置SSH无秘钥登录

    [hadoop@hadoop01 ~]$ cd .ssh[hadoop@hadoop01 .ssh]$ ll -d ./  #查看.ssh文件夹的权限drwx——. 2 h…

    Java 2023年6月16日
    096
  • linux下启动MongoDB权限不够

    bash: ./mongod: 权限不够 解决办法: 在MongoDB安装目录下: chmod -R 740 bin Original: https://www.cnblogs.c…

    Java 2023年6月15日
    083
  • [JVM] CPU缓存一致性协议

    CPU缓存一致性协议 CPU高速缓存 CPU缓存是位于cpu和内存之间的临时数据交换器,它的容量比内存小的夺但是交换速度要比内存快得多,主要是 为了解决cpu运行时的处理速度与内存…

    Java 2023年6月5日
    091
  • 反射

    快速入门: re.properties&#x6587;&#x4EF6;&#xFF1A; classfullpath=com.example.Cat meth…

    Java 2023年6月5日
    089
  • Vue 前端权限控制的优化改进版

    1、前言 之前《Vue前端访问控制方案 》一文中提出,使用class=”permissions”结合元素id来标识权限控制相关的dom元素,并通过公共方法c…

    Java 2023年6月14日
    073
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球