mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2025-12-26 16:26:41 +08:00
JeecgBoot 3.1.0 版本发布,基于代码生成器的企业级低代码平台
This commit is contained in:
@ -10,6 +10,6 @@ WORKDIR /jeecg-cloud-gateway
|
||||
|
||||
EXPOSE 9999
|
||||
|
||||
ADD ./target/jeecg-cloud-gateway-3.0.jar ./
|
||||
ADD ./target/jeecg-cloud-gateway-3.1.0.jar ./
|
||||
|
||||
CMD sleep 10;java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-gateway-3.0.jar
|
||||
CMD sleep 10;java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-gateway-3.1.0.jar
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-module</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.0</version>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@ -65,4 +65,4 @@ hystrix:
|
||||
isolation:
|
||||
strategy: SEMAPHORE
|
||||
thread:
|
||||
timeoutInMilliseconds: 3000
|
||||
timeoutInMilliseconds: 9000
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-module</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.0</version>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jeecg-cloud-monitor</artifactId>
|
||||
|
||||
@ -10,6 +10,6 @@ WORKDIR /jeecg-cloud-nacos
|
||||
|
||||
EXPOSE 8848
|
||||
|
||||
ADD ./target/jeecg-cloud-nacos-3.0.jar ./
|
||||
ADD ./target/jeecg-cloud-nacos-3.1.0.jar ./
|
||||
|
||||
CMD sleep 5;java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-nacos-3.0.jar
|
||||
CMD sleep 5;java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-nacos-3.1.0.jar
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-module</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.0</version>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<artifactId>jeecg-cloud-nacos</artifactId>
|
||||
<name>jeecg-cloud-nacos</name>
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-module</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.0</version>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<artifactId>jeecg-cloud-sentinel</artifactId>
|
||||
<name>jeecg-cloud-sentinel</name>
|
||||
|
||||
@ -10,6 +10,6 @@ WORKDIR /jeecg-cloud-system
|
||||
|
||||
EXPOSE 7001
|
||||
|
||||
ADD ./target/jeecg-cloud-system-start-3.0.jar ./
|
||||
ADD ./target/jeecg-cloud-system-start-3.1.0.jar ./
|
||||
|
||||
CMD sleep 10;java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-system-start-3.0.jar
|
||||
CMD sleep 10;java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-system-start-3.1.0.jar
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-module</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.0</version>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jeecg-cloud-system-start</artifactId>
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>分布式事务测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-seata</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-seata</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,59 @@
|
||||
package org.jeecg.modules.test.seata.controller;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.jeecg.modules.test.seata.dto.PlaceOrderRequest;
|
||||
import org.jeecg.modules.test.seata.service.SeataOrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/order")
|
||||
@Api(tags = "seata测试")
|
||||
public class SeataOrderController {
|
||||
|
||||
@Autowired
|
||||
private SeataOrderService orderService;
|
||||
|
||||
/**
|
||||
* 自由下单
|
||||
*/
|
||||
@PostMapping("/placeOrder")
|
||||
@ApiOperation(value = "自由下单", notes = "自由下单")
|
||||
public String placeOrder(@Validated @RequestBody PlaceOrderRequest request) {
|
||||
orderService.placeOrder(request);
|
||||
return "下单成功";
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试商品库存不足-异常回滚
|
||||
*/
|
||||
@PostMapping("/test1")
|
||||
@ApiOperation(value = "测试商品库存不足", notes = "测试商品库存不足")
|
||||
public String test1() {
|
||||
//商品单价10元,库存20个,用户余额50元,模拟一次性购买22个。 期望异常回滚
|
||||
orderService.placeOrder(new PlaceOrderRequest(1L, 1L, 22));
|
||||
return "下单成功";
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试用户账户余额不足-异常回滚
|
||||
*/
|
||||
@PostMapping("/test2")
|
||||
@ApiOperation(value = "测试用户账户余额不足", notes = "测试用户账户余额不足")
|
||||
public String test2() {
|
||||
//商品单价10元,库存20个,用户余额50元,模拟一次性购买6个。 期望异常回滚
|
||||
orderService.placeOrder(new PlaceOrderRequest(1L, 1L, 6));
|
||||
return "下单成功";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package org.jeecg.modules.test.seata.dto;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
/**
|
||||
* @Description: 订单请求对象
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PlaceOrderRequest {
|
||||
|
||||
@NotNull
|
||||
private Long userId;
|
||||
|
||||
@NotNull
|
||||
private Long productId;
|
||||
|
||||
@NotNull
|
||||
private Integer amount;
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package org.jeecg.modules.test.seata.dto;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @Description: 余额请求对象
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ReduceBalanceRequest {
|
||||
|
||||
private Long userId;
|
||||
private Integer price;
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package org.jeecg.modules.test.seata.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
/**
|
||||
* @Description: 库存请求对象
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ReduceStockRequest {
|
||||
|
||||
private Long productId;
|
||||
private Integer amount;
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package org.jeecg.modules.test.seata.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Description: 账户
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@TableName("account")
|
||||
public class SeataAccount {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 余额
|
||||
*/
|
||||
private Double balance;
|
||||
|
||||
private Date lastUpdateTime;
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package org.jeecg.modules.test.seata.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import org.jeecg.modules.test.seata.enums.OrderStatus;
|
||||
|
||||
/**
|
||||
* @Description: 订单
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Builder
|
||||
@Data
|
||||
@TableName("p_order")
|
||||
public class SeataOrder {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Long userId;
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private Long productId;
|
||||
/**
|
||||
* 订单状态
|
||||
*/
|
||||
private OrderStatus status;
|
||||
/**
|
||||
* 数量
|
||||
*/
|
||||
private Integer amount;
|
||||
/**
|
||||
* 总金额
|
||||
*/
|
||||
private Double totalPrice;
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package org.jeecg.modules.test.seata.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
/**
|
||||
* @Description: 产品
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@TableName("product")
|
||||
public class SeataProduct {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
/**
|
||||
* 价格
|
||||
*/
|
||||
private Double price;
|
||||
/**
|
||||
* 库存
|
||||
*/
|
||||
private Integer stock;
|
||||
|
||||
private Date lastUpdateTime;
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package org.jeecg.modules.test.seata.enums;
|
||||
|
||||
/**
|
||||
* @Description: 订单状态
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public enum OrderStatus {
|
||||
/**
|
||||
* INIT
|
||||
*/
|
||||
INIT,
|
||||
/**
|
||||
* SUCCESS
|
||||
*/
|
||||
SUCCESS,
|
||||
/**
|
||||
* FAIL
|
||||
*/
|
||||
FAIL
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package org.jeecg.modules.test.seata.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.jeecg.modules.test.seata.entity.SeataAccount;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Mapper
|
||||
public interface SeataAccountMapper extends BaseMapper<SeataAccount> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package org.jeecg.modules.test.seata.mapper;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.jeecg.modules.test.seata.entity.SeataOrder;
|
||||
|
||||
@Mapper
|
||||
public interface SeataOrderMapper extends BaseMapper<SeataOrder> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package org.jeecg.modules.test.seata.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.jeecg.modules.test.seata.entity.SeataProduct;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Mapper
|
||||
public interface SeataProductMapper extends BaseMapper<SeataProduct> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package org.jeecg.modules.test.seata.service;
|
||||
|
||||
/**
|
||||
* @Description: 账户接口
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface SeataAccountService {
|
||||
/**
|
||||
* @param userId 用户 ID
|
||||
* @param price 扣减金额
|
||||
*/
|
||||
void reduceBalance(Long userId, Double price);
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package org.jeecg.modules.test.seata.service;
|
||||
|
||||
import org.jeecg.modules.test.seata.dto.PlaceOrderRequest;
|
||||
|
||||
/**
|
||||
* @Description: 订单接口
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface SeataOrderService {
|
||||
/**
|
||||
* 下单
|
||||
*
|
||||
* @param placeOrderRequest 订单请求参数
|
||||
*/
|
||||
void placeOrder(PlaceOrderRequest placeOrderRequest);
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package org.jeecg.modules.test.seata.service;
|
||||
|
||||
/**
|
||||
* @Description: 产品接口
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface SeataProductService {
|
||||
/**
|
||||
* 扣减库存
|
||||
*
|
||||
* @param productId 商品 ID
|
||||
* @param amount 扣减数量
|
||||
* @return 商品总价
|
||||
*/
|
||||
Double reduceStock(Long productId, Integer amount);
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package org.jeecg.modules.test.seata.service.impl;
|
||||
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.test.seata.entity.SeataAccount;
|
||||
import org.jeecg.modules.test.seata.mapper.SeataAccountMapper;
|
||||
import org.jeecg.modules.test.seata.service.SeataAccountService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SeataAccountServiceImpl implements SeataAccountService {
|
||||
@Resource
|
||||
private SeataAccountMapper accountMapper;
|
||||
|
||||
/**
|
||||
* 事务传播特性设置为 REQUIRES_NEW 开启新的事务
|
||||
*/
|
||||
@DS("account")
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void reduceBalance(Long userId, Double price) {
|
||||
log.info("=============ACCOUNT START=================");
|
||||
SeataAccount account = accountMapper.selectById(userId);
|
||||
Assert.notNull(account, "用户不存在");
|
||||
Double balance = account.getBalance();
|
||||
log.info("下单用户{}余额为 {},商品总价为{}", userId, balance, price);
|
||||
|
||||
if (balance < price) {
|
||||
log.warn("用户 {} 余额不足,当前余额:{}", userId, balance);
|
||||
throw new RuntimeException("余额不足");
|
||||
}
|
||||
log.info("开始扣减用户 {} 余额", userId);
|
||||
double currentBalance = account.getBalance() - price;
|
||||
account.setBalance(currentBalance);
|
||||
accountMapper.updateById(account);
|
||||
log.info("扣减用户 {} 余额成功,扣减后用户账户余额为{}", userId, currentBalance);
|
||||
log.info("=============ACCOUNT END=================");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
package org.jeecg.modules.test.seata.service.impl;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
|
||||
import io.seata.spring.annotation.GlobalTransactional;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.test.seata.dto.PlaceOrderRequest;
|
||||
import org.jeecg.modules.test.seata.entity.SeataOrder;
|
||||
import org.jeecg.modules.test.seata.enums.OrderStatus;
|
||||
import org.jeecg.modules.test.seata.mapper.SeataOrderMapper;
|
||||
import org.jeecg.modules.test.seata.service.SeataAccountService;
|
||||
import org.jeecg.modules.test.seata.service.SeataOrderService;
|
||||
import org.jeecg.modules.test.seata.service.SeataProductService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
/**
|
||||
* @Description: 订单服务类
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SeataOrderServiceImpl implements SeataOrderService {
|
||||
|
||||
@Resource
|
||||
private SeataOrderMapper orderMapper;
|
||||
@Autowired
|
||||
private SeataAccountService accountService;
|
||||
@Autowired
|
||||
private SeataProductService productService;
|
||||
|
||||
@DS("order")
|
||||
@Override
|
||||
@Transactional
|
||||
@GlobalTransactional
|
||||
public void placeOrder(PlaceOrderRequest request) {
|
||||
log.info("=============ORDER START=================");
|
||||
Long userId = request.getUserId();
|
||||
Long productId = request.getProductId();
|
||||
Integer amount = request.getAmount();
|
||||
log.info("收到下单请求,用户:{}, 商品:{},数量:{}", userId, productId, amount);
|
||||
|
||||
|
||||
SeataOrder order = SeataOrder.builder()
|
||||
.userId(userId)
|
||||
.productId(productId)
|
||||
.status(OrderStatus.INIT)
|
||||
.amount(amount)
|
||||
.build();
|
||||
|
||||
orderMapper.insert(order);
|
||||
log.info("订单一阶段生成,等待扣库存付款中");
|
||||
// 扣减库存并计算总价
|
||||
Double totalPrice = productService.reduceStock(productId, amount);
|
||||
// 扣减余额
|
||||
accountService.reduceBalance(userId, totalPrice);
|
||||
|
||||
order.setStatus(OrderStatus.SUCCESS);
|
||||
order.setTotalPrice(totalPrice);
|
||||
orderMapper.updateById(order);
|
||||
log.info("订单已成功下单");
|
||||
log.info("=============ORDER END=================");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
package org.jeecg.modules.test.seata.service.impl;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.jeecg.modules.test.seata.entity.SeataProduct;
|
||||
import org.jeecg.modules.test.seata.mapper.SeataProductMapper;
|
||||
import org.jeecg.modules.test.seata.service.SeataProductService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Description: 产品服务类
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SeataProductServiceImpl implements SeataProductService {
|
||||
|
||||
@Resource
|
||||
private SeataProductMapper productMapper;
|
||||
|
||||
/**
|
||||
* 事务传播特性设置为 REQUIRES_NEW 开启新的事务
|
||||
*/
|
||||
@DS("product")
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Override
|
||||
public Double reduceStock(Long productId, Integer amount) {
|
||||
log.info("=============PRODUCT START=================");
|
||||
// 检查库存
|
||||
SeataProduct product = productMapper.selectById(productId);
|
||||
Assert.notNull(product, "商品不存在");
|
||||
Integer stock = product.getStock();
|
||||
log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, amount);
|
||||
|
||||
if (stock < amount) {
|
||||
log.warn("商品编号为{} 库存不足,当前库存:{}", productId, stock);
|
||||
throw new RuntimeException("库存不足");
|
||||
}
|
||||
log.info("开始扣减商品编号为 {} 库存,单价商品价格为{}", productId, product.getPrice());
|
||||
// 扣减库存
|
||||
int currentStock = stock - amount;
|
||||
product.setStock(currentStock);
|
||||
productMapper.updateById(product);
|
||||
double totalPrice = product.getPrice() * amount;
|
||||
log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, amount, totalPrice);
|
||||
log.info("=============PRODUCT END=================");
|
||||
return totalPrice;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
seata:
|
||||
enable-auto-data-source-proxy: false
|
||||
service:
|
||||
grouplist:
|
||||
default: 127.0.0.1:8091
|
||||
vgroup-mapping:
|
||||
springboot-seata-group: default
|
||||
# seata 事务组编号 用于TC集群名
|
||||
tx-service-group: springboot-seata-group
|
||||
transport:
|
||||
heartbeat: false
|
||||
spring:
|
||||
datasource:
|
||||
dynamic:
|
||||
seata: true # 开启对 seata的支持
|
||||
seata-mode: AT #支持XA及AT模式,默认AT
|
||||
datasource:
|
||||
# 设置 账号数据源配置
|
||||
account:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3306/jeecg-account?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-account.sql
|
||||
# 设置 订单数据源配置
|
||||
order:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3306/jeecg-order?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-order.sql
|
||||
# 设置商品 数据源配置
|
||||
product:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3306/jeecg-product?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-product.sql
|
||||
@ -0,0 +1,29 @@
|
||||
DROP TABLE IF EXISTS account;
|
||||
CREATE TABLE account
|
||||
(
|
||||
id INT(11) NOT NULL AUTO_INCREMENT,
|
||||
balance DOUBLE DEFAULT NULL,
|
||||
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
DROP TABLE IF EXISTS undo_log;
|
||||
CREATE TABLE undo_log
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
branch_id BIGINT(20) NOT NULL,
|
||||
xid VARCHAR(100) NOT NULL,
|
||||
context VARCHAR(128) NOT NULL,
|
||||
rollback_info LONGBLOB NOT NULL,
|
||||
log_status INT(11) NOT NULL,
|
||||
log_created DATETIME NOT NULL,
|
||||
log_modified DATETIME NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY ux_undo_log (xid, branch_id)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
||||
INSERT INTO account (id, balance)
|
||||
VALUES (1, 50);
|
||||
@ -0,0 +1,32 @@
|
||||
DROP TABLE IF EXISTS p_order;
|
||||
CREATE TABLE p_order
|
||||
(
|
||||
id INT(11) NOT NULL AUTO_INCREMENT,
|
||||
user_id INT(11) DEFAULT NULL,
|
||||
product_id INT(11) DEFAULT NULL,
|
||||
amount INT(11) DEFAULT NULL,
|
||||
total_price DOUBLE DEFAULT NULL,
|
||||
status VARCHAR(100) DEFAULT NULL,
|
||||
add_time DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
DROP TABLE IF EXISTS undo_log;
|
||||
CREATE TABLE undo_log
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
branch_id BIGINT(20) NOT NULL,
|
||||
xid VARCHAR(100) NOT NULL,
|
||||
context VARCHAR(128) NOT NULL,
|
||||
rollback_info LONGBLOB NOT NULL,
|
||||
log_status INT(11) NOT NULL,
|
||||
log_created DATETIME NOT NULL,
|
||||
log_modified DATETIME NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY ux_undo_log (xid, branch_id)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
||||
@ -0,0 +1,31 @@
|
||||
DROP TABLE IF EXISTS product;
|
||||
CREATE TABLE product
|
||||
(
|
||||
id INT(11) NOT NULL AUTO_INCREMENT,
|
||||
price DOUBLE DEFAULT NULL,
|
||||
stock INT(11) DEFAULT NULL,
|
||||
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
DROP TABLE IF EXISTS undo_log;
|
||||
CREATE TABLE undo_log
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
branch_id BIGINT(20) NOT NULL,
|
||||
xid VARCHAR(100) NOT NULL,
|
||||
context VARCHAR(128) NOT NULL,
|
||||
rollback_info LONGBLOB NOT NULL,
|
||||
log_status INT(11) NOT NULL,
|
||||
log_created DATETIME NOT NULL,
|
||||
log_modified DATETIME NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY ux_undo_log (xid, branch_id)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
INSERT INTO product (id, price, stock)
|
||||
VALUES (1, 10, 20);
|
||||
@ -0,0 +1,79 @@
|
||||
/*
|
||||
Navicat Premium Data Transfer
|
||||
|
||||
Source Server : localhost
|
||||
Source Server Type : MariaDB
|
||||
Source Server Version : 100316
|
||||
Source Host : localhost:3300
|
||||
Source Schema : seata
|
||||
|
||||
Target Server Type : MariaDB
|
||||
Target Server Version : 100316
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 05/01/2022 20:25:07
|
||||
*/
|
||||
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for branch_table
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `branch_table`;
|
||||
CREATE TABLE `branch_table` (
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`transaction_id` bigint(20) NULL DEFAULT NULL,
|
||||
`resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`status` tinyint(4) NULL DEFAULT NULL,
|
||||
`client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`gmt_create` datetime(6) NULL DEFAULT NULL,
|
||||
`gmt_modified` datetime(6) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`branch_id`) USING BTREE,
|
||||
INDEX `idx_xid`(`xid`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for global_table
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `global_table`;
|
||||
CREATE TABLE `global_table` (
|
||||
`xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`transaction_id` bigint(20) NULL DEFAULT NULL,
|
||||
`status` tinyint(4) NOT NULL,
|
||||
`application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`transaction_service_group` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`timeout` int(11) NULL DEFAULT NULL,
|
||||
`begin_time` bigint(20) NULL DEFAULT NULL,
|
||||
`application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`gmt_create` datetime(0) NULL DEFAULT NULL,
|
||||
`gmt_modified` datetime(0) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`xid`) USING BTREE,
|
||||
INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE,
|
||||
INDEX `idx_transaction_id`(`transaction_id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for lock_table
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `lock_table`;
|
||||
CREATE TABLE `lock_table` (
|
||||
`row_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`xid` varchar(96) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`transaction_id` bigint(20) NULL DEFAULT NULL,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`table_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`pk` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`gmt_create` datetime(0) NULL DEFAULT NULL,
|
||||
`gmt_modified` datetime(0) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`row_key`) USING BTREE,
|
||||
INDEX `idx_branch_id`(`branch_id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@ -0,0 +1,12 @@
|
||||
CREATE TABLE `undo_log` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) NOT NULL,
|
||||
`context` varchar(128) NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime NOT NULL,
|
||||
`log_modified` datetime NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=137 DEFAULT CHARSET=utf8;
|
||||
@ -0,0 +1,46 @@
|
||||
CREATE TABLE `sys_log0` (
|
||||
`id` varchar(32) NOT NULL,
|
||||
`log_type` int(2) DEFAULT NULL COMMENT '日志类型(1登录日志,2操作日志)',
|
||||
`log_content` varchar(1000) DEFAULT NULL COMMENT '日志内容',
|
||||
`operate_type` int(2) DEFAULT NULL COMMENT '操作类型',
|
||||
`userid` varchar(32) DEFAULT NULL COMMENT '操作用户账号',
|
||||
`username` varchar(100) DEFAULT NULL COMMENT '操作用户名称',
|
||||
`ip` varchar(100) DEFAULT NULL COMMENT 'IP',
|
||||
`method` varchar(500) DEFAULT NULL COMMENT '请求java方法',
|
||||
`request_url` varchar(255) DEFAULT NULL COMMENT '请求路径',
|
||||
`request_param` longtext DEFAULT NULL COMMENT '请求参数',
|
||||
`request_type` varchar(10) DEFAULT NULL COMMENT '请求类型',
|
||||
`cost_time` bigint(20) DEFAULT NULL COMMENT '耗时',
|
||||
`create_by` varchar(32) DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(32) DEFAULT NULL COMMENT '更新人',
|
||||
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
KEY `index_table_userid` (`userid`) USING BTREE,
|
||||
KEY `index_logt_ype` (`log_type`) USING BTREE,
|
||||
KEY `index_operate_type` (`operate_type`) USING BTREE,
|
||||
KEY `index_createtime` (`create_time`) USING BTREE
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='系统日志表';
|
||||
CREATE TABLE `sys_log1` (
|
||||
`id` varchar(32) NOT NULL,
|
||||
`log_type` int(2) DEFAULT NULL COMMENT '日志类型(1登录日志,2操作日志)',
|
||||
`log_content` varchar(1000) DEFAULT NULL COMMENT '日志内容',
|
||||
`operate_type` int(2) DEFAULT NULL COMMENT '操作类型',
|
||||
`userid` varchar(32) DEFAULT NULL COMMENT '操作用户账号',
|
||||
`username` varchar(100) DEFAULT NULL COMMENT '操作用户名称',
|
||||
`ip` varchar(100) DEFAULT NULL COMMENT 'IP',
|
||||
`method` varchar(500) DEFAULT NULL COMMENT '请求java方法',
|
||||
`request_url` varchar(255) DEFAULT NULL COMMENT '请求路径',
|
||||
`request_param` longtext DEFAULT NULL COMMENT '请求参数',
|
||||
`request_type` varchar(10) DEFAULT NULL COMMENT '请求类型',
|
||||
`cost_time` bigint(20) DEFAULT NULL COMMENT '耗时',
|
||||
`create_by` varchar(32) DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(32) DEFAULT NULL COMMENT '更新人',
|
||||
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
KEY `index_table_userid` (`userid`) USING BTREE,
|
||||
KEY `index_logt_ype` (`log_type`) USING BTREE,
|
||||
KEY `index_operate_type` (`operate_type`) USING BTREE,
|
||||
KEY `index_createtime` (`create_time`) USING BTREE
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='系统日志表';
|
||||
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>jeecg-cloud-test-shardingsphere</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-shardingsphere</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,86 @@
|
||||
package org.jeecg.modules.test.sharding.algorithm;
|
||||
|
||||
|
||||
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
|
||||
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
|
||||
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 用于处理使用单一键
|
||||
* 根据分片字段的值和sharding-count进行取模运算
|
||||
* SQL 语句中有>,>=, <=,<,=,IN 和 BETWEEN AND 操作符,都可以应用此分片策略。
|
||||
*
|
||||
* @author zyf
|
||||
*/
|
||||
public class StandardModTableShardAlgorithm implements StandardShardingAlgorithm<Integer> {
|
||||
private Properties props = new Properties();
|
||||
|
||||
|
||||
/**
|
||||
* 用于处理=和IN的分片
|
||||
*
|
||||
* @param collection 目标分片的集合(表名)
|
||||
* @param preciseShardingValue 逻辑表相关信息
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) {
|
||||
|
||||
for (String name : collection) {
|
||||
Integer value = preciseShardingValue.getValue();
|
||||
//根据值进行取模,得到一个目标值
|
||||
if (name.indexOf(value % 2+"") > -1) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于处理BETWEEN AND分片,如果不配置RangeShardingAlgorithm,SQL中的BETWEEN AND将按照全库路由处理
|
||||
*
|
||||
* @param collection
|
||||
* @param rangeShardingValue
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Integer> rangeShardingValue) {
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化对象的时候调用的方法
|
||||
*/
|
||||
@Override
|
||||
public void init() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 对应分片算法(sharding-algorithms)的类型
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getType() {
|
||||
return "STANDARD_MOD";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties getProps() {
|
||||
return this.props;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分片相关属性
|
||||
*
|
||||
* @param properties
|
||||
*/
|
||||
@Override
|
||||
public void setProps(Properties properties) {
|
||||
this.props = properties;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
package org.jeecg.modules.test.sharding.controller;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.aspect.annotation.AutoLog;
|
||||
import org.jeecg.common.system.base.controller.JeecgController;
|
||||
import org.jeecg.modules.test.sharding.entity.ShardingSysLog;
|
||||
import org.jeecg.modules.test.sharding.service.IShardingSysLogService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Description: 分库分表测试
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Api(tags = "分库分表测试")
|
||||
@RestController
|
||||
@RequestMapping("/sharding/")
|
||||
public class JeecgShardingDemoController extends JeecgController<ShardingSysLog, IShardingSysLogService> {
|
||||
@Autowired
|
||||
private IShardingSysLogService shardingSysLogService;
|
||||
|
||||
/**
|
||||
* 添加
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/add")
|
||||
@AutoLog(value = "分库分表添加")
|
||||
@ApiOperation(value = "分库分表添加", notes = "分库分表添加")
|
||||
public Result<?> add() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ShardingSysLog shardingSysLog = new ShardingSysLog();
|
||||
shardingSysLog.setLogContent("jeecg");
|
||||
shardingSysLog.setLogType(i);
|
||||
shardingSysLog.setOperateType(i);
|
||||
shardingSysLogService.save(shardingSysLog);
|
||||
}
|
||||
return Result.OK();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,109 @@
|
||||
package org.jeecg.modules.test.sharding.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.jeecg.common.aspect.annotation.Dict;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 系统日志表
|
||||
* </p>
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_log")
|
||||
public class ShardingSysLog implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 耗时
|
||||
*/
|
||||
private Long costTime;
|
||||
|
||||
/**
|
||||
* IP
|
||||
*/
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
private String requestParam;
|
||||
|
||||
/**
|
||||
* 请求类型
|
||||
*/
|
||||
private String requestType;
|
||||
|
||||
/**
|
||||
* 请求路径
|
||||
*/
|
||||
private String requestUrl;
|
||||
/**
|
||||
* 请求方法
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
* 操作人用户名称
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 操作人用户账户
|
||||
*/
|
||||
private String userid;
|
||||
/**
|
||||
* 操作详细日志
|
||||
*/
|
||||
private String logContent;
|
||||
|
||||
/**
|
||||
* 日志类型(1登录日志,2操作日志)
|
||||
*/
|
||||
@Dict(dicCode = "log_type")
|
||||
private Integer logType;
|
||||
|
||||
/**
|
||||
* 操作类型(1查询,2添加,3修改,4删除,5导入,6导出)
|
||||
*/
|
||||
@Dict(dicCode = "operate_type")
|
||||
private Integer operateType;
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package org.jeecg.modules.test.sharding.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.jeecg.modules.test.sharding.entity.ShardingSysLog;
|
||||
|
||||
|
||||
/**
|
||||
* @Description: 系统日志表 Mapper 接口
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface ShardingSysLogMapper extends BaseMapper<ShardingSysLog> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.jeecg.modules.demo.sharding.mapper.ShardingSysLogMapper">
|
||||
|
||||
</mapper>
|
||||
@ -0,0 +1,14 @@
|
||||
package org.jeecg.modules.test.sharding.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.jeecg.modules.test.sharding.entity.ShardingSysLog;
|
||||
|
||||
/**
|
||||
* @Description: 系统日志表 服务类
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface IShardingSysLogService extends IService<ShardingSysLog> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package org.jeecg.modules.test.sharding.service.impl;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.jeecg.modules.test.sharding.entity.ShardingSysLog;
|
||||
import org.jeecg.modules.test.sharding.mapper.ShardingSysLogMapper;
|
||||
import org.jeecg.modules.test.sharding.service.IShardingSysLogService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 系统日志表 服务实现类
|
||||
* </p>
|
||||
*/
|
||||
@Service
|
||||
@DS("sharding")
|
||||
public class ShardingSysLogServiceImpl extends ServiceImpl<ShardingSysLogMapper, ShardingSysLog> implements IShardingSysLogService {
|
||||
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
spring:
|
||||
shardingsphere:
|
||||
props:
|
||||
sql-show: true
|
||||
datasource:
|
||||
ds0:
|
||||
driverClassName: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/jeecg-boot?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
username: root
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
password: root
|
||||
names: ds0
|
||||
# 规则配置
|
||||
rules:
|
||||
sharding:
|
||||
# 配置绑定表,每一行为一组
|
||||
binding-tables: sys_log
|
||||
# 分布式序列算法配置
|
||||
key-generators:
|
||||
snowflake:
|
||||
type: SNOWFLAKE
|
||||
props:
|
||||
worker-id: 123
|
||||
# 分片算法配置
|
||||
sharding-algorithms:
|
||||
table-classbased:
|
||||
props:
|
||||
strategy: standard
|
||||
algorithmClassName: org.jeecg.modules.test.sharding.algorithm.StandardModTableShardAlgorithm
|
||||
type: CLASS_BASED
|
||||
tables:
|
||||
# 逻辑表名称
|
||||
sys_log:
|
||||
#配置具体表的数据节点
|
||||
actual-data-nodes: ds0.sys_log$->{0..1}
|
||||
# 分表策略
|
||||
table-strategy:
|
||||
standard:
|
||||
# 分片算法名称
|
||||
sharding-algorithm-name: table-classbased
|
||||
# 分片列名称
|
||||
sharding-column: log_type
|
||||
26
jeecg-boot/jeecg-cloud-module/jeecg-cloud-test/pom.xml
Normal file
26
jeecg-boot/jeecg-cloud-module/jeecg-cloud-test/pom.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-module</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>pom</packaging>
|
||||
<artifactId>jeecg-cloud-test</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-base-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<modules>
|
||||
<module>jeecg-cloud-test-seata</module>
|
||||
<module>jeecg-cloud-test-shardingsphere</module>
|
||||
</modules>
|
||||
</project>
|
||||
@ -10,7 +10,7 @@ WORKDIR /jeecg-cloud-xxljob
|
||||
|
||||
EXPOSE 9080
|
||||
|
||||
ADD ./target/jeecg-cloud-xxljob-3.0.jar ./
|
||||
ADD ./target/jeecg-cloud-xxljob-3.1.0.jar ./
|
||||
|
||||
CMD java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-xxljob-3.0.jar
|
||||
CMD java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-xxljob-3.1.0.jar
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-module</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.0</version>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-boot-parent</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.0</version>
|
||||
<version>3.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
<module>jeecg-cloud-monitor</module>
|
||||
<module>jeecg-cloud-system-start</module>
|
||||
<module>jeecg-cloud-xxljob</module>
|
||||
<module>jeecg-cloud-test</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user