提交 c1e67cb2 authored 作者: 陈世营's avatar 陈世营

分表测试

上级 f61c8030
......@@ -5,7 +5,6 @@ import com.loit.shardingsphere.seata.nacos.feign.modules.entity.OrderEntity;
import com.loit.shardingsphere.seata.nacos.feign.modules.service.IBusinessService;
import com.loit.shardingsphere.seata.nacos.feign.modules.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -30,7 +29,11 @@ public class DemoController {
@GetMapping("purchase")
public void purchase() {
for (long i = 1; i < 20; i++) {
businessService.purchase(i);
try {
businessService.purchase(i);
} catch (Exception e) {
log.info(e.getMessage(), e);
}
}
log.info("success");
}
......
......@@ -2,5 +2,11 @@ package com.loit.shardingsphere.seata.nacos.feign.modules.service;
public interface IBusinessService {
/**
* 分库分表
*/
void purchase(Long userId);
}
......@@ -3,12 +3,12 @@ package com.loit.shardingsphere.seata.nacos.feign.modules.service.impl;
import com.loit.shardingsphere.seata.nacos.feign.modules.entity.OrderEntity;
import com.loit.shardingsphere.seata.nacos.feign.modules.service.IBusinessService;
import com.loit.shardingsphere.seata.nacos.feign.modules.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("businessService")
public class BusinessServiceImpl implements IBusinessService {
......@@ -16,14 +16,20 @@ public class BusinessServiceImpl implements IBusinessService {
@Autowired
IOrderService orderService;
/**
* 分库分表 使用全局事务
*
* @param userId
*/
@Override
@ShardingTransactionType(TransactionType.BASE)
@GlobalTransactional
@ShardingTransactionType(TransactionType.LOCAL)
//@GlobalTransactional
@Transactional
public void purchase(Long userId) {
if (userId == null) {
return;
}
TransactionTypeHolder.set(TransactionType.BASE);
TransactionTypeHolder.set(TransactionType.LOCAL);
OrderEntity orderEntity = new OrderEntity();
orderEntity.setOrderId(userId);
orderEntity.setStatus("seata");
......@@ -32,4 +38,24 @@ public class BusinessServiceImpl implements IBusinessService {
//throw new RuntimeException("回滚测试");
}
/**
* 分表 使用本地事务
* @param userId
*/
// @Override
// @ShardingTransactionType(TransactionType.LOCAL)
// @Transactional
// public void purchase(Long userId) {
// if (userId == null) {
// return;
// }
// TransactionTypeHolder.set(TransactionType.LOCAL);
// OrderEntity orderEntity = new OrderEntity();
// orderEntity.setOrderId(userId);
// orderEntity.setStatus("seata");
// orderEntity.setUserId(userId.intValue());
// orderService.insertOrder(orderEntity);
// throw new RuntimeException("回滚测试");
// }
}
server:
port: 8090
spring:
application:
name: loit-shardingshere
cloud:
nacos:
discovery:
# Nacos 注册中心地址
server-addr: 39.100.254.140:8103
namespace: 3ac84119-5558-4bf8-b309-034dd0e458e0
aop: #使用CGLIB来实现AOP的时候
proxy-target-class: true
shardingsphere:
datasource:
names: ds
ds:
type: com.alibaba.druid.pool.DruidDataSource
# type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: abcd1234A!
jdbc-url: jdbc:mysql://39.98.202.173:3306/demo_ds?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
url: jdbc:mysql://39.98.202.173:3306/demo_ds?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
filters: stat,wall,log4j
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=5000
sharding:
tables:
t_order:
actual-data-nodes: ds.t_order_$->{0..1}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_$->{order_id % 2}
key-generator:
column: order_id
type: SNOWFLAKE
props:
worker.id: 123
t_order_item:
actual-data-nodes: ds.t_order_item_$->{0..1}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_item_$->{order_id % 2}
key-generator:
column: order_item_id
type: SNOWFLAKE
props:
worker.id: 123
#mybatis:
# mapper-locations: classpath:/mapper/**/*.xml #把xml文件放在com.XX.mapper.*中可能会出现找到的问题,这里把他放在resource下的mapper中
# #实体扫描,多个package用逗号或者分号分隔
# typeAliasesPackage: com.company.spring.boot.shardingsphere.seata.modules.entity #这里是实体类的位置
# configuration:
# map-underscore-to-camel-case: true
# cache-enabled: false
mybatis-plus:
# datasource: dataSource
mapper-locations: classpath:/mapper/**/*.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.loit.shardingsphere.seata.modules.entity
#typeEnumsPackage: com.baomidou.springboot.entity.enums
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID", 4:"该类型为未设置主键类型", 5:"字符串全局唯一ID";
id-type: 2
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: 1
#驼峰下划线转换
db-column-underline: true
#刷新mapper 调试神器
refresh-mapper: true
#数据库大写下划线转换
#capital-mode: true
#逻辑删除配置(下面3个配置)
logic-delete-value: 0
logic-not-delete-value: 1
# SQL 解析缓存,开启后多租户 @SqlParser 注解生效
sql-parser-cache: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
configuration-properties:
dbType: mysql #设置全局属性用于控制数据库的类型
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论