网站建设推广浩森宇特浙江seo外包
什么是数据分片?
简单来说,就是指通过某种特定的条件,将我们存放在同一个数据库中的数据分散存放到多个数据库(主机)上面,以达到分散单台设备负载的效果。
数据的切分(Sharding)根据其切分规则的类型,可以分为两种切分模式:
垂直(纵向)切分:是按照不同的表(或者 Schema)来切分到不同的数据库(主机)之上
水平(横向)切分:是根据表中的数据的逻辑关系,将同一个表中的数据按照某种条件拆分到多台数据库(主机)上面。
注意:分库分表必须是干净的库和表(不能有数据)
分片原则:
能不切分尽量不要切分。数据量不是很大的库或者表,尽量不要分片。
尽量按照功能模块分库,避免跨库join。
项目中可以使用ShardingSphere-JDBC加载不同库中的表进行操作
一、准备服务器
服务器规划:使用docker方式创建如下容器
- 主服务器:容器名server-user,端口3301
- 从服务器:容器名server-order,端口3302
1. 创建server-user容器
(1)创建容器
docker run -d \
-p 3301:3306 \
-v /liush/server/user/conf:/etc/mysql/conf.d \
-v /liush/server/user/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
--name server-user \
mysql:8.0.29
(2)登录MySQL服务器
① 进入容器
docker exec -it server-user env LANG=C.UTF-8 /bin/bash
② 进入容器内的mysql命令行
mysql -uroot -proot
③ 修改默认密码插件
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
(3)创建数据库
CREATE DATABASE db_user;
USE db_user;
CREATE TABLE t_user (id BIGINT AUTO_INCREMENT,uname VARCHAR(30),PRIMARY KEY (id)
);
2. 创建server-order容器
(1)创建容器
docker run -d \
-p 3302:3306 \
-v /liush/server/order/conf:/etc/mysql/conf.d \
-v /liush/server/order/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
--name server-order \
mysql:8.0.29
(2)登录MySQL服务器
① 进入容器:
docker exec -it server-order env LANG=C.UTF-8 /bin/bash
② 进入容器内的mysql命令行
mysql -uroot -proot
③ 修改默认密码插件
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
(3)创建数据库
CREATE DATABASE db_order;
USE db_order;
CREATE TABLE t_order (id BIGINT AUTO_INCREMENT,order_no VARCHAR(30),user_id BIGINT,amount DECIMAL(10,2),PRIMARY KEY(id)
);
二、程序实现
1. 创建实体类
@TableName("t_order")
@Data
public class Order {@TableId(type = IdType.AUTO)private Long id;private String orderNo;private Long userId;private BigDecimal amount;
}
2. 创建Mapper
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
}
3. 配置垂直分片
垂直拆分: 从不同的库中加载多张不同的表在一个项目中使用
# 应用名称
spring.application.name=sharding-jdbc-demo
# 环境设置
spring.profiles.active=dev# 配置真实数据源
spring.shardingsphere.datasource.names=server-user,server-order# 配置第 1 个数据源
spring.shardingsphere.datasource.server-user.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-user.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-user.url=jdbc:mysql://192.168.100.201:3301/db_user
spring.shardingsphere.datasource.server-user.username=root
spring.shardingsphere.datasource.server-user.password=root# 配置第 2 个数据源
spring.shardingsphere.datasource.server-order.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.url=jdbc:mysql://192.168.100.201:3302/db_order
spring.shardingsphere.datasource.server-order.username=root
spring.shardingsphere.datasource.server-order.password=root# 标准分片表配置(数据节点)
# <table-name>逻辑表名:匹配 数据源名.真实表
#spring.shardingsphere.rules.sharding.tables.<table-name>.actual-data-nodes= # 由数据源名 + 表名组成,以小数点分隔。
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=server-order.t_order
# 打印SQL
spring.shardingsphere.props.sql-show=true
user库,order主库+从库两个:如下配置
# 配置真实数据源
spring.shardingsphere.datasource.names=server-user,server-order# 配置第 1 个数据源:user库的数据源
spring.shardingsphere.datasource.server-user.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-user.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-user.url=jdbc:mysql://192.168.1.171:3326/db_user
spring.shardingsphere.datasource.server-user.username=root
spring.shardingsphere.datasource.server-user.password=123456# 配置第 2 个数据源:order库的数据源(order库可以配置读写分离)
spring.shardingsphere.datasource.server-order.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.url=jdbc:mysql://192.168.1.171:3316/mydb3
spring.shardingsphere.datasource.server-order.username=root
spring.shardingsphere.datasource.server-order.password=123456# 标准分片表配置(数据节点)
# <table-name>逻辑表名:匹配 数据源名.真实表
#spring.shardingsphere.rules.sharding.tables.<table-name>.actual-data-nodes= # 由数据源名 + 表名组成,以小数点分隔。
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user
spring.shardingsphere.rules.sharding.tables.t_account.actual-data-nodes=server-user.t_account
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=server-order.t_order
# 打印SQL
spring.shardingsphere.props.sql-show=true# =============为订单库 mydb3配置读写分离
# 配置第 2 个数据源的salve1数据源
spring.shardingsphere.datasource.order-slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.order-slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.order-slave1.url=jdbc:mysql://192.168.1.171:3307/mydb3
spring.shardingsphere.datasource.order-slave1.username=root
spring.shardingsphere.datasource.order-slave1.password=123456
# 配置第 2 个数据源的salve2数据源
spring.shardingsphere.datasource.order-slave2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.order-slave2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.order-slave2.url=jdbc:mysql://192.168.1.171:3308/mydb3
spring.shardingsphere.datasource.order-slave2.username=root
spring.shardingsphere.datasource.order-slave2.password=123456
## 读写分离配置
# 读写分离类型,如: Static,Dynamic
# Static:数据源在配置文件中是直接配置的
# Dynamic:数据源是由程序动态读取的
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.type=Static
# 写数据源名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.write-data-source-name=server-order
# 读数据源名称,多个从数据源用逗号分隔
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.read-data-source-names=server-order,order-slave1,order-slave2
# 负载均衡算法名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.load-balancer-name=alg_round
# 负载均衡算法配置
# 负载均衡算法类型:ROUND_ROBIN、RANDOM、WEIGHT
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_round.type=ROUND_ROBIN
# 负载均衡算法属性配置:type=WEIGHT 的时候配置
#spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave1=1
#spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave2=1
三、测试垂直分片
@SpringBootTest
public class ShardingTest {@Autowiredprivate UserMapper userMapper;@Autowiredprivate OrderMapper orderMapper;/*** 垂直分片:* user数据自动写入server-user服务器的的t_user表* order数据自动写入server-order服务器的的t_order表*/@Testpublic void testInsert1(){User user = new User();user.setUname("helen");userMapper.insert(user);Order order = new Order();order.setOrderNo("ATGUIGU");order.setAmount(new BigDecimal(100));order.setUserId(user.getId());orderMapper.insert(order);}
}