当前位置: 首页 > news >正文

shardingsphere分库分表跨库访问 添加分片规则

shardingsphere分库分表跨库访问 添加分片规则

建立 JDBC 环境

创建表

t_order:

CREATE TABLE `t_order` (`tid` bigint(20) NOT NULL,`tname` varchar(255) DEFAULT NULL,`goods_id` bigint(20) DEFAULT NULL,`tstatus` varchar(255) DEFAULT NULL,PRIMARY KEY (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

建立 SpringBoot 工程

修改 pom.xml:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.7.RELEASE</version><relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.22</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version></dependency><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.0.0-beta</version></dependency>
</dependencies>

创建实体类:

/*** 订单** @author BNTang* @date 2021/10/11*/
@Data
@TableName("t_order")
public class Order {private Long tid;private String tname;private Long goodsId;private String tstatus;
}

创建 Mapper:

/*** @author BNTang* @version 1.0* @project ShardingSpherePro* @description* @since Created in 2021/10/11 011 20:47**/
public interface OrderMapper extends BaseMapper<Order> {
}

修改启动类,添加注解:

@MapperScan("top.it6666.shardingspherepro.mapper")

application.properties

spring.shardingsphere.datasource.names=shardingspheredb1
spring.shardingsphere.datasource.shardingspheredb1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.shardingspheredb1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.shardingspheredb1.url=jdbc:mysql://localhost:3310/shardingspheredb1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.shardingspheredb1.username=root
spring.shardingsphere.datasource.shardingspheredb1.password=root
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

编写测试类:

@SpringBootTest
@RunWith(SpringRunner.class)
class ShardingSphereProApplicationTests {@Resourceprivate OrderMapper orderMapper;@Testvoid addOrder() {for (int i = 0; i < 10; i++) {Order order = new Order();order.setTid((long) i);order.setTname("订单" + i);order.setGoodsId(Long.valueOf("" + (1000 + i)));order.setTstatus("1");System.out.println(order);this.orderMapper.insert(order);}}
}

数据分片存储

建立分片真实表,t_order_0,t_order_1 SQL如下:

CREATE TABLE `t_order_0` (`tid` bigint(20) NOT NULL,`tname` varchar(255) DEFAULT NULL,`goods_id` bigint(20) DEFAULT NULL,`tstatus` varchar(255) DEFAULT NULL,PRIMARY KEY (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `t_order_1` (`tid` bigint(20) NOT NULL,`tname` varchar(255) DEFAULT NULL,`goods_id` bigint(20) DEFAULT NULL,`tstatus` varchar(255) DEFAULT NULL,PRIMARY KEY (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

分表配置

修改 application.properties 添加如下相关的配置内容:

# 配置t_order真实表规则
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=shardingspheredb1.t_order_$->{0..1}# 配置分表策略 主键+分片算法
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=tid
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=table-inline# 配置 分片算法
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression=t_order_$->{tid % 2}# 主键盘生成策略
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=tid
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=1# 打印执行sql
spring.shardingsphere.props.sql-show=true

spring.shardingsphere.props.sql-show=true
这里就来一一解释一下如上配置当中比较关键的几个内容 spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes 该内容就是配置 t_order 真实表规则, 我如上配置的就是 0,1
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression 配置的内容就是真实表的寻找算法
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column 指定了分表以 tid 进行分表操作
如上的内容配置完毕之后再次运行测试类,在运行测试类之前其实可以将 id 的设置给去除因为如上配置了 主键盘生成策略

分库分表
添加第二个数据源,修改 application.properties:

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

修改表规则,修改配置文件,都是同一个配置文件内容修改,不再强调了:

# 水平拆分  水平分片
# 配置 t_order 表规则                                                 数据源.真实表
# 配置t_order真实表规则
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=shardingspheredb$->{1..2}.t_order_$->{0..1}

配置配置分库,主键 + 分片算法策略:

# 配置分库策略  主键+分片算法
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column=goods_id
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name=database-inline
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=shardingspheredb$->{goods_id % 2 + 1}

最终 application.properties 配置文件内容如下:

spring.shardingsphere.datasource.names=shardingspheredb1,shardingspheredb2
spring.shardingsphere.datasource.shardingspheredb1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.shardingspheredb1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.shardingspheredb1.url=jdbc:mysql://www.yangbuyi.top:3310/shardingspheredb1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.shardingspheredb1.username=root
spring.shardingsphere.datasource.shardingspheredb1.password=yangbuyiya
# 配置第 2 个数据源
spring.shardingsphere.datasource.shardingspheredb2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.shardingspheredb2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.shardingspheredb2.url=jdbc:mysql://www.yangbuyi.top:3310/shardingspheredb2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.shardingspheredb2.username=root
spring.shardingsphere.datasource.shardingspheredb2.password=yangbuyiya
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 水平拆分  水平分片
# 配置 t_order 表规则                                                 数据源.真实表
# 配置t_order真实表规则
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=shardingspheredb$->{1..2}.t_order_$->{0..1}
# 配置分表策略 主键+分片算法
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=tid
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=table-inline
# 配置 分片算法
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression=t_order_$->{tid % 2}
# 主键盘生成策略
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=tid
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=1
# 打印执行sql
spring.shardingsphere.props.sql-show=true
# 配置分库策略  主键+分片算法
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column=goods_id
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name=database-inline
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=shardingspheredb$->{goods_id % 2 + 1}

相关文章:

shardingsphere分库分表跨库访问 添加分片规则

shardingsphere分库分表跨库访问 添加分片规则 建立 JDBC 环境 创建表 t_order&#xff1a; CREATE TABLE t_order (tid bigint(20) NOT NULL,tname varchar(255) DEFAULT NULL,goods_id bigint(20) DEFAULT NULL,tstatus varchar(255) DEFAULT NULL,PRIMARY KEY (tid) ) E…...

c++:std::map下标运算符的不合理使用

这是我分析之前遗留代码时发现的一个隐藏点&#xff1b;不过我并不认为这样使用std::map是合理的。 看看简化后的代码&#xff0c;v1、v2的值应该是多少呢&#xff1f; #include <map>std::map<int, int> cm[2];int get_cm_value(int device, int ctrl) { auto …...

KeyFormer:使用注意力分数压缩KV缓存

Keyformer: KV Cache Reduction through Key Tokens Selection for Efficient Generative Inference 202403&#xff0c;发表在Mlsys Introduction 优化KV cache的策略&#xff0c;主要是集中在系统级别的优化上&#xff0c;比如FlashAttention、PagedAttention&#xff0c;它…...

MetaGPT源码 (ContextMixin 类)

目录 理解 ContextMixin什么是 ContextMixin&#xff1f;主要组件实现细节 测试 ContextMixin示例&#xff1a;ModelX1. 配置优先级2. 多继承3. 多继承重写4. 配置优先级 在本文中&#xff0c;我们将探索 ContextMixin 类&#xff0c;它在多重继承场景中的集成及其在 Python 配…...

MATLAB生成.exe独立程序过程(常见问题解决方法)(2024.12.14)

本文只记录我执行过程中遇到的关键问题、以及解决方法&#xff0c;不讲诉整个流程。 电脑环境 win11系统 matlab 2024b 版本 整体流程 1.下载matlab运行时库,简写为MCR 2.配置MCR环境 3.打包程序 4.目标机器安装程序 一、下载MCR 下载这个折腾了大半天&#xff0c;大概问题就是…...

PHP排序算法:数组内有A~E,A移到C或者C移到B后排序,还按原顺序排序,循环

效果 PHP代码 public function demo($params){function moveNext($arr){$length count($arr);$lastElement $arr[$length - 1];for ($i $length - 1; $i > 0; $i--) {$arr[$i] $arr[$i - 1];}$arr[0] $lastElement;return $arr;}function moveAndReplace($array, $from…...

ChatGPT搜索全新升级,向全体用户开放,近屿智能助力AI行业发展

12月17日&#xff0c;OpenAI在第八天直播中正式宣布ChatGPT搜索功能全面升级&#xff0c;并即日起对所有ChatGPT用户开放。此次更新不仅带来了显著的性能提升&#xff0c;还引入了多项突破性功能&#xff0c;如更快的搜索速度、全新的地图体验以及YouTube视频嵌入&#xff0c;为…...

win10配置免密ssh登录远程的ubuntu

为了在终端ssh远程和使用VScode远程我的VM上的ubuntu不需要设置密码&#xff0c;需要在win10配置免密ssh登录远程的ubuntu。 在win10打开cmd&#xff0c;执行下面的代码生成密钥对&#xff08;会提示进行设置&#xff0c;按照默认的配置就行&#xff0c;一直回车&#xff09;&…...

skywalking 搭建 备忘录

基础环境 apache-skywalking-apm-9.6.0.tar.gz apache-skywalking-java-agent-9.1.0.tgz elasticsearch 7.14.1 采用dockers搭建 或者手动部署 kibana 可视化 应用 微服务版 consumer.jar eureka.jar 注册中心 provider.jar skywalking 地址 https://skywalkin…...

linux日常常用命令(AI向)

进程挂后台运行 nohup sh ./scripts/*****.sh > ./output/*****.log 2>&1 &删除***用户的所有python进程 pkill -u *** -f "^python"列出“***”用户的进程信息 ps aux --sort-%mem | grep ^***git add ./*git commit -m "注释"git push …...

信奥赛CSP-J复赛集训(bfs专题)(5):洛谷P3395:路障

信奥赛CSP-J复赛集训(bfs专题-刷题题单及题解)(5):洛谷P3395:路障 题目描述 B 君站在一个 n n n\times n n...

《红队和蓝队在网络安全中的定义与分工》

网络安全中什么是红队蓝队 在网络安全领域&#xff0c;红队和蓝队是一种对抗性的演练机制&#xff0c;用于测试和提升网络安全防御能力。 红队&#xff08;Red Team&#xff09; 定义与目标 红队是扮演攻击者角色的团队。他们的主要任务是模拟真实的网络攻击&#xff0c;利用各…...

李宏毅深度强化学习入门笔记:PPO

李宏毅-深度强化学习-入门笔记&#xff1a;PPO 一、Policy Gradient&#xff08;一&#xff09;基本元素&#xff08;二&#xff09;Policy of Actor1. Policy π \pi π 是带有参数 θ \theta θ 的 network2. 例子&#xff1a;运行流程 &#xff08;三&#xff09;Actor, E…...

vue2项目中如何把rem设置为固定的100px

在 Vue 2 项目中&#xff0c;可以通过动态设置 html 元素的 font-size 来将 1rem 固定为 100px。以下是具体步骤&#xff1a; 在项目的入口文件 main.js 中添加以下代码&#xff0c;用于动态设置 html 的 font-size&#xff1a; // main.js function setHtmlFontSize() {cons…...

C++多线程常用方法

在 C 中&#xff0c;线程相关功能主要通过头文件提供的类和函数来实现&#xff0c;以下是一些常用的线程接口方法和使用技巧&#xff1a; std::thread类 构造函数&#xff1a; 可以通过传入可调用对象&#xff08;如函数指针、函数对象、lambda 表达式等&#xff09;来创建一…...

ubuntu+ros新手笔记(三):21讲没讲到的MoveIt2

1 安装MoveIt2 安装参照在ROS2中&#xff0c;通过MoveIt2控制Gazebo中的自定义机械手 安装 MoveIt2可以选择自己编译源码安装&#xff0c;或者直接从二进制安装。 个人建议直接二进制安装&#xff0c;可以省很多事。 sudo apt install ros-humble-moveitmoveit-setup-assistan…...

Android Studio创建新项目并引入第三方so外部aar库驱动NFC读写器读写IC卡

本示例使用设备&#xff1a;https://item.taobao.com/item.htm?spma21dvs.23580594.0.0.52de2c1bbW3AUC&ftt&id615391857885 一、打开Android Studio,点击 File> New>New project 菜单&#xff0c;选择 要创建的项目模版&#xff0c;点击 Next 二、输入项目名称…...

window QT/C++ 与 lua交互(mingw + lua + LuaBridge + luasocket)

一、环境与准备工作 测试环境:win10 编译器:mingw QT版本:QT5.12.3 下载三种源码: LuaBridge源码:https://github.com/vinniefalco/LuaBridge LUA源码(本测试用的是5.3.5):https://www.lua.org/download.html luasocket源码:https://github.com/diegonehab/luasocket 目…...

中阳科技:量化模型驱动的智能交易革命

在金融市场飞速发展的今天&#xff0c;量化交易作为科技与金融的深度融合&#xff0c;正推动市场格局向智能化转型。中阳科技凭借先进的数据分析技术与算法研发能力&#xff0c;探索量化模型的升级与优化&#xff0c;为投资者提供高效、智能的交易解决方案。 量化交易的本质与价…...

电子应用设计方案-56:智能书柜系统方案设计

智能书柜系统方案设计 一、引言 随着数字化时代的发展和人们对知识获取的需求增加&#xff0c;智能书柜作为一种创新的图书管理和存储解决方案&#xff0c;能够提供更高效、便捷和个性化的服务。本方案旨在设计一款功能齐全、智能化程度高的智能书柜系统。 二、系统概述 1. 系…...

宠物兔需要洗澡吗?

在宠物兔的养护领域&#xff0c;“宠物兔需要洗澡吗” 这个问题一直备受争议。其实&#xff0c;这不能简单地一概而论&#xff0c;而要综合多方面因素考量。 兔子本身是爱干净的动物&#xff0c;它们日常会通过自我舔舐来打理毛发。从这个角度讲&#xff0c;如果兔子生活环境较…...

ubuntu升级python版本

Ubuntu升级Python版本 解压缩文件&#xff1a; 下载完成后&#xff0c;解压缩文件&#xff1a; tar -xf Python-3.12.0.tgz编译并安装&#xff1a; 进入解压后的目录&#xff0c;然后配置和安装Python&#xff1a; codecd Python-3.12.0 ./configure --enable-optimizations ma…...

《Time Ghost》的制作:使用 DOTS ECS 制作更为复杂的大型环境

*基于 Unity 6 引擎制作的 demo 《Time Ghost》 开始《Time Ghost》项目时的目标之一是提升在 Unity 中构建大型户外环境的构建标准。为了实现这一目标&#xff0c;我们要有处理更为复杂的场景的能力、有足够的工具支持&#xff0c;同时它对引擎的核心图形、光照、后处理、渲染…...

详细描述一下 Elasticsearch 更新和删除文档的过程。

1、删 除 和 更 新 也 都 是 写 操 作 &#xff0c; 但 是 Elasticsearch 中的 文 档 是 不 可 变 的 &#xff0c; 因 此 不 能被 删 除 或 者 改 动 以 展 示 其 变 更 &#xff1b; 2、磁盘 上 的 每 个 段 都 有 一 个 相 应 的 .del 文件 。当删 除 请 求 发 送 后 &#…...

OpenCV与Qt5开发卡尺找圆工具

文章目录 前言一、卡尺原理二、1D边缘提取三、圆拟合四、软件实现结束语 基于OpenCV与Qt5构建卡尺找圆工具 前言 博主近期基于海康Vision Master4.0做了一个工业视觉工程项目&#xff0c;其中就使用到了海康VM的找圆工具&#xff0c;然后博主根据其中的技术原理&#xff0c;也…...

【网络安全】Web Timing 和竞争条件攻击:揭开隐藏的攻击面

Web Timing 和竞争条件攻击&#xff1a;揭开隐藏的攻击面 在传统的 Web 应用中&#xff0c;漏洞的发现和利用通常相对容易理解。如果代码存在问题&#xff0c;我们可以通过发送特定输入来强制 Web 应用执行非预期的操作。这种情况下&#xff0c;输入和输出之间往往有直接关系&…...

分立器件---运算放大器关键参数

运算放大器 关键参数 1、供电电压:有单电源电压、双电源电压,双电源电压尽量两个电源都接。如图LM358B,供电电压可以是20V或者是40V和GND。 2、输入偏置电流IB:当运放输出直流电压为零时,运放两个输入端流进或者流出直流电流的平均值。同向输入端电流IB+与反向输入端电流…...

Stable Diffusion Controlnet常用控制类型解析与实战课程 4

本节内容&#xff0c;是stable diffusion Controlnet常用控制类型解析与实战的第四节课程。上节课程&#xff0c;我们陆续讲解了几个与图像风格约束相关的控制类型&#xff0c;本节课程我们再学习一些实用价值较高的控制类型&#xff0c;看一看他们提供了哪些控制思路。 一&…...

Linux 本地编译安装 gcc9

这里演示非sudo权限的本地linux 用户安装 gcc9 下载源代码&#xff1a; 可以从GCC官方网站或其镜像站点下载GCC 9的源代码压缩包。使用wget或curl命令&#xff0c;这通常不需要额外权限 wget https://ftp.gnu.org/gnu/gcc/gcc-9.5.0/gcc-9.5.0.tar.gz tar -xf gcc-9.5.0.tar…...

SpringBoot 自定义事件

在Spring Boot中&#xff0c;自定义事件和监听器是一种强大的机制&#xff0c;允许你在应用程序的不同部分之间进行解耦通信。你可以定义自定义事件&#xff0c;并在需要的时候发布这些事件&#xff0c;同时让其他组件通过监听器来响应这些事件。 以下是如何在Spring Boot中创…...