首页 > 其他分享 >sharding分表应用笔记(一)——分表数据源配置

sharding分表应用笔记(一)——分表数据源配置

时间:2023-11-07 20:01:36浏览次数:34  
标签:数据源 配置 sharding shardingsphere 分表 2.2

sharding分表应用笔记(一)——分表数据源配置

目录

1 前言

应用背景:物理数据源只有一个;对于部分数据量大的表实行按月分表处理,其他的表仍然保持原先的模式不变。本篇记录sharding分表的逻辑数据源配置。

环境:spring

2 配置

2.1 相关依赖

<!-- without spring -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>4.1.1</version>
</dependency>

<!-- for spring namespace -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-namespace</artifactId>
    <version>4.1.1</version>
</dependency>

出于稳定性考虑,选用了4.x版本

因为项目使用spring,所以采用spring命名空间的配置方式。如果是spring-boot项目可以引入以下依赖,使用spring-boot配置文件的方式进行配置。其他方式的具体配置方法可参考官方文档。

<!-- for spring boot -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>${sharding-sphere.version}</version>
</dependency>

2.2 命名空间配置

2.2.1 引入sharding命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://shardingsphere.apache.org/schema/shardingsphere/sharding
                        http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>

引入的相关新内容主要是

xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
xsi:schemaLocation="http://shardingsphere.apache.org/schema/shardingsphere/sharding
http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd"

2.2.2 物理数据源配置

这个是比较常规的数据库配置。

<!-- 数据源(使用连接池) -->
<bean id = "dataSource" class = "com.alibaba.druid.poolDruidDataSource" destroy-method = "close" >
    <!-- 数据库基本信息配置 -->
    <property name = "url" value = "${jdbc.url}" />
    <property name = "username" value = "${jdbc.username}" />
    <property name = "password" value = "${jdbc.password}" />
    <property name = "driverClassName" value = "${jdbc.driverClassName}" />
    <property name = "filters" value = "config,wall" />
    <!-- 最大并发连接数 -->
    <property name = "maxActive" value = "${jdbc.maxActive}" />
    <!-- 初始化连接数量 -->
    <property name = "initialSize" value = "${jdbc.initialSize}" />
    <!-- 配置获取连接等待超时的时间 -->
    <property name = "maxWait" value = "${jdbc.maxWait}" />
    <!-- 最小空闲连接数 -->
    <property name = "minIdle" value = "${jdbc.minIdle}" />
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name = "timeBetweenEvictionRunsMillis" value ="60000" />
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name = "minEvictableIdleTimeMillis" value ="300000" />
    <property name = "validationQuery" value = "select 1 from dual" />
    <property name = "testWhileIdle" value = "true" />
    <property name = "testOnBorrow" value = "false" />
    <property name = "testOnReturn" value = "false" />
    <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${jdbc.publicKey}" />
</bean>

项目有环境分包,所以有些参数动态获取,这里不进行展示

2.2.3 分表数据源配置

分表数据源在物理数据源的基础上进一步包装一层Sharding的数据源配置,实现一个物理数据源分为两个逻辑数据源。

<!-- 分表配置 -->
<sharding:data-source id="shardingDataSource">
    <sharding:sharding-rule data-source-names="dataSource" default-data-source-name="dataSource">
        <sharding:table-rules>
            <sharding:table-rule logic-table="t_table" actual-data-nodes="dataSource.t_table_2023_0$->{7..9}" table-strategy-ref="byCreateTimeTableStrategy" />
        </sharding:table-rules>
    </sharding:sharding-rule>
    <sharding:props>
        <prop key="sql.show">true</prop>
    </sharding:props>
</sharding:data-source>

(?)代表可缺省;(+)代表可重复配置

<sharding:data-source />

名称 类型 说明
id 属性 Spring Bean Id
sharding-rule 标签 数据分片配置规则
props (?) 标签 属性配置

<sharding:sharding-rule />

名称 类型 说明
data-source-names 属性 数据源Bean列表,本次只有单个数据源;如果有多个Bean以逗号分隔
default-data-source-name (?) 属性 未配置分片规则的表将通过默认数据源定位
table-rules 标签 表分片规则配置对象

<sharding:table-rules />

名称 类型 说明
table-rule (+) 标签 表分片规则配置对象

<sharding:table-rule />

名称 类型 说明
logic-table 属性 逻辑表名称
actual-data-nodes (?) 属性 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况。以代码中为例,使用了inline表达式,代表数据节点是从2023_07到2023_09三个月的三张月份表
table-strategy-ref (?) 属性 表分片策略,对应sharding:xxx-strategy中的策略Id,缺省表示使用<sharding:sharding-rule />配置的默认表分片策略

<sharding:props />

名称 类型 说明
sql.show (?) 属性 是否开启SQL显示,默认值: false

其他未涉及的配置项请参考官方文档

3 外部链接

ShardingSphere-4.1.1中文说明文档:https://shardingsphere.apache.org/document/4.1.1/cn/overview/

标签:数据源,配置,sharding,shardingsphere,分表,2.2
From: https://www.cnblogs.com/nagiumi-misaka/p/17815790.html

相关文章

  • 多源异构数据源融合怎么做,用这个一步搞定
    多源异构数据源融合是将来自不同来源、不同类型和不同结构的数据整合在一起,以实现更全面、准确和综合的分析和决策支持。以下是一个详细的步骤指南,以帮助您了解如何进行多源异构数据源融合。1.确定数据需求和目标:-首先,明确您的数据需求和目标。确定您想要从数据中获取什么样......
  • 自增主键与雪花算法的优缺点、设计更适合分库分表的UUID算法
    (目录)为什么不推荐使用自增主键递增主键的作用我们在数据库里保存的数据就跟excel表一样,一行行似的而在底层,这一行行数据,就是保存在一个个16k大小的页里。每次都去遍历所有的行性能会不好,于是为了加速搜索,我们可以根据主键id,从小到大排列这些行数据,将这些数据页用双向链表......
  • 【初学Nacos小问题】Nacos同一个命名空间下配置的数据源在项目中是如何识别的!
    在user-service项目中添加一个bootstrap.yml文件,需要配置服务名、开发环境、nacos地址、后缀名,对应nacos添加的配置文件名。内容如下:spring:application:name:userservice#服务名称profiles:active:dev#开发环境,这里是devcloud:nacos:serv......
  • Centos7 部署gitea,使用sqlite作为数据源
    #创建用户git,指定登录shell为Bash,-d指定家目录默认/home/git,-m如果指定的家目录不存在,则创建该目录#踩坑:-r不分配登录shell和家目录useradd-s/bin/bash-d-mgit#编辑/etc/sudoers文件以允许git用户在执行sudo命令时无需密码vi/etc/sudoers在文件中找到以rootALL=......
  • 如何找到 SAP Fiori Elements 应用某个字段显示值具体的数据源试读版
    笔者将自己在SAP领域16年(2007~2023)的SAPUI5(Fiori)和OData开发的技术沉淀,进行了系统的归纳和总结,分别写成了三套由浅入深的学习教程,收到了不错的反响:零基础快速学习ABAP一套适合SAPUI5开发人员循序渐进的学习教程SAPOData开发实战教程-从入门到提高这三套教程都......
  • 数据库【分库分表】
    一、场景    由于用户数量越来越大会出现以下问题:订单量剧增,单表数据量已经达到了千万的级别了,这个时候的索引查询已经很慢了,所以现在我们的类似这些大数据表的查询性能很差。数据量持续增加,现在我们的磁盘大部分空间都被使用,导致数据库的复制备份操作很缓慢,所以,目前数......
  • 数据库怎么分库的?怎么分表的?
    一、怎么分库垂直分库以表为依据,按照业务归属不同,将不同的表拆分到不同的库中。水平分库以字段为依据,按照一定策略(hash、range等),将一个库中的数据拆分到多个库中。二、怎么分表水平分表:以字段为依据,按照一定策略(hash、range等),将一个表中的数据拆分到多个表中。垂直分表:以字段为......
  • 分库分表
    分库:是为了解决数据库连接资源不足问题,和磁盘IO的性能瓶颈问题。分表:是为了解决单表数据量太大,sql语句查询数据时,即使走了索引也非常耗时问题。此外还可以解决消耗cpu资源问题。分库分表:可以解决数据库连接资源不足、磁盘IO的性能瓶颈、检索数据耗时和消耗cpu资源等问题。......
  • shardingdb:支持分片和并发读写的 GoLevelDB
    概述shardingdb是一个开源包,旨在为GoLevelDB增加分片和并发读写功能。它可以作为LevelDB的替代品,方便地集成到现有项目中。本博客将介绍shardingdb及其功能,并介绍如何在您的项目中使用它。特点-分片支持:shardingdb使您能够将数据分布在多个LevelDB实例中,提高性能和可扩......
  • Nacos单机模式配置远程数据源、配置身份认证
    1.nacos介绍官网链接:https://nacos.io/zh-cn/docs/what-is-nacos.htmlGithub:https://github.com/alibaba/nacos/Nacos/nɑ:kəʊs/是DynamicNamingandConfigurationService的首字母简称,一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。Nacos致力于......