Sharding-JDBC之水平分表

news/2024/9/23 4:36:12/

目录

    • 一、简介
    • 1.1、垂直分表
    • 1.2、水平分表
    • 二、maven依赖
    • 三、数据库
      • 3.1、创建数据库
      • 3.2、创建表
    • 四、配置(二选一)
      • 4.1、properties配置
      • 4.2、yml配置
    • 五、实现
      • 5.1、实体
      • 5.2、持久层
      • 5.3、服务层
      • 5.4、测试类
        • 5.4.1、保存数据
        • 5.4.2、查询数据

一、简介

1.1、垂直分表

  垂直分片又称为纵向拆分。最简单的就是单库把一个表拆成多个关联的表,通常是一个表中存储的信息类型比较多,比如一二十个字段,但是经常用到的可能又比较少,频繁操作就影响性能,所以就把大表拆分成多个小表,比如 tb_course 拆分成 tb_course tb_course_detail 。这个主要是数据库设计的相关知识,也和我们这里说的 Sharding-JDBC 关系不是那么大,我就不多进行讲解,所以后面也不会讲垂直分表这种了,主要是我们要讲的水平分表。

1.2、水平分表

  水平分片又称为横向拆分。 最简单的就是单库水平分表,也就是一个库中有两个一模一样的表,只是名称不一样比如: tb_order_1 tb_order_2 ,如下图所示。

在这里插入图片描述

二、maven依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.alian</groupId><artifactId>sharding-jdbc</artifactId><version>0.0.1-SNAPSHOT</version><name>sharding-jdbc</name><description>sharding-jdbc</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.1.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.15</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

  有些小伙伴的 druid 可能用的是 druid-spring-boot-starter

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version>
</dependency>

  然后出现可能使用不了的各种问题,这个时候你只需要在主类上添加 @SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class}) 即可

package com.alian.shardingjdbc;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
@SpringBootApplication
public class ShardingJdbcApplication {public static void main(String[] args) {SpringApplication.run(ShardingJdbcApplication.class, args);}}

三、数据库

3.1、创建数据库

sharding_0

CREATE DATABASE `sharding_0` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

3.2、创建表

tb_order_1

CREATE TABLE `tb_order_1` (`order_id` bigint(20) NOT NULL COMMENT '主键',`user_id` int unsigned NOT NULL DEFAULT '0' COMMENT '用户id',`price` int unsigned NOT NULL DEFAULT '0' COMMENT '价格(单位:分)',`order_status` tinyint unsigned NOT NULL DEFAULT '1' COMMENT '订单状态(1:待付款,2:已付款,3:已取消)',`order_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`title` varchar(100)  NOT NULL DEFAULT '' COMMENT '订单标题',PRIMARY KEY (`order_id`),KEY `idx_user_id` (`user_id`),KEY `idx_order_time` (`order_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';

tb_order_2

CREATE TABLE `tb_order_2` (`order_id` bigint(20) NOT NULL COMMENT '主键',`user_id` int unsigned NOT NULL DEFAULT '0' COMMENT '用户id',`price` int unsigned NOT NULL DEFAULT '0' COMMENT '价格(单位:分)',`order_status` tinyint unsigned NOT NULL DEFAULT '1' COMMENT '订单状态(1:待付款,2:已付款,3:已取消)',`order_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`title` varchar(100)  NOT NULL DEFAULT '' COMMENT '订单标题',PRIMARY KEY (`order_id`),KEY `idx_user_id` (`user_id`),KEY `idx_order_time` (`order_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';

四、配置(二选一)

4.1、properties配置

application.properties

server.port=8899
server.servlet.context-path=/sharding-jdbc# 允许一个实体映射多个表
#spring.main.allow-bean-definition-overriding=true
# 数据源名称,多数据源以逗号分隔
spring.shardingsphere.datasource.names=ds1
# 数据库连接池类名称
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
# 数据库驱动类名
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库url连接
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://192.168.19.129:3306/sharding_0?serverTimezone=GMT%2B8&characterEncoding=utf8&useUnicode=true&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&autoReconnect=true&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=6000&maxReconnects=5
# 数据库用户名
spring.shardingsphere.datasource.ds1.username=alian
# 数据库密码
spring.shardingsphere.datasource.ds1.password=123456# 指定tb_order表的数据分布情况,配置数据节点,使用Groovy的表达式,逻辑表tb_order对应的节点是:ds1.tb_order_1, ds1.tb_order_2
spring.shardingsphere.sharding.tables.tb_order.actual-data-nodes=ds1.tb_order_$->{1..2}# 采用行表达式分片策略:InlineShardingStrategy
# 指定tb_order表的分片策略中的分片键
spring.shardingsphere.sharding.tables.tb_order.table-strategy.inline.sharding-column=order_id
# 指定tb_order表的分片策略中的分片算法表达式,使用Groovy的表达式
spring.shardingsphere.sharding.tables.tb_order.table-strategy.inline.algorithm-expression=tb_order_$->{ order_id % 2 == 0 ? 2 : 1 }# 指定tb_order表的主键为order_id
spring.shardingsphere.sharding.tables.tb_order.key-generator.column=order_id
# 指定tb_order表的主键生成策略为SNOWFLAKE
spring.shardingsphere.sharding.tables.tb_order.key-generator.type=SNOWFLAKE
# 指定雪花算法的worker.id
spring.shardingsphere.sharding.tables.tb_order.key-generator.props.worker.id=100
# 指定雪花算法的max.tolerate.time.difference.milliseconds
spring.shardingsphere.sharding.tables.tb_order.key-generator.props.max.tolerate.time.difference.milliseconds=20# 打开sql输出日志
spring.shardingsphere.props.sql.show=true

4.2、yml配置

application.yml

server:port: 8899servlet:context-path: /sharding-jdbcspring:main:# 允许定义相同的bean对象去覆盖原有的allow-bean-definition-overriding: trueshardingsphere:props:sql:# 打开sql输出日志show: truedatasource:# 数据源名称,多数据源以逗号分隔names: ds1ds1:# 数据库连接池类名称type: com.alibaba.druid.pool.DruidDataSource# 数据库驱动类名driver-class-name: com.mysql.cj.jdbc.Driver# 数据库url连接url: jdbc:mysql://192.168.19.129:3306/sharding_0?serverTimezone=GMT%2B8&characterEncoding=utf8&useUnicode=true&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&autoReconnect=true&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=6000&maxReconnects=5# 数据库用户名username: alian# 数据库密码password: 123456sharding:# 未配置分片规则的表将通过默认数据源定位default-data-source-name: ds1tables:tb_order:# 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式actual-data-nodes: ds1.tb_order_$->{1..2}# 分表策略table-strategy:# 行表达式分片策略inline:# 分片键sharding-column: order_id# 算法表达式algorithm-expression: tb_order_$->{order_id%2==0?2:1}# key生成器key-generator:# 自增列名称,缺省表示不使用自增主键生成器column: order_id# 自增列值生成器类型,缺省表示使用默认自增列值生成器(SNOWFLAKE/UUID)type: SNOWFLAKE# SnowflakeShardingKeyGeneratorprops:# SNOWFLAKE算法的worker.idworker:id: 100# SNOWFLAKE算法的max.tolerate.time.difference.millisecondsmax:tolerate:time:difference:milliseconds: 20
  • actual-data-nodes :使用Groovy的表达式 ds1.tb_order_$->{1…2},表示逻辑表tb_order对应的物理表是:ds1.tb_order_1ds1.tb_order_2
  • key-generator :key生成器,需要指定字段和类型,如果是SNOWFLAKE,最好也配置下props中的两个属性: worker.id max.tolerate.time.difference.milliseconds 属性(主要还是yml中配置)
  • table-strategy 表的分片策略,这里只是一个简单的奇数偶数,采用的是 行表达式分片策略 ,需要指定分片键和分片算法表达式(算法支持Groovy的表达式)

五、实现

5.1、实体

Order.java

@Data
@Entity
@Table(name = "tb_order")
public class Order implements Serializable {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "order_id")private Long orderId;@Column(name = "user_id")private Integer userId;@Column(name = "price")private Integer price;@Column(name = "order_status")private Integer orderStatus;@Column(name = "title")private String title;@Column(name = "order_time")private LocalDateTime orderTime;}

5.2、持久层

OrderRepository.java

public interface OrderRepository extends PagingAndSortingRepository<Order, Long> {/*** 根据订单id查询订单* @param orderId* @return*/Order findOrderByOrderId(Long orderId);
}

5.3、服务层

OrderService.java

@Slf4j
@Service
public class OrderService {@Autowiredprivate OrderRepository orderRepository;public void saveOrder(Order order) {orderRepository.save(order);}public Order queryOrder(Long orderId) {return orderRepository.findOrderByOrderId(orderId);}
}

5.4、测试类

OrderTests.java

@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class OrderTests {@Autowiredprivate OrderService orderService;@Testpublic void saveOrder() {for (int i = 0; i < 10; i++) {Order order = new Order();order.setUserId(1000);// 随机生成50到100的金额int price = (int) Math.round(Math.random() * (10000 - 5000) + 5000);order.setPrice(price);order.setOrderStatus(2);order.setOrderTime(LocalDateTime.now());order.setTitle("");orderService.saveOrder(order);}}@Testpublic void queryOrder() {Long orderId = 845308485917736960L;Order order = orderService.queryOrder(orderId);log.info("查询的结果:{}", order);}
}

5.4.1、保存数据

结果如下:

14:34:08 180 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 181 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_2 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:07.717, 7601, , 1000, 845308485917736960]
14:34:08 231 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 231 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_1 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.23, 7175, , 1000, 845308486400081921]
14:34:08 255 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 256 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_2 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.254, 5318, , 1000, 845308486504939520]
14:34:08 277 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 278 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_1 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.276, 5490, , 1000, 845308486597214209]
14:34:08 299 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 300 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_2 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.298, 7559, , 1000, 845308486689488896]
14:34:08 321 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 321 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_1 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.32, 9422, , 1000, 845308486777569281]
14:34:08 342 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 343 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_2 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.341, 7935, , 1000, 845308486869843968]
14:34:08 362 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 363 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_1 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.361, 8939, , 1000, 845308486953730049]
14:34:08 383 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 383 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_2 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.382, 7499, , 1000, 845308487041810432]
14:34:08 408 INFO [main]:Logic SQL: insert into tb_order (order_status, order_time, price, title, user_id) values (?, ?, ?, ?, ?)
14:34:08 408 INFO [main]:Actual SQL: ds1 ::: insert into tb_order_1 (order_status, order_time, price, title, user_id, order_id) values (?, ?, ?, ?, ?, ?) ::: [2, 2023-03-22 14:34:08.407, 9598, , 1000, 845308487146668033]

效果图:

在这里插入图片描述

5.4.2、查询数据

  从上面的结果我们可以看到order_id为 845308485917736960 的记录在 tb_order_2 表,我们查询时实际的查询也是去这个表中查的,请看下面的 Actual SQL

15:03:09 579 INFO [main]:Logic SQL: select order0_.order_id as order_id1_0_, order0_.order_status as order_st2_0_, order0_.order_time as order_ti3_0_, order0_.price as price4_0_, order0_.title as title5_0_, order0_.user_id as user_id6_0_ from tb_order order0_ where order0_.order_id=?
15:03:09 580 INFO [main]:Actual SQL: ds1 ::: select order0_.order_id as order_id1_0_, order0_.order_status as order_st2_0_, order0_.order_time as order_ti3_0_, order0_.price as price4_0_, order0_.title as title5_0_, order0_.user_id as user_id6_0_ from tb_order_2 order0_ where order0_.order_id=? ::: [845308485917736960]
15:03:09 621 INFO [main]:查询的结果:Order(orderId=845308485917736960, userId=1000, price=7601, orderStatus=2, title=, orderTime=2023-03-22T14:34:08)

http://www.ppmy.cn/news/43031.html

相关文章

nginx反向代理_负载均衡的配置

说明 两台虚拟机&#xff1a; 88节点是自己的虚拟机 66节点是小组成员的虚拟机&#xff0c;我们暂且叫同学机 tomcat端口&#xff0c;分别为8081和8082 总结就是&#xff1a; 自己虚拟机上面安装nginx和tomcat8082 同学机上安装tomcat8081 一、开始安装nginx&#xff08;只安装…

在Spring Boot微服务使用Jedis操作Redis String字符串

记录&#xff1a;407 场景&#xff1a;在Spring Boot微服务使用Jedis操作Redis String字符串。 版本&#xff1a;JDK 1.8,Spring Boot 2.6.3,redis-6.2.5,jedis-3.7.1。 1.微服务中配置Redis信息 1.1在application.yml中Jedis配置信息 hub:example:redis:jedis:host: 192.…

Matlab论文插图绘制模板第85期—模值赋色的箭头图

在之前的文章中&#xff0c;分享了Matlab箭头图的绘制模板&#xff1a; 进一步&#xff0c;如果我们想对每一个箭头赋上颜色&#xff0c;以更加直观地表示其模值的大小&#xff0c;该怎么操作呢&#xff1f; 那么&#xff0c;来看一下模值赋色的箭头图的绘制模板。 先来看一下…

EightCap易汇:外汇投资入门需要了解哪些必要知识?

外汇市场是国际投资市场&#xff0c;日内交易量巨大&#xff0c;盈利机会极多。外汇是一种含有杠杆的投资产品&#xff0c;杠杆带来了高收益&#xff0c;也会带来高风险&#xff0c;对于外汇新手来说存在一定难度。新手投资者要如何交易&#xff0c;才能抓住外汇市场的盈利机会…

山东双软认证需要什么条件

山东双软认证需要什么条件 ​ 一、软件企业认定服务 1.软件企业认定基本条件 ①在我国境内成立&#xff0c;有固定办公场所&#xff0c;以软件开发销售为主营业务的企业&#xff1b; ②大专学历人数≧当年月平均人数的40%&#xff0c;研发人员≧当年月平均人数20%&#xff1b…

由libunifex来看Executor的任务构建

前言 之前的一篇文章讲述了future的优缺点&#xff0c;以及future的组合性&#xff0c;其中也讲述了构建任务DAG一些问题&#xff0c;同时给出了比较好的方案则是Executor。 Executor还未进入标准&#xff08;C23&#xff09;&#xff0c;Executor拥有惰性构建及良好的抽象模型…

登录界面到个人中心

登录页面&#xff1a; 1. 使用路由渲染登录组件 在 /src/views 目录之下&#xff0c;创建 Login 文件夹&#xff0c;并在其下新建 Login.vue登录组件 在 /src/router/index.js 路由模块中&#xff0c;导入需要通过路由渲染的 login.vue 登录组件 在路由模块的 routes 数组中…

c++11 标准模板(STL)(std::unordered_multimap)(十三)

定义于头文件 <unordered_map> template< class Key, class T, class Hash std::hash<Key>, class KeyEqual std::equal_to<Key>, class Allocator std::allocator< std::pair<const Key, T> > > class unordered…