🚀 feat(新增): 完成

This commit is contained in:
Bunny 2024-07-29 22:45:55 +08:00
parent 32bd6284d5
commit fdc0ea469a
6 changed files with 90 additions and 23 deletions

View File

@ -30,7 +30,8 @@ public class Order implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.ASSIGN_ID)
// 当配置shardingSphere雪花算法这里的AUTO会使用shardingSphere的
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String orderNo;

View File

@ -2,6 +2,7 @@ package cn.bunny.service.controller;
import cn.bunny.entity.order.Order;
import cn.bunny.pojo.result.Result;
import cn.bunny.service.mapper.OrderMapper;
import cn.bunny.service.service.OrderService;
import cn.bunny.service.service.UserService;
import cn.bunny.vo.page.PageResult;
@ -11,6 +12,8 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
/**
* <p>
* 前端控制器
@ -27,6 +30,8 @@ public class OrderController {
private OrderService orderService;
@Autowired
private UserService userService;
@Autowired
private OrderMapper orderMapper;
@Operation(summary = "分页查询订单", description = "分页查询订单")
@GetMapping("queryPage/{page}/{limit}")
@ -39,7 +44,13 @@ public class OrderController {
@Operation(summary = "插入数据", description = "插入数据")
@PostMapping("addOrder")
public Result<Order> addOrder(@RequestBody Order order) {
orderService.save(order);
for (long i = 5; i < 9; i++) {
order = new Order();
order.setOrderNo("ATGUIGU" + i);
order.setUserId(2L);
order.setAmount(new BigDecimal(100));
orderMapper.insert(order);
}
return Result.success();
}
}

View File

@ -6,19 +6,19 @@ dataSources:
master:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.3.21:3306/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
jdbcUrl: jdbc:mysql://192.168.3.98:3306/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"
slave1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.3.21:3307/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
jdbcUrl: jdbc:mysql://192.168.3.98:3307/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"
slave2:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.3.21:3308/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
jdbcUrl: jdbc:mysql://192.168.3.98:3308/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"
rules:

View File

@ -7,23 +7,26 @@ dataSources:
server_user:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.3.21:3304/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
jdbcUrl: jdbc:mysql://192.168.3.98:3304/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"
server_order0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.3.21:3300/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
jdbcUrl: jdbc:mysql://192.168.3.98:3300/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"
server_order1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.3.21:3301/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
jdbcUrl: jdbc:mysql://192.168.3.98:3301/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"
rules:
- !SINGLE
tables:
- "*.*.*"
# 数据分片配置
- !SHARDING
# 路由表 当表位 t_user 路由到 dataSources为server_user下的t_user
@ -31,7 +34,7 @@ rules:
t_user:
actualDataNodes: server_user.t_user
t_order:
actualDataNodes: server_order${0..1}.t_order{0..1}
actualDataNodes: server_order${0..1}.t_order${0..1}
# 指定分片策略--数据库
databaseStrategy:
standard:
@ -39,15 +42,29 @@ rules:
shardingColumn: user_id
# 指定分片算法名称
shardingAlgorithmName: server_order_ds_user_id # server_order_user_id
# 指定分片策略--表
tableStrategy:
standard:
# 指定分片列字段
shardingColumn: order_no
# 指定分片算法名称
shardingAlgorithmName: alg_hash_mod # server_order_user_id
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
# # 指定分片策略--表
# tableStrategy:
# standard:
# # 指定分片列字段
# shardingColumn: order_no
# # 指定分片算法名称
# shardingAlgorithmName: alg_hash_mod # server_order_user_id
# 分片算法
autoTables:
t_order:
actualDataSources: server_order${0..1} # 数据源名称
shardingStrategy: # 切分策略
standard: # 用于单分片键的标准分片场景
shardingColumn: order_no # 分片列名称
shardingAlgorithmName: alg_hash_mod # 自动分片算法名称
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
# 分片算法
shardingAlgorithms:
server_order_ds_user_id:
type: INLINE
@ -57,8 +74,9 @@ rules:
type: HASH_MOD
props:
sharding-count: 2
keyGenerators:
snowflake:
type: SNOWFLAKE
# 打印SQL
props:
sql-show: true

View File

@ -6,13 +6,13 @@ dataSources:
server_user:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.3.21:3304/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
jdbcUrl: jdbc:mysql://192.168.3.98:3304/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"
server_order:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.3.21:3305/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
jdbcUrl: jdbc:mysql://192.168.3.98:3305/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"

View File

@ -2,17 +2,22 @@ package cn.bunny.service.service.impl;
import cn.bunny.entity.order.Order;
import cn.bunny.service.mapper.OrderMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.DigestUtils;
import java.math.BigDecimal;
import java.util.List;
@SpringBootTest
public class OrderServiceShardingTest {
@Autowired
private OrderMapper orderMapper;
@Autowired
private JdbcTemplate jdbcTemplate;
// 水平分片测试
@Test
@ -48,6 +53,7 @@ public class OrderServiceShardingTest {
// 分库测试
@Test
void testInsertOrderStrategy2() {
for (long i = 4; i < 8; i++) {
double random = Math.random();
String md5DigestAsHex = DigestUtils.md5DigestAsHex(String.valueOf(random).getBytes());
@ -61,11 +67,18 @@ public class OrderServiceShardingTest {
}
}
// 分库分别测试
// 分库分别测试数据库必须要有这个表 生成规则 order_${0..1}
@Test
void testInsertOrderStrategy3() {
for (long i = 5; i < 9; i++) {
// jdbcTemplate.execute("CREATE TABLE t_order (\n" +
// " id BIGINT,\n" +
// " order_no VARCHAR(30),\n" +
// " user_id BIGINT,\n" +
// " amount DECIMAL(10,2),\n" +
// " PRIMARY KEY(id) USING BTREE\n" +
// ") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;");
for (long i = 5; i < 9; i++) {
Order order = new Order();
order.setOrderNo("ATGUIGU" + i);
order.setUserId(2L);
@ -73,4 +86,28 @@ public class OrderServiceShardingTest {
orderMapper.insert(order);
}
}
// 会查询所有将数据库自动组装
@Test
void selectAllOrder1() {
List<Order> orders = orderMapper.selectList(null);
orders.forEach(System.out::println);
}
// 会根据配置规则吗比如UserId为奇数只会查询配置为奇数的数据库
@Test
void selectAllOrder2() {
List<Order> orders = orderMapper.selectList(Wrappers.<Order>lambdaQuery().eq(Order::getUserId, 1L));
orders.forEach(System.out::println);
}
}
// UUID 不做主键因为无序的
// 雪花算法是Twitter开源的
// 有序性雪花算法生成的ID是有序的其中包含时间戳信息因此在一定程度上可以保持生成的ID的有序性便于数据库索引等操作
// 提高索引查询效率
// 性能UUID是128位的相比之下雪花算法生成的ID长度较短64位在存储和索引上更加高效
//
// 可读性雪花算法生成的ID相对于UUID来说更容易解析和理解因为其中包含了时间戳等信息可以直观地看出ID的生成时间
//
// 分布式特性雪花算法考虑了分布式系统中的唯一性要求通过机器ID和序列号等信息保证在分布式环境下生成的ID的唯一性