江宁网站建设价格,建设音乐网站的目的,wordpress 3d旋转,开发微信公众什么是数据分片#xff1f; 简单来说#xff0c;就是指通过某种特定的条件#xff0c;将我们存放在同一个数据库中的数据分散存放到多个数据库#xff08;主机#xff09;上面#xff0c;以达到分散单台设备负载的效果。 数据的切分#xff08;Sharding#xff09;根据… 什么是数据分片 简单来说就是指通过某种特定的条件将我们存放在同一个数据库中的数据分散存放到多个数据库主机上面以达到分散单台设备负载的效果。 数据的切分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_PASSWORDroot \
--name server-user \
mysql:8.0.29
2登录MySQL服务器 ① 进入容器 docker exec -it server-user env LANGC.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_PASSWORDroot \
--name server-order \
mysql:8.0.29
2登录MySQL服务器 ① 进入容器 docker exec -it server-order env LANGC.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 BaseMapperOrder {
}
3. 配置垂直分片 垂直拆分 从不同的库中加载多张不同的表在一个项目中使用 # 应用名称
spring.application.namesharding-jdbc-demo
# 环境设置
spring.profiles.activedev# 配置真实数据源
spring.shardingsphere.datasource.namesserver-user,server-order# 配置第 1 个数据源
spring.shardingsphere.datasource.server-user.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-user.driver-class-namecom.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-user.urljdbc:mysql://192.168.100.201:3301/db_user
spring.shardingsphere.datasource.server-user.usernameroot
spring.shardingsphere.datasource.server-user.passwordroot# 配置第 2 个数据源
spring.shardingsphere.datasource.server-order.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-namecom.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.urljdbc:mysql://192.168.100.201:3302/db_order
spring.shardingsphere.datasource.server-order.usernameroot
spring.shardingsphere.datasource.server-order.passwordroot# 标准分片表配置数据节点
# table-name逻辑表名匹配 数据源名.真实表
#spring.shardingsphere.rules.sharding.tables.table-name.actual-data-nodes # 由数据源名 表名组成以小数点分隔。
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodesserver-user.t_user
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodesserver-order.t_order
# 打印SQL
spring.shardingsphere.props.sql-showtrue user库order主库从库两个如下配置 # 配置真实数据源
spring.shardingsphere.datasource.namesserver-user,server-order# 配置第 1 个数据源user库的数据源
spring.shardingsphere.datasource.server-user.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-user.driver-class-namecom.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-user.urljdbc:mysql://192.168.1.171:3326/db_user
spring.shardingsphere.datasource.server-user.usernameroot
spring.shardingsphere.datasource.server-user.password123456# 配置第 2 个数据源:order库的数据源(order库可以配置读写分离)
spring.shardingsphere.datasource.server-order.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-namecom.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.urljdbc:mysql://192.168.1.171:3316/mydb3
spring.shardingsphere.datasource.server-order.usernameroot
spring.shardingsphere.datasource.server-order.password123456# 标准分片表配置数据节点
# table-name逻辑表名匹配 数据源名.真实表
#spring.shardingsphere.rules.sharding.tables.table-name.actual-data-nodes # 由数据源名 表名组成以小数点分隔。
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodesserver-user.t_user
spring.shardingsphere.rules.sharding.tables.t_account.actual-data-nodesserver-user.t_account
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodesserver-order.t_order
# 打印SQL
spring.shardingsphere.props.sql-showtrue# 为订单库 mydb3配置读写分离
# 配置第 2 个数据源的salve1数据源
spring.shardingsphere.datasource.order-slave1.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.order-slave1.driver-class-namecom.mysql.jdbc.Driver
spring.shardingsphere.datasource.order-slave1.urljdbc:mysql://192.168.1.171:3307/mydb3
spring.shardingsphere.datasource.order-slave1.usernameroot
spring.shardingsphere.datasource.order-slave1.password123456
# 配置第 2 个数据源的salve2数据源
spring.shardingsphere.datasource.order-slave2.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.order-slave2.driver-class-namecom.mysql.jdbc.Driver
spring.shardingsphere.datasource.order-slave2.urljdbc:mysql://192.168.1.171:3308/mydb3
spring.shardingsphere.datasource.order-slave2.usernameroot
spring.shardingsphere.datasource.order-slave2.password123456
## 读写分离配置
# 读写分离类型如: StaticDynamic
# Static数据源在配置文件中是直接配置的
# Dynamic数据源是由程序动态读取的
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.typeStatic
# 写数据源名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.write-data-source-nameserver-order
# 读数据源名称多个从数据源用逗号分隔
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.read-data-source-namesserver-order,order-slave1,order-slave2
# 负载均衡算法名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.load-balancer-namealg_round
# 负载均衡算法配置
# 负载均衡算法类型ROUND_ROBIN、RANDOM、WEIGHT
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_round.typeROUND_ROBIN
# 负载均衡算法属性配置typeWEIGHT 的时候配置
#spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave11
#spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave21
三、测试垂直分片
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);}
}