🚀 feat(新增): 配置出错

This commit is contained in:
bunny 2024-07-29 16:57:26 +08:00
parent 7986daec9c
commit 32bd6284d5
6 changed files with 148 additions and 3 deletions

View File

@ -30,7 +30,7 @@ public class Order implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long id;
private String orderNo;

View File

@ -10,7 +10,7 @@ spring:
datasource:
driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
# url: jdbc:shardingsphere:classpath:sharding/sharding-default.yaml
url: jdbc:shardingsphere:classpath:sharding/sharding-sharding.yaml
url: jdbc:shardingsphere:classpath:sharding/sharding-standard.yaml
jackson:
date-format: yyyy-MM-dd HH:mm:ss

View File

@ -0,0 +1,68 @@
# 水平分片不要创建主键自增
mode:
type: Standalone
repository:
type: JDBC
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
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
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
username: root
password: "02120212"
rules:
# 数据分片配置
- !SHARDING
# 路由表 当表位 t_user 路由到 dataSources为server_user下的t_user
tables:
t_user:
actualDataNodes: server_user.t_user
t_order:
actualDataNodes: server_order${0..1}.t_order{0..1}
# 指定分片策略--数据库
databaseStrategy:
standard:
# 指定分片列字段
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
# 分片算法
shardingAlgorithms:
server_order_ds_user_id:
type: INLINE
props:
algorithm-expression: server_order${user_id % 2} # 分片规则表达式,偶数位存储到 server_order0 奇数位存储到 server_order1
alg_hash_mod:
type: HASH_MOD
props:
sharding-count: 2
# 打印SQL
props:
sql-show: true
# 行表达式 参考https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/inline-expression/
# order${0..1}.order${0..1} 表示 order0.order0, order0.order1, order1.order0, order1.order1
# ${['one', 'two']}_ds${1..3} 表示 one_ds1, one_ds2, one_ds2, one_ds3, two_ds1, two_ds2, two_ds3 等于笛卡尔积

View File

@ -0,0 +1,76 @@
package cn.bunny.service.service.impl;
import cn.bunny.entity.order.Order;
import cn.bunny.service.mapper.OrderMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.DigestUtils;
import java.math.BigDecimal;
@SpringBootTest
public class OrderServiceShardingTest {
@Autowired
private OrderMapper orderMapper;
// 水平分片测试
@Test
void testShardingOrder() {
double random = Math.random();
String md5DigestAsHex = DigestUtils.md5DigestAsHex(String.valueOf(random).getBytes());
Order order = new Order();
order.setOrderNo(md5DigestAsHex.substring(0, 6));
order.setUserId(1L);
order.setAmount(new BigDecimal(100));
orderMapper.insert(order);
}
// 分库测试
@Test
void testInsertOrderStrategy() {
for (long i = 0; i < 4; i++) {
double random = Math.random();
String md5DigestAsHex = DigestUtils.md5DigestAsHex(String.valueOf(random).getBytes());
Order order = new Order();
order.setOrderNo(md5DigestAsHex.substring(0, 6));
order.setUserId(i);
order.setAmount(new BigDecimal(100));
orderMapper.insert(order);
}
}
// 分库测试
@Test
void testInsertOrderStrategy2() {
for (long i = 4; i < 8; i++) {
double random = Math.random();
String md5DigestAsHex = DigestUtils.md5DigestAsHex(String.valueOf(random).getBytes());
Order order = new Order();
order.setOrderNo(md5DigestAsHex.substring(0, 6));
order.setUserId(i);
order.setAmount(new BigDecimal(100));
orderMapper.insert(order);
}
}
// 分库分别测试
@Test
void testInsertOrderStrategy3() {
for (long i = 5; i < 9; i++) {
Order order = new Order();
order.setOrderNo("ATGUIGU" + i);
order.setUserId(2L);
order.setAmount(new BigDecimal(100));
orderMapper.insert(order);
}
}
}

View File

@ -13,12 +13,13 @@ import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
@SpringBootTest
public class OrderServiceTest {
public class OrderServiceVerticalTest {
@Autowired
private UserMapper userMapper;
@Autowired
private OrderMapper orderMapper;
// 垂直分片测试
@Test
void insertOrder() throws NoSuchAlgorithmException {
double num = Math.random();