1 前言
垂直分片分为垂直分库于垂直分表,但是实际应用中数据库表会根据业务需要提前做好规划,垂直分表的情况很少使用。但是随着业务增长,某些重点业务数据库压力增大,可能考虑需要考虑垂直分库的情况。
垂直分库,简单表述就是根据表业务不同,放置在不同服务器中。下面就这种情况做下配置演示。
2 创建容器
服务器规划:使用docker创建如下容器
- 服务器:容器名
server-user
,端口3301 - 服务器:容器名
server-order
,端口3302
2.1 创建server-user容器
Step1:创建容器
docker run -it -p 3309:3306 --name mysql8-master --privileged=true \
-v /Users/gaogzhen/data/docker/mysql/mysql8/server-user/conf/conf.d:/etc/mysql/conf.d \
-v /Users/gaogzhen/data/docker/mysql/mysql8/server-user/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql
Step2:登录MySQL服务器
# 进入容器
docker exec -it server-user env LANG=c.UTF-8 /bin/bash
# 连接mysql服务
mysql -uroot -p
# 修改默认密码插件
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Step3:创建数据库
CREATE DATABASE db_user;
use db_user;
CREATE TABLE t_user(
id BIGINT AUTO_INCREMENT,
name VARCHAR(30),
PRIMARY KEY(id)
);
2.2 创建server-order 容器
Step1:创建容器
docker run -it -p 3302:3306 --name server-order --privileged=true \
-v /Users/gaogzhen/data/docker/mysql/mysql8/server-order/conf/conf.d:/etc/mysql/conf.d \
-v /Users/gaogzhen/data/docker/mysql/mysql8/server-order/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql
docker run -it -p 3302:3306 --name server-order --privileged=true -v /Users/gaogzhen/data/docker/mysql/mysql8/server-order/conf/conf.d:/etc/mysql/conf.d -v /Users/gaogzhen/data/docker/mysql/mysql8/server-order/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
Step2:登录MySQL服务器
# 进入容器
docker exec -it server-order env LANG=c.UTF-8 /bin/bash
# 连接mysql服务
mysql -uroot -p
# 修改默认密码插件
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Step3:创建数据库
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)
);
3 编写程序
生成entity、service与mapper参考上一篇文章连接2.
4 配置
# 应用名称
spring.application.name=sharging-jdbc-demo
# 开发环境设置
spring.profiles.active=dev
# 内存模式
spring.shardingsphere.mode.type=Memory
# 配置真实数据源
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.jdbc-url=jdbc:mysql://127.0.0.1:3301/db_user?allowPublicKeyRetrieval=true&useSSL=false
spring.shardingsphere.datasource.server-user.username=root
spring.shardingsphere.datasource.server-user.password=123456
# 配置第 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.jdbc-url=jdbc:mysql://127.0.0.1:3302/db_order?allowPublicKeyRetrieval=true&useSSL=false
spring.shardingsphere.datasource.server-order.username=root
spring.shardingsphere.datasource.server-order.password=123456
# 标准分片表配置
## 有数据源+表名组成,一小数点分割。多个表以逗号分割,支持inline表达式
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
# mybatis plus 配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.gaogzhen.shardingjdbcdemo.entity
- 指定逻辑表与真实数据源.表的对应关系,那么对于逻辑表的操作,最终会路由到真实表。
5 测试
package com.gaogzhen.shardingjdbcdemo;
import com.gaogzhen.shardingjdbcdemo.entity.Order;
import com.gaogzhen.shardingjdbcdemo.entity.User;
import com.gaogzhen.shardingjdbcdemo.mapper.OrderMapper;
import com.gaogzhen.shardingjdbcdemo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.math.BigDecimal;
@SpringBootTest
public class VerticalSharingTest {
@Resource
private UserMapper userMapper;
@Resource
private OrderMapper orderMapper;
@Test
public void testInsert() {
User user = new User();
user.setName("张三");
userMapper.insert(user);
Order order = new Order();
order.setUserId(user.getId());
order.setOrderNo("1111");
order.setAmount(BigDecimal.valueOf(10));
orderMapper.insert(order);
}
@Test
public void testSelect() {
User user = userMapper.selectById(1L);
Order order = orderMapper.selectById(1L);
}
}
- 测试插入
- 测试查询
结语
如果小伙伴什么问题或者指教,欢迎交流。
QQ:806797785
仓库源代码地址:https://gitee.com/gaogzhen/shardingsphere-jdbc-demo.git
参考链接:
[1]ShardingSphere5实战教程[CP/OL].2022-09-14.p18-23.
[2]0101读写分离测试-jdbc-shardingsphere-中间件[CP/OL].