Sharding-JDBC(八)5.3 系列升级解读

news2024/7/6 19:12:38

目录

    • 一、背景
    • 二、影响范围
      • 1.Maven 坐标调整
      • 2.自定义算法调整
      • 3.事务调整
      • 4.配置文件调整
    • 三、升级指导
      • 1.新的 ShardingSphereDriver 数据库驱动
      • 2.正在使用 Spring Boot Starter 如何升级
        • 升级前
        • 升级后
      • 3.正在使用 Spring Namespace 如何升级
        • 升级前
        • 升级后
    • 四、总结

在这里插入图片描述

5.3.0 官方文档: https://shardingsphere.apache.org/document/5.3.0/cn/overview/

补充:如果要看 5.4.0 的文档,直接把地址中的 5.3.0 改为 5.4.0 即可,以此类推。

一、背景

5.3.0 版本以前,ShardingSphere-JDBC 同时支持 Java API、YAML、Spring Boot Starter 和 Spring Namespace 等配置方式。为兼容 Spring 的配置方式给 ShardingSphere 社区带来了以下难题

  • 当新增或更新 API 时,需要调整多项配置文件,工作量大
  • 社区需要维护多重配置文档 & 示例
  • Spring Bean 生命周期管理易受项目其他依赖的影响。如:PostProcessor 无法正常执行。
  • Spring Boot Starter & Spring Namespace 配置风格与 ShardingSphere 标准的 YAML 存在较大差别
  • Spring Boot Starter & Spring Namespace 受 Spring 版本影响,带来额外的配置兼容性问题

例如:在最新发布的 Spring Boot 3.0.0 中,移除了对 2.x 版本 spring.factories 的支持。这为想要升级但正在使用 ShardingSphere Spring Boot Starter 的用户带来了挑战,而升级 Spring Boot 依赖也会为 ShardingSphere 用户带来新的兼容性问题。基于以上考虑,ShardingSphere 社区决定在 ShardingSphere 5.3.0 Release 中移除 Spring 全部依赖和配置支持。

那么,对需要使用 Spring Boot 或 Spring Namespace 的 ShardingSphere-JDBC 用户,应当如何接入 ShardingSphere?原有的用户怎样升级呢?本文将为您解答这些疑问。

二、影响范围

5.3 系列版本升级的影响范围如下:

  • 使用 ShardingSphere Spring Boot Starter 的用户;
  • 使用 ShardingSphere Spring Namespace 的用户。

1.Maven 坐标调整

原有配置:

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
    <version>${shardingsphere.version}</version>
</dependency>

<dependency>
	<groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId>
    <version>${shardingsphere.version}</version>
</dependency>

调整为:

<dependency>
	<groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core</artifactId>
    <version>${shardingsphere.version}</version>
</dependency>

2.自定义算法调整

移除 Spring 模块会同时移除 AlgorithmProvided 相关类。若此前用户在自定义算法中**有使用到 Bean 注入相关的逻辑,更新后将失效。**对需要在算法中使用 Spring Bean 的场景,需开发者主动管理。

3.事务调整

移除 Spring 模块同时移除事务,用于支持方法级别事务声明的 @ShardingSphereTransactionType 注解。若用户有在方法级别更改事务类型的需求,请使用 Java API 方式。

参考:用户手册-分布式事务,https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/special-api/transaction/java-api/

4.配置文件调整

在升级 5.3.0 版本后,原有的 Spring Boot Starter 或 Spring Namespace 数据源配置将会失效,具体配置升级的方式在下面会有介绍。

三、升级指导

1.新的 ShardingSphereDriver 数据库驱动

从 5.1.2 版本开始,工程师无需修改代码。ShardingSphere-JDBC 提供了原生 JDBC 驱动 ShardingSphereDriver,工程师仅通过配置即可接入使用,接入方式如下:

参考:用户手册-JDBC 驱动,https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/

这种方式可以更加统一 ShardingSphere-JDBCShardingSphere-Proxy 的配置文件格式,只需少量修改即可复用。在升级到 5.3.x 版本后,使用 Spring Boot Starter 或 Spring Namespace 的用户推荐使用 ShardingSphereDriver 方式来接入 ShardingSphere-JDBC。

2.正在使用 Spring Boot Starter 如何升级

升级前

在 application.yml 中,ShardingSphere 相关的配置如下:

application.yml

spring:
  shardingsphere:
    database:
      name: sharding_db
    datasource:
      names: ds_0,ds_1
      ds_0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
        username: root
        password:
      ds_1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
        username: root
        password:
    rules:
      sharding:
        default-database-strategy:
          standard:
            sharding-column: id
            sharding-algorithm-name: database_inline
        tables:
          t_order:
            actual-data-nodes: ds_$->{0..1}.t_order_$->{0..1}
            table-strategy:
              standard:
                sharding-column: count
                sharding-algorithm-name: t_order_inline
        sharding-algorithms: 
          database_inline:
            type: INLINE
            props:
              algorithm-expression: ds_$->{user_id % 2}
          t_order_inline:
            type: INLINE
            props:
              algorithm-expression: t_order_$->{order_id % 2}
    props:
      sql-show: true

升级后

resources 目录下新建 YAML 配置文件,如:sharding.yaml,并按照用户手册-YAML配置改写原有配置内容。

参考:用户手册-YAML 配置,https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/

application.yml

spring:
  datasource:
    driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
    url: jdbc:shardingsphere:classpath:sharding.yaml

sharding.yaml

dataSources:
  ds_0:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
    username: root
    password:
  ds_1:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
    username: root
    password:
 
rules:
- !SHARDING
  tables:
    t_order:
      actualDataNodes: ds_$->{0..1}.t_order_$->{0..1}
      tableStrategy:
        standard:
          shardingColumn: count
          shardingAlgorithmName: t_order_inline
  defaultDatabaseStrategy:
    standard:
      shardingColumn: id
      shardingAlgorithmName: database_inline
  shardingAlgorithms:
    database_inline:
      type: INLINE
      props:
        algorithm-expression: ds_$->{user_id % 2}
    t_order_inline:
      type: INLINE
      props:
        algorithm-expression: t_order_$->{order_id % 2}
 
props:
  sql-show: true

如果以集群模式部署,当对应 namespace 已有所需配置:sharding.yaml 中仅配置 mode 即可:

mode:
  type: Cluster
  repository:
    type: ZooKeeper
    props:
      namespace: governance_ds
      server-lists: localhost:2181
      retryIntervalMilliseconds: 500
      timeToLiveSeconds: 60
      maxRetries: 3
      operationTimeoutMilliseconds: 500

3.正在使用 Spring Namespace 如何升级

升级前

spring-sharding.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
       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://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://shardingsphere.apache.org/schema/shardingsphere/datasource
                           http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
                           http://shardingsphere.apache.org/schema/shardingsphere/sharding
                           http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
                           ">
 
    <bean id="ds_0" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    
    <bean id="ds_1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    
    <sharding:standard-strategy id="databaseStrategy" sharding-column="user_id" algorithm-ref="inlineStrategyShardingAlgorithm" />
 
    <sharding:sharding-algorithm id="inlineStrategyShardingAlgorithm" type="INLINE">
        <props>
            <prop key="algorithm-expression">ds_${user_id % 2}</prop>
        </props>
    </sharding:sharding-algorithm>
    
    <sharding:standard-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-ref="orderTableAlgorithm" />
    
    <sharding:sharding-algorithm id="orderTableAlgorithm" type="INLINE">
        <props>
            <prop key="algorithm-expression">t_order_${order_id % 2}</prop>
        </props>
    </sharding:sharding-algorithm>
    
    <sharding:rule id="shardingRule">
        <sharding:table-rules>
            <sharding:table-rule logic-table="t_order" database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy" />
        </sharding:table-rules>
    </sharding:rule>
    
    <shardingsphere:data-source id="shardingDataSource" database-name="sharding-databases" data-source-names="ds_0,ds_1" rule-refs="shardingRule" >
        <props>
            <prop key="sql-show">true</prop>
        </props>
    </shardingsphere:data-source>
</beans>

升级后

sharding.yaml

新增 sharding.yaml 配置文件,格式同 Spring Boot 示例中一致。

spring-sharding.xml

spring-sharding.xml 中移除原有 ShardingSphere 配置,替换为 ShardingSphereDriver 配置内容

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="shardingDataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="org.apache.shardingsphere.driver.ShardingSphereDriver" />
        <property name="url" value="jdbc:shardingsphere:classpath:sharding.yaml" />
    </bean>
</beans>

🎉 完成以上配置,即可畅享

ShardingSphere-JDBC 全新版本!

四、总结

此次升级,大大减少了 ShardingSphere-JDBCShardingSphere -Proxy 在配置方面的差异,为 ShardingSphere-JDBC 用户顺利过渡到 ShardingSphere 集群架构打好了基础,在 API 标准化、提升配置兼容性方面迈出了坚实的一步。

对于新用户而言,ShardingSphereDriver 的配置方式也能减少配置侵入性,上手更简单。此后,Apache ShardingSphere 社区也能更好的专注于自身功能迭代,为用户和开发者带来更多更好的特性。

整理完毕,完结撒花~ 🌻





参考地址:

1.ShardingSphere 5.3 系列升级解读:Spring 配置升级指南,https://blog.csdn.net/ShardingSphere/article/details/128910559

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/950399.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

实训笔记8.30

实训笔记8.30 8.30笔记一、项目开发流程一共分为七个阶段1.1 数据产生阶段1.2 数据采集存储阶段1.3 数据清洗预处理阶段1.4 数据统计分析阶段1.5 数据迁移导出阶段1.6 数据可视化阶段 二、项目数据清洗预处理的实现2.1 清洗预处理规则2.2 代码实现 三、项目的数据统计分析阶段3…

Linux通信--构建进程通信IPC的方案之共享内存|实现使用共享内存进行serverclient通信

共享内存是最快的IPC形式。一旦这样的内存映射到共享它的进程地址空间&#xff0c;这些进程间数据传递不再涉及到内核&#xff0c;即进程不再通过执行进入内核的系统调用来传递彼此的数据。 目录 一、共享内存的原理 二、使用共享内存 三、共享内存函数 1.shmget(用来创建共…

字节跳动岗位的薪酬体系曝光,看完感叹:真的不服不行

曾经的互联网是PC的时代&#xff0c;随着智能手机的普及&#xff0c;移动互联网开始飞速崛起。而字节跳动抓住了这波机遇&#xff0c;2015年&#xff0c;字节跳动全面加码短视频&#xff0c;从那以后&#xff0c;抖音成为了字节跳动用户、收入和估值的最大增长引擎。 自从字节…

Web3数据云OORT推出商用版智能代理构建平台:OORT TDS

随着技术进步和数据隐私问题的日益凸显&#xff0c;生成式AI和去中心化技术联手为企业和个人开辟了全新的互动视野。站在这一趋势的前沿&#xff0c;OORT展现了其在去中心化数据云领域的技术实力&#xff0c;作为行业的领先者&#xff0c;今日Oort正式宣布OORT TDS (Talk-to-Da…

MAGNA 直连 EDI 解决方案

全球首屈一指的汽车零部件供应商&#xff0c;在28个国家设有335家制造工厂&#xff0c;96个产品开发、工程和销售中心。产品主要包括制造车身、底盘、外饰、座椅、动力总成、电子、主动驾驶辅助、镜像、闭锁以及车顶系统&#xff0c;拥有多个领域的电子和软件工程能力。 MAGNA与…

row_number() over(partition by xx order by xx desc)

一、目的 主要用于根据某个字段对数据分组去重 二、demo 1. 有数据表 duplicate_test 如下 2. 使用 name 作为 key 对数据分组&#xff0c;并增加一列标识序号 idx&#xff08;根据 时间戳倒序标记序号&#xff09; select name,row_number() over(partition by name order…

IT6225B芯片方案|替代IT6225B方案|CS5366国产Typec转hdmi投屏方案

国产CS5366 透过模拟与数字的设计及28nm先进制程工艺,大幅降低功耗,无需增加散热片,提高产品可靠性,CS5366完全替代联阳IT6225B/IT6225,CS5366是一款Type-C转HDMI 2.0 4K60USB 3.0PD3.1/3.0高集成度视频转换芯片方案. 1.cs5366功耗低&#xff1a; CS5366系列符合USB电源传输规…

TCP数据报结构分析(面试重点)

在传输层中有UDP和TCP两个重要的协议&#xff0c;下面将针对TCP数据报的结构进行分析 关于UDP数据报的结构分析推荐看UDP数据报结构分析&#xff08;面试重点&#xff09; TCP结构图示 TCP报头结构的分析 一.16位源端口号 源端口表示发送数据时&#xff0c;发送方的端口号&am…

IO模型:阻塞和非阻塞

一、五种IO模型------读写外设数据的方式 阻塞: 不能操作就睡觉 非阻塞&#xff1a;不能操作就返回错误 多路复用&#xff1a;委托中介监控 信号驱动&#xff1a;让内核如果能操作时发信号&#xff0c;在信号处理函数中操作 异步IO&#xff1a;向内核注册操作请求&…

ES+Redis+MySQL,这个高可用架构设计太顶了!

目录 背景ES 高可用方案会员 Redis 缓存方案高可用会员主库方案异常会员关系治理展望&#xff1a;更精细化的流控和降级策略 背景 会员系统是一种基础系统&#xff0c;跟公司所有业务线的下单主流程密切相关。如果会员系统出故障&#xff0c;会导致用户无法下单&#xff0c;…

RS485隔离电路方案

RS485总线是一种使用平衡发送&#xff0c;差分接收实现通讯的通用串口通信总线&#xff0c;由于其具有抗共模干扰能力强、成本低、抗噪能力强、传输距离远、传输速率高、可连接多达256个收发器等优点&#xff0c;广泛应用于工业智能仪表&#xff0c;通讯设备等各个领域。 RS485…

Flutter:getX的学习

前言 学习教程&#xff1a;Getx教程_FlutterGetx系列实战教程 简介 getX是第三方的状态管理插件&#xff0c;不仅具有状态管理的功能&#xff0c;还具有路由管理、主题管理、国际化多语言管理、网络请求、数据验证等功能。相比其他状态管理组件&#xff0c;getX简单、功能强大…

JDK源码解析-Object

1. Object类 所有类的基类——java.lang.Object Object 类是所有类的基类&#xff0c;当一个类没有直接继承某个类时&#xff0c;默认继承Object类Object 类属于 java.lang 包&#xff0c;此包下的所有类在使用时无需手动导入&#xff0c;系统会在程序编译期间自动导入。 思…

(二)范数与距离

本文主要内容如下&#xff1a; 1. 范数的定义2. 常见的范数举例3. 范数的等价4. 距离与度量空间的定义 1. 范数的定义 定义1-1&#xff1a;设 E E E 为向量空间&#xff0c; R \mathbb{R} R 为实数域。若映射 ∥ ⋅ ∥ : E → R : x ↦ ∥ x ∥ \begin{equation*} \lVert\cd…

12.物联网LWIP之消息处理机制,lwip消息传递机制

一。LWIP数据包消息处理 1.接受数据包 2.构造消息 3.投递消息 4.获取消息 5.处理数据包 api_msg 这个结构体包括执行函数所必需的一切,对于另一个线程上下文中的netconn(主要用于处理netconn)在tcpip_thread上下文中(线程安全)。 struct api_msg { /* 大家可以理解为是一个so…

ssm学生信息管理系统源码和论文

ssm学生信息管理系统源码和论文075 开发工具&#xff1a;idea 数据库mysql5.7 数据库链接工具&#xff1a;navcat,小海豚等 技术&#xff1a;ssm 摘 要 传统办法管理学生信息首先需要花费的时间比较多&#xff0c;其次数据出错率比较高&#xff0c;而且对错误的数据进行…

多目标应用:基于多目标向日葵优化算法(MOSFO)的微电网多目标优化调度MATLAB

一、微网系统运行优化模型 参考文献&#xff1a; [1]李兴莘,张靖,何宇,等.基于改进粒子群算法的微电网多目标优化调度[J].电力科学与工程, 2021, 37(3):7 二、多目标向日葵优化算法 多目标向日葵优化算法&#xff08;Multi-objective sunflower optimization&#xff0c;MOS…

企业网络安全:威胁检测和响应 (TDR)

什么是威胁检测和响应 威胁检测和响应&#xff08;TDR&#xff09;是指识别和消除 IT 基础架构中存在的恶意威胁的过程。它涉及主动监控、分析和操作&#xff0c;以降低风险并防止未经授权的访问、恶意活动和数据泄露&#xff0c;以免它们对组织的网络造成任何潜在损害。威胁检…

新开通的抖店没有销量和体验分,如何找达人带货起店?教程如下

我是王路飞。 做抖店&#xff0c;想要快速起店&#xff0c;无非就是做动销&#xff0c;或者货损。 但是动销比较有风险&#xff0c;货损的话&#xff0c;一个是新手不会具体的操作和设置&#xff0c;一个是自己利润受损。 所以今天给你们说下&#xff0c;新开通的抖店在没有…

Java EE 突击 15 - Spring Boot 统一功能处理

Spring Boot 统一功能处理 一 . 统一功能的处理1.1 初级阶段 : 不断重复1.2 中级阶段 : 集成方法1.3 高级阶段 : Spring AOP1.4 超高级阶段 : Spring 拦截器准备工作实现拦截器自定义拦截器将自定义拦截器加入到系统配置 拦截器实现原理扩展 : 统一访问前缀添加 二 . 统一异常的…