mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-01-03 03:45:28 +08:00
3.2.0-beta,重构很大:升级springboot2.6.6、spring-cloud-alibaba 2021.1、mybatisplus3.5.1、代码规范部分重构
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>公共测试模块</description>
|
||||
|
||||
@ -5,11 +5,6 @@ package org.jeecg.modules.test.constant;
|
||||
*/
|
||||
public interface CloudConstant {
|
||||
|
||||
/**
|
||||
* 微服务名【对应模块jeecg-boot-module-demo】
|
||||
*/
|
||||
public final static String SERVER_NAME_JEECGDEMO = "jeecg-demo";
|
||||
|
||||
/**
|
||||
* MQ测试队列名字
|
||||
*/
|
||||
|
||||
@ -2,21 +2,24 @@ package org.jeecg.modules.test.feign.client;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
|
||||
import org.jeecg.common.constant.ServiceNameConstants;
|
||||
import org.jeecg.config.FeignConfig;
|
||||
import org.jeecg.modules.test.constant.CloudConstant;
|
||||
import org.jeecg.modules.test.feign.factory.JeecgTestClientFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.Mapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* 常规feign接口定义
|
||||
*/
|
||||
@FeignClient(value = CloudConstant.SERVER_NAME_JEECGDEMO, configuration = FeignConfig.class,fallbackFactory = JeecgTestClientFactory.class)
|
||||
@FeignClient(value = ServiceNameConstants.SERVICE_DEMO, configuration = FeignConfig.class,fallbackFactory = JeecgTestClientFactory.class)
|
||||
@Component
|
||||
public interface JeecgTestClient {
|
||||
|
||||
@PostMapping(value = "/test/getMessage")
|
||||
Result<Object> getMessage(@RequestParam(value = "name",required = false) String name);
|
||||
@GetMapping(value = "/test/getMessage")
|
||||
String getMessage(@RequestParam(value = "name",required = false) String name);
|
||||
}
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
package org.jeecg.modules.test.feign.client;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* 动态feign接口定义
|
||||
*/
|
||||
public interface JeecgTestClientDyn {
|
||||
|
||||
@PostMapping(value = "/test/getMessage")
|
||||
Result<String> getMessage(@RequestParam(value = "name",required = false) String name);
|
||||
}
|
||||
@ -1,63 +1,75 @@
|
||||
package org.jeecg.modules.test.feign.controller;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.test.feign.client.JeecgTestClient;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/sys/test")
|
||||
@Api(tags = "【微服务】单元测试")
|
||||
public class JeecgTestFeignController {
|
||||
|
||||
// @Autowired
|
||||
//private JeecgFeignService jeecgFeignService;
|
||||
|
||||
@Autowired
|
||||
private JeecgTestClient jeecgTestClient;
|
||||
|
||||
@Autowired
|
||||
private RabbitMqClient rabbitMqClient;
|
||||
/**
|
||||
* 熔断: fallbackFactory优先于 @SentinelResource
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getMessage")
|
||||
@ApiOperation(value = "测试feign调用demo服务1", notes = "测试feign @SentinelResource熔断写法 | 测试熔断关闭jeecg-demo服务")
|
||||
@SentinelResource(value = "test_more_getMessage", fallback = "getDefaultUser")
|
||||
public Result<String> getMessage(@RequestParam(value = "name", required = false) String name) {
|
||||
log.info("---------Feign fallbackFactory优先级高于@SentinelResource-----------------");
|
||||
String resultMsg = jeecgTestClient.getMessage(" I am jeecg-system 服务节点,呼叫 jeecg-demo!");
|
||||
return Result.OK(null, resultMsg);
|
||||
}
|
||||
|
||||
@PostMapping("getMessage")
|
||||
@ApiOperation(value = "测试feign", notes = "测试feign")
|
||||
@SentinelResource(value = "fallback",fallback = "getDefaultUser")
|
||||
public Result<Object> getMessage(@RequestParam(value = "name",required = false) String name) {
|
||||
return jeecgTestClient.getMessage("fegin——jeecg-boot2");
|
||||
/**
|
||||
* 测试方法:关闭demo服务,访问请求 http://127.0.0.1:9999/sys/test/getMessage
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getMessage2")
|
||||
@ApiOperation(value = "测试feign调用demo服务2", notes = "测试feign fallbackFactory熔断写法 | 测试熔断关闭jeecg-demo服务")
|
||||
public Result<String> getMessage2(@RequestParam(value = "name", required = false) String name) {
|
||||
log.info("---------测试 Feign fallbackFactory-----------------");
|
||||
String resultMsg = jeecgTestClient.getMessage(" I am jeecg-system 服务节点,呼叫 jeecg-demo!");
|
||||
return Result.OK(null, resultMsg);
|
||||
}
|
||||
|
||||
|
||||
// @GetMapping("getMessage2")
|
||||
// @ApiOperation(value = "测试动态feign", notes = "测试动态feign")
|
||||
// public Result<String> getMessage2() {
|
||||
// JeecgTestClientDyn myClientDyn = jeecgFeignService.newInstance(JeecgTestClientDyn.class, CloudConstant.SERVER_NAME_JEECGDEMO);
|
||||
// return myClientDyn.getMessage("动态fegin——jeecg-boot2");
|
||||
// }
|
||||
|
||||
@PostMapping("test")
|
||||
@GetMapping("/fallback")
|
||||
@ApiOperation(value = "测试熔断", notes = "测试熔断")
|
||||
@SentinelResource(value = "fallback",fallback = "getDefaultUser")
|
||||
public Result<Object> test(@RequestParam(value = "name",required = false) String name) {
|
||||
if(StringUtils.isEmpty(name)){
|
||||
@SentinelResource(value = "test_more_fallback", fallback = "getDefaultUser")
|
||||
public Result<Object> test(@RequestParam(value = "name", required = false) String name) {
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
throw new IllegalArgumentException("name param is empty");
|
||||
}
|
||||
|
||||
return Result.OK();
|
||||
}
|
||||
|
||||
/**
|
||||
* 熔断,默认回调函数
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public Result<Object> getDefaultUser(String name) {
|
||||
System.out.println("熔断,默认回调函数");
|
||||
return Result.OK("访问超时");
|
||||
log.info("熔断,默认回调函数");
|
||||
return Result.error(null, "访问超时, 自定义 @SentinelResource Fallback");
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ package org.jeecg.modules.test.feign.factory;
|
||||
|
||||
|
||||
|
||||
import feign.hystrix.FallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.jeecg.modules.test.feign.client.JeecgTestClient;
|
||||
import org.jeecg.modules.test.feign.fallback.JeecgTestFallback;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ -7,8 +7,11 @@ import org.jeecg.modules.test.feign.client.JeecgTestClient;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
* 接口fallback实现
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2022/4/11 19:41
|
||||
*/
|
||||
public class JeecgTestFallback implements JeecgTestClient {
|
||||
|
||||
@Setter
|
||||
@ -16,7 +19,7 @@ public class JeecgTestFallback implements JeecgTestClient {
|
||||
|
||||
|
||||
@Override
|
||||
public Result<Object> getMessage(String name) {
|
||||
return Result.OK("访问超时");
|
||||
public String getMessage(String name) {
|
||||
return "访问超时, 自定义FallbackFactory";
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@ import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.constant.CloudConstant;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
@ -19,29 +20,33 @@ import java.util.Map;
|
||||
public class DemoLockTest {
|
||||
@Autowired
|
||||
RedissonLockClient redissonLock;
|
||||
@Autowired
|
||||
RabbitMqClient rabbitMqClient;
|
||||
// @Autowired
|
||||
// RabbitMqClient rabbitMqClient;
|
||||
|
||||
/**
|
||||
* 测试方法:
|
||||
* @Scheduled(cron = "0/5 * * * * ?") 表示每5秒执行一次
|
||||
* @JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)分布式锁,10秒钟才释放
|
||||
* 结果:每10秒钟输出一次 “执行 分布式锁 业务逻辑1” 就说明锁成功了
|
||||
*
|
||||
* 测试分布式锁【注解方式】
|
||||
*/
|
||||
//@Scheduled(cron = "0/5 * * * * ?")
|
||||
@Scheduled(cron = "0/5 * * * * ?")
|
||||
@JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)
|
||||
public void execute() throws InterruptedException {
|
||||
log.info("执行execute任务开始,休眠三秒");
|
||||
Thread.sleep(3000);
|
||||
System.out.println("=======================业务逻辑1=============================");
|
||||
Map map = new BaseMap();
|
||||
map.put("orderId", "BJ0001");
|
||||
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
|
||||
//延迟10秒发送
|
||||
map.put("orderId", "NJ0002");
|
||||
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map, 10000);
|
||||
log.info("execute任务结束,休眠三秒");
|
||||
log.info("执行execute任务开始,休眠十秒开始,当前系统时间戳(秒):"+ System.currentTimeMillis()/1000);
|
||||
Thread.sleep(10000);
|
||||
log.info("========执行 分布式锁 业务逻辑1=============");
|
||||
// Map map = new BaseMap();
|
||||
// map.put("orderId", "BJ0001");
|
||||
// rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
|
||||
// //延迟10秒发送
|
||||
// map.put("orderId", "NJ0002");
|
||||
// rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map, 10000);
|
||||
|
||||
log.info("execute任务结束,休眠十秒完成,当前系统时间戳(秒):"+ System.currentTimeMillis()/1000);
|
||||
}
|
||||
|
||||
public DemoLockTest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试分布式锁【编码方式】
|
||||
@ -51,7 +56,7 @@ public class DemoLockTest {
|
||||
if (redissonLock.tryLock(CloudConstant.REDISSON_DEMO_LOCK_KEY2, -1, 6000)) {
|
||||
log.info("执行任务execute2开始,休眠十秒");
|
||||
Thread.sleep(10000);
|
||||
System.out.println("=======================业务逻辑2=============================");
|
||||
log.info("=============业务逻辑2===================");
|
||||
log.info("定时execute2结束,休眠十秒");
|
||||
|
||||
redissonLock.unlock(CloudConstant.REDISSON_DEMO_LOCK_KEY2);
|
||||
|
||||
@ -32,30 +32,28 @@ public class DemoJobHandler {
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
|
||||
@XxlJob(value = "demoJob")
|
||||
public ReturnT<String> demoJobHandler(String params) {
|
||||
log.info("我是定时任务,我执行了...............................");
|
||||
log.info("我是 jeecg-system 服务里的定时任务 demoJob,我执行了...............................");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 2、分片广播任务
|
||||
*/
|
||||
|
||||
@XxlJob("shardingJobHandler")
|
||||
public ReturnT<String> shardingJobHandler(String param) throws Exception {
|
||||
|
||||
// 分片参数
|
||||
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
|
||||
XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
log.info("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
|
||||
// 业务逻辑
|
||||
for (int i = 0; i < shardingVO.getTotal(); i++) {
|
||||
if (i == shardingVO.getIndex()) {
|
||||
XxlJobLogger.log("第 {} 片, 命中分片开始处理", i);
|
||||
log.info("第 {} 片, 命中分片开始处理", i);
|
||||
} else {
|
||||
XxlJobLogger.log("第 {} 片, 忽略", i);
|
||||
log.info("第 {} 片, 忽略", i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,8 +63,9 @@ public class DemoJobHandler {
|
||||
|
||||
/**
|
||||
* 3、命令行任务
|
||||
*
|
||||
* 输入参数:ipconfig /all
|
||||
*/
|
||||
|
||||
@XxlJob("commandJobHandler")
|
||||
public ReturnT<String> commandJobHandler(String param) throws Exception {
|
||||
String command = param;
|
||||
@ -82,14 +81,14 @@ public class DemoJobHandler {
|
||||
// command log
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
XxlJobLogger.log(line);
|
||||
log.info(line);
|
||||
}
|
||||
|
||||
// command exit
|
||||
process.waitFor();
|
||||
exitValue = process.exitValue();
|
||||
} catch (Exception e) {
|
||||
XxlJobLogger.log(e);
|
||||
log.info(e.getMessage(),e);
|
||||
} finally {
|
||||
if (bufferedReader != null) {
|
||||
bufferedReader.close();
|
||||
@ -106,18 +105,18 @@ public class DemoJobHandler {
|
||||
|
||||
/**
|
||||
* 4、跨平台Http任务
|
||||
* 参数示例:
|
||||
* "url: http://www.baidu.com\n" +
|
||||
* "method: get\n" +
|
||||
* "data: content\n";
|
||||
*
|
||||
* 输入参数:
|
||||
* url: https://www.baidu.com
|
||||
* method: get
|
||||
* data: content
|
||||
*/
|
||||
|
||||
@XxlJob("httpJobHandler")
|
||||
public ReturnT<String> httpJobHandler(String param) throws Exception {
|
||||
|
||||
// param parse
|
||||
if (param == null || param.trim().length() == 0) {
|
||||
XxlJobLogger.log("param[" + param + "] invalid.");
|
||||
log.info("param[" + param + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
String[] httpParams = param.split("\n");
|
||||
@ -138,11 +137,11 @@ public class DemoJobHandler {
|
||||
|
||||
// param valid
|
||||
if (url == null || url.trim().length() == 0) {
|
||||
XxlJobLogger.log("url[" + url + "] invalid.");
|
||||
log.info("url[" + url + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
if (method == null || !Arrays.asList("GET", "POST").contains(method)) {
|
||||
XxlJobLogger.log("method[" + method + "] invalid.");
|
||||
log.info("method[" + method + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
|
||||
@ -191,10 +190,10 @@ public class DemoJobHandler {
|
||||
}
|
||||
String responseMsg = result.toString();
|
||||
|
||||
XxlJobLogger.log(responseMsg);
|
||||
log.info(responseMsg);
|
||||
return ReturnT.SUCCESS;
|
||||
} catch (Exception e) {
|
||||
XxlJobLogger.log(e);
|
||||
log.info(e.getMessage(),e);
|
||||
return ReturnT.FAIL;
|
||||
} finally {
|
||||
try {
|
||||
@ -205,7 +204,7 @@ public class DemoJobHandler {
|
||||
connection.disconnect();
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
XxlJobLogger.log(e2);
|
||||
log.info(e2.getMessage(),e2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -215,10 +214,9 @@ public class DemoJobHandler {
|
||||
/**
|
||||
* 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
|
||||
*/
|
||||
|
||||
@XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
|
||||
public ReturnT<String> demoJobHandler2(String param) throws Exception {
|
||||
XxlJobLogger.log("XXL-JOB, Hello World.");
|
||||
log.info("XXL-JOB, Hello World.");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@ -22,9 +22,9 @@ public class XxclJobTest {
|
||||
* @return
|
||||
*/
|
||||
|
||||
@XxlJob(value = "testJob")
|
||||
@XxlJob(value = "xxclJobTest")
|
||||
public ReturnT<String> demoJobHandler(String params) {
|
||||
log.info("我是demo服务里的定时任务testJob,我执行了...............................");
|
||||
log.info("我是 jeecg-system 服务里的定时任务 xxclJobTest , 我执行了...............................");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>消息队列测试模块</description>
|
||||
|
||||
@ -17,16 +17,25 @@ import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* RabbitMqClient发送消息
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/sys/test")
|
||||
@Api(tags = "【微服务】单元测试")
|
||||
@Api(tags = "【微服务】MQ单元测试")
|
||||
public class JeecgMqTestController {
|
||||
|
||||
@Autowired
|
||||
private RabbitMqClient rabbitMqClient;
|
||||
|
||||
|
||||
/**
|
||||
* 测试方法:快速点击发送MQ消息
|
||||
* 观察三个接受者如何分配处理消息:HelloReceiver1、HelloReceiver2、HelloReceiver3,会均衡分配
|
||||
*
|
||||
* @param req
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/rabbitmq")
|
||||
@ApiOperation(value = "测试rabbitmq", notes = "测试rabbitmq")
|
||||
public Result<?> rabbitMqClientTest(HttpServletRequest req) {
|
||||
|
||||
@ -17,6 +17,8 @@ import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 定义接收者(可以定义N个接受者,消息会均匀的发送到N个接收者中)
|
||||
*
|
||||
* RabbitMq接受者1
|
||||
* (@RabbitListener声明类上,一个类只能监听一个队列)
|
||||
*/
|
||||
@ -35,7 +37,7 @@ public class HelloReceiver1 extends BaseRabbiMqHandler<BaseMap> {
|
||||
public void handler(BaseMap map, Channel channel) {
|
||||
//业务处理
|
||||
String orderId = map.get("orderId").toString();
|
||||
System.out.println("MQ Receiver1,orderId : " + orderId);
|
||||
log.info("【我是处理人1】 MQ Receiver1,orderId : " + orderId);
|
||||
// jeecgTestClient.getMessage("JEECG");
|
||||
try{
|
||||
// HttpHeaders requestHeaders = new HttpHeaders();
|
||||
|
||||
@ -13,6 +13,8 @@ import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
|
||||
/**
|
||||
* 定义接收者(可以定义N个接受者,消息会均匀的发送到N个接收者中)
|
||||
*
|
||||
* RabbitMq接受者2
|
||||
* (@RabbitListener声明类上,一个类只能监听一个队列)
|
||||
*/
|
||||
@ -28,7 +30,7 @@ public class HelloReceiver2 extends BaseRabbiMqHandler<BaseMap> {
|
||||
public void handler(BaseMap map, Channel channel) {
|
||||
//业务处理
|
||||
String orderId = map.get("orderId").toString();
|
||||
log.info("MQ Receiver2,orderId : " + orderId);
|
||||
log.info("【我是处理人2】 MQ Receiver2,orderId : " + orderId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -12,7 +12,9 @@ import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
|
||||
/**
|
||||
* RabbitMq接受者3
|
||||
* 定义接收者(可以定义N个接受者,消息会均匀的发送到N个接收者中)
|
||||
*
|
||||
* RabbitMq接受者3【我是处理人3】
|
||||
* (@RabbitListener声明类方法上,一个类可以多监听多个队列)
|
||||
*/
|
||||
@Slf4j
|
||||
@ -26,7 +28,7 @@ public class HelloReceiver3 extends BaseRabbiMqHandler<BaseMap> {
|
||||
public void handler(BaseMap map, Channel channel) {
|
||||
//业务处理
|
||||
String orderId = map.get("orderId").toString();
|
||||
log.info("MQ Receiver3,orderId : " + orderId);
|
||||
log.info("【我是处理人3】MQ Receiver3,orderId : " + orderId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
<?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-seata</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>分布式事务测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-seata-account</artifactId>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,17 @@
|
||||
package org.jeecg;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* 分布式事务-账户服务
|
||||
* @author zyf
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SeataAccountApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataAccountApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package org.jeecg.modules.test.seata.account.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.test.seata.account.service.SeataAccountService;
|
||||
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 java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/test/seata/account")
|
||||
public class SeataAccountController {
|
||||
|
||||
@Autowired
|
||||
private SeataAccountService accountService;
|
||||
|
||||
@PostMapping("/reduceBalance")
|
||||
public void reduceBalance(Long userId, BigDecimal amount) {
|
||||
accountService.reduceBalance(userId, amount);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package org.jeecg.modules.test.seata.entity;
|
||||
package org.jeecg.modules.test.seata.account.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
@ -24,7 +25,7 @@ public class SeataAccount {
|
||||
/**
|
||||
* 余额
|
||||
*/
|
||||
private Double balance;
|
||||
private BigDecimal balance;
|
||||
|
||||
private Date lastUpdateTime;
|
||||
}
|
||||
@ -1,8 +1,9 @@
|
||||
package org.jeecg.modules.test.seata.mapper;
|
||||
package org.jeecg.modules.test.seata.account.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.jeecg.modules.test.seata.entity.SeataAccount;
|
||||
import org.jeecg.modules.test.seata.account.entity.SeataAccount;
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
@ -1,4 +1,6 @@
|
||||
package org.jeecg.modules.test.seata.service;
|
||||
package org.jeecg.modules.test.seata.account.service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 账户接口
|
||||
@ -9,7 +11,7 @@ package org.jeecg.modules.test.seata.service;
|
||||
public interface SeataAccountService {
|
||||
/**
|
||||
* @param userId 用户 ID
|
||||
* @param price 扣减金额
|
||||
* @param amount 扣减金额
|
||||
*/
|
||||
void reduceBalance(Long userId, Double price);
|
||||
void reduceBalance(Long userId, BigDecimal amount);
|
||||
}
|
||||
@ -1,17 +1,19 @@
|
||||
package org.jeecg.modules.test.seata.service.impl;
|
||||
package org.jeecg.modules.test.seata.account.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.jeecg.modules.test.seata.account.entity.SeataAccount;
|
||||
import org.jeecg.modules.test.seata.account.mapper.SeataAccountMapper;
|
||||
import org.jeecg.modules.test.seata.account.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;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
@ -31,19 +33,19 @@ public class SeataAccountServiceImpl implements SeataAccountService {
|
||||
@DS("account")
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void reduceBalance(Long userId, Double price) {
|
||||
public void reduceBalance(Long userId, BigDecimal amount) {
|
||||
log.info("=============ACCOUNT START=================");
|
||||
SeataAccount account = accountMapper.selectById(userId);
|
||||
Assert.notNull(account, "用户不存在");
|
||||
Double balance = account.getBalance();
|
||||
log.info("下单用户{}余额为 {},商品总价为{}", userId, balance, price);
|
||||
BigDecimal balance = account.getBalance();
|
||||
log.info("下单用户{}余额为 {},商品总价为{}", userId, balance, amount);
|
||||
|
||||
if (balance < price) {
|
||||
if (balance.compareTo(amount)==-1) {
|
||||
log.warn("用户 {} 余额不足,当前余额:{}", userId, balance);
|
||||
throw new RuntimeException("余额不足");
|
||||
}
|
||||
log.info("开始扣减用户 {} 余额", userId);
|
||||
double currentBalance = account.getBalance() - price;
|
||||
BigDecimal currentBalance = account.getBalance().subtract(amount);
|
||||
account.setBalance(currentBalance);
|
||||
accountMapper.updateById(account);
|
||||
log.info("扣减用户 {} 余额成功,扣减后用户账户余额为{}", userId, currentBalance);
|
||||
@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 5002
|
||||
spring:
|
||||
application:
|
||||
name: seata-account
|
||||
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:3300/jeecg-account?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-account.sql
|
||||
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
|
||||
@ -0,0 +1,37 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for account
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `account`;
|
||||
CREATE TABLE `account` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`balance` decimal(10, 2) NULL DEFAULT NULL,
|
||||
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of account
|
||||
-- ----------------------------
|
||||
INSERT INTO `account` VALUES (1, 50.00, '2022-03-16 17:02:53');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for undo_log
|
||||
-- ----------------------------
|
||||
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) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime(0) NOT NULL,
|
||||
`log_modified` datetime(0) NOT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@ -0,0 +1,14 @@
|
||||
<?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-seata</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>分布式事务测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-seata-order</artifactId>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,18 @@
|
||||
package org.jeecg;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class SeataOrderApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataOrderApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package org.jeecg.modules.test.seata.controller;
|
||||
package org.jeecg.modules.test.seata.order.controller;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
@ -8,8 +8,9 @@ package org.jeecg.modules.test.seata.controller;
|
||||
*/
|
||||
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.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
|
||||
import org.jeecg.modules.test.seata.order.service.SeataOrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@ -18,7 +19,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/order")
|
||||
@RequestMapping("/test/seata/order")
|
||||
@Api(tags = "seata测试")
|
||||
public class SeataOrderController {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package org.jeecg.modules.test.seata.dto;
|
||||
package org.jeecg.modules.test.seata.order.dto;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
@ -24,5 +24,5 @@ public class PlaceOrderRequest {
|
||||
private Long productId;
|
||||
|
||||
@NotNull
|
||||
private Integer amount;
|
||||
private Integer count;
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package org.jeecg.modules.test.seata.dto;
|
||||
package org.jeecg.modules.test.seata.order.dto;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
@ -1,4 +1,4 @@
|
||||
package org.jeecg.modules.test.seata.dto;
|
||||
package org.jeecg.modules.test.seata.order.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@ -1,11 +1,13 @@
|
||||
package org.jeecg.modules.test.seata.entity;
|
||||
package org.jeecg.modules.test.seata.order.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;
|
||||
import org.jeecg.modules.test.seata.order.enums.OrderStatus;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 订单
|
||||
@ -36,9 +38,9 @@ public class SeataOrder {
|
||||
/**
|
||||
* 数量
|
||||
*/
|
||||
private Integer amount;
|
||||
private Integer count;
|
||||
/**
|
||||
* 总金额
|
||||
*/
|
||||
private Double totalPrice;
|
||||
private BigDecimal totalPrice;
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package org.jeecg.modules.test.seata.enums;
|
||||
package org.jeecg.modules.test.seata.order.enums;
|
||||
|
||||
/**
|
||||
* @Description: 订单状态
|
||||
@ -0,0 +1,23 @@
|
||||
package org.jeecg.modules.test.seata.order.feign;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@FeignClient(value ="seata-account")
|
||||
public interface AccountClient {
|
||||
|
||||
/**
|
||||
* 扣减余额
|
||||
* @param userId
|
||||
* @param amount
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/test/seata/account/reduceBalance")
|
||||
String reduceBalance(@RequestParam("userId") Long userId, @RequestParam("amount") BigDecimal amount);
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package org.jeecg.modules.test.seata.order.feign;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@FeignClient(value ="seata-product")
|
||||
public interface ProductClient {
|
||||
/**
|
||||
* 扣减库存
|
||||
*
|
||||
* @param productId
|
||||
* @param count
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/test/seata/product/reduceStock")
|
||||
BigDecimal reduceStock(@RequestParam("productId") Long productId, @RequestParam("count") Integer count);
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package org.jeecg.modules.test.seata.mapper;
|
||||
package org.jeecg.modules.test.seata.order.mapper;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
@ -9,7 +9,7 @@ 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.SeataOrder;
|
||||
import org.jeecg.modules.test.seata.order.entity.SeataOrder;
|
||||
|
||||
@Mapper
|
||||
public interface SeataOrderMapper extends BaseMapper<SeataOrder> {
|
||||
@ -1,6 +1,7 @@
|
||||
package org.jeecg.modules.test.seata.service;
|
||||
package org.jeecg.modules.test.seata.order.service;
|
||||
|
||||
import org.jeecg.modules.test.seata.dto.PlaceOrderRequest;
|
||||
|
||||
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
|
||||
|
||||
/**
|
||||
* @Description: 订单接口
|
||||
@ -1,20 +1,22 @@
|
||||
package org.jeecg.modules.test.seata.service.impl;
|
||||
package org.jeecg.modules.test.seata.order.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.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
|
||||
import org.jeecg.modules.test.seata.order.entity.SeataOrder;
|
||||
import org.jeecg.modules.test.seata.order.enums.OrderStatus;
|
||||
import org.jeecg.modules.test.seata.order.feign.AccountClient;
|
||||
import org.jeecg.modules.test.seata.order.feign.ProductClient;
|
||||
import org.jeecg.modules.test.seata.order.mapper.SeataOrderMapper;
|
||||
import org.jeecg.modules.test.seata.order.service.SeataOrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 订单服务类
|
||||
* @author: zyf
|
||||
@ -27,10 +29,10 @@ public class SeataOrderServiceImpl implements SeataOrderService {
|
||||
|
||||
@Resource
|
||||
private SeataOrderMapper orderMapper;
|
||||
@Autowired
|
||||
private SeataAccountService accountService;
|
||||
@Autowired
|
||||
private SeataProductService productService;
|
||||
@Resource
|
||||
private AccountClient accountClient;
|
||||
@Resource
|
||||
private ProductClient productClient;
|
||||
|
||||
@DS("order")
|
||||
@Override
|
||||
@ -40,26 +42,26 @@ public class SeataOrderServiceImpl implements SeataOrderService {
|
||||
log.info("=============ORDER START=================");
|
||||
Long userId = request.getUserId();
|
||||
Long productId = request.getProductId();
|
||||
Integer amount = request.getAmount();
|
||||
log.info("收到下单请求,用户:{}, 商品:{},数量:{}", userId, productId, amount);
|
||||
Integer count = request.getCount();
|
||||
log.info("收到下单请求,用户:{}, 商品:{},数量:{}", userId, productId, count);
|
||||
|
||||
|
||||
SeataOrder order = SeataOrder.builder()
|
||||
.userId(userId)
|
||||
.productId(productId)
|
||||
.status(OrderStatus.INIT)
|
||||
.amount(amount)
|
||||
.count(count)
|
||||
.build();
|
||||
|
||||
orderMapper.insert(order);
|
||||
log.info("订单一阶段生成,等待扣库存付款中");
|
||||
// 扣减库存并计算总价
|
||||
Double totalPrice = productService.reduceStock(productId, amount);
|
||||
BigDecimal amount = productClient.reduceStock(productId, count);
|
||||
// 扣减余额
|
||||
accountService.reduceBalance(userId, totalPrice);
|
||||
accountClient.reduceBalance(userId, amount);
|
||||
|
||||
order.setStatus(OrderStatus.SUCCESS);
|
||||
order.setTotalPrice(totalPrice);
|
||||
order.setTotalPrice(amount);
|
||||
orderMapper.updateById(order);
|
||||
log.info("订单已成功下单");
|
||||
log.info("=============ORDER END=================");
|
||||
@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 5001
|
||||
spring:
|
||||
application:
|
||||
name: seata-order
|
||||
datasource:
|
||||
dynamic:
|
||||
seata: true # 开启对 seata的支持
|
||||
seata-mode: AT #支持XA及AT模式,默认AT
|
||||
datasource:
|
||||
# 设置 账号数据源配置
|
||||
order:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3300/jeecg-order?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-order.sql
|
||||
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
|
||||
@ -0,0 +1,37 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for p_order
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `p_order`;
|
||||
CREATE TABLE `p_order` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` int(11) NULL DEFAULT NULL,
|
||||
`product_id` int(11) NULL DEFAULT NULL,
|
||||
`count` int(11) NULL DEFAULT NULL,
|
||||
`total_price` decimal(10, 2) NULL DEFAULT NULL,
|
||||
`status` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`add_time` timestamp NULL DEFAULT current_timestamp(),
|
||||
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for undo_log
|
||||
-- ----------------------------
|
||||
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) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime(0) NOT NULL,
|
||||
`log_modified` datetime(0) NOT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@ -0,0 +1,14 @@
|
||||
<?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-seata</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>分布式事务测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-seata-product</artifactId>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,16 @@
|
||||
package org.jeecg;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SeataProductApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataProductApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package org.jeecg.modules.test.seata.product.controller;
|
||||
|
||||
import org.jeecg.modules.test.seata.product.service.SeataProductService;
|
||||
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 java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/test/seata/product")
|
||||
public class SeataProductController {
|
||||
|
||||
@Autowired
|
||||
private SeataProductService seataProductService;
|
||||
|
||||
@PostMapping("/reduceStock")
|
||||
public BigDecimal reduceStock(Long productId, Integer count) {
|
||||
return seataProductService.reduceStock(productId, count);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package org.jeecg.modules.test.seata.entity;
|
||||
package org.jeecg.modules.test.seata.product.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
/**
|
||||
* @Description: 产品
|
||||
@ -23,7 +24,7 @@ public class SeataProduct {
|
||||
/**
|
||||
* 价格
|
||||
*/
|
||||
private Double price;
|
||||
private BigDecimal price;
|
||||
/**
|
||||
* 库存
|
||||
*/
|
||||
@ -1,7 +1,8 @@
|
||||
package org.jeecg.modules.test.seata.mapper;
|
||||
package org.jeecg.modules.test.seata.product.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.jeecg.modules.test.seata.entity.SeataProduct;
|
||||
import org.jeecg.modules.test.seata.product.entity.SeataProduct;
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
@ -1,4 +1,6 @@
|
||||
package org.jeecg.modules.test.seata.service;
|
||||
package org.jeecg.modules.test.seata.product.service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 产品接口
|
||||
@ -11,8 +13,8 @@ public interface SeataProductService {
|
||||
* 扣减库存
|
||||
*
|
||||
* @param productId 商品 ID
|
||||
* @param amount 扣减数量
|
||||
* @param count 扣减数量
|
||||
* @return 商品总价
|
||||
*/
|
||||
Double reduceStock(Long productId, Integer amount);
|
||||
BigDecimal reduceStock(Long productId, Integer count);
|
||||
}
|
||||
@ -1,17 +1,19 @@
|
||||
package org.jeecg.modules.test.seata.service.impl;
|
||||
package org.jeecg.modules.test.seata.product.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.jeecg.modules.test.seata.product.entity.SeataProduct;
|
||||
import org.jeecg.modules.test.seata.product.mapper.SeataProductMapper;
|
||||
import org.jeecg.modules.test.seata.product.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;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 产品服务类
|
||||
@ -32,25 +34,25 @@ public class SeataProductServiceImpl implements SeataProductService {
|
||||
@DS("product")
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Override
|
||||
public Double reduceStock(Long productId, Integer amount) {
|
||||
public BigDecimal reduceStock(Long productId, Integer count) {
|
||||
log.info("=============PRODUCT START=================");
|
||||
// 检查库存
|
||||
SeataProduct product = productMapper.selectById(productId);
|
||||
Assert.notNull(product, "商品不存在");
|
||||
Integer stock = product.getStock();
|
||||
log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, amount);
|
||||
log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, count);
|
||||
|
||||
if (stock < amount) {
|
||||
if (stock < count) {
|
||||
log.warn("商品编号为{} 库存不足,当前库存:{}", productId, stock);
|
||||
throw new RuntimeException("库存不足");
|
||||
}
|
||||
log.info("开始扣减商品编号为 {} 库存,单价商品价格为{}", productId, product.getPrice());
|
||||
// 扣减库存
|
||||
int currentStock = stock - amount;
|
||||
int currentStock = stock - count;
|
||||
product.setStock(currentStock);
|
||||
productMapper.updateById(product);
|
||||
double totalPrice = product.getPrice() * amount;
|
||||
log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, amount, totalPrice);
|
||||
BigDecimal totalPrice = product.getPrice().multiply(new BigDecimal(count));
|
||||
log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, count, totalPrice);
|
||||
log.info("=============PRODUCT END=================");
|
||||
return totalPrice;
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 5003
|
||||
spring:
|
||||
application:
|
||||
name: seata-product
|
||||
datasource:
|
||||
dynamic:
|
||||
seata: true # 开启对 seata的支持
|
||||
seata-mode: AT #支持XA及AT模式,默认AT
|
||||
datasource:
|
||||
# 设置 账号数据源配置
|
||||
product:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3300/jeecg-product?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-product.sql
|
||||
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
|
||||
@ -0,0 +1,38 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for product
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `product`;
|
||||
CREATE TABLE `product` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`price` decimal(10, 2) NULL DEFAULT NULL,
|
||||
`stock` int(11) NULL DEFAULT NULL,
|
||||
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of product
|
||||
-- ----------------------------
|
||||
INSERT INTO `product` VALUES (1, 10.00, 20, '2022-01-13 09:52:50');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for undo_log
|
||||
-- ----------------------------
|
||||
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) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime(0) NOT NULL,
|
||||
`log_modified` datetime(0) NOT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@ -5,18 +5,27 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>分布式事务测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-seata</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>jeecg-cloud-test-seata-account</module>
|
||||
<module>jeecg-cloud-test-seata-product</module>
|
||||
<module>jeecg-cloud-test-seata-order</module>
|
||||
</modules>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-cloud</artifactId>
|
||||
<version>${jeecgboot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-seata</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<version>${jeecgboot.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,38 +0,0 @@
|
||||
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
|
||||
@ -1,29 +0,0 @@
|
||||
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);
|
||||
@ -1,32 +0,0 @@
|
||||
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;
|
||||
@ -1,31 +0,0 @@
|
||||
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);
|
||||
@ -1,12 +0,0 @@
|
||||
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;
|
||||
@ -21,6 +21,7 @@ CREATE TABLE `sys_log0` (
|
||||
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操作日志)',
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-shardingsphere</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@ -23,18 +23,17 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
@Api(tags = "分库分表测试")
|
||||
@RestController
|
||||
@RequestMapping("/sharding/")
|
||||
@RequestMapping("/sharding")
|
||||
public class JeecgShardingDemoController extends JeecgController<ShardingSysLog, IShardingSysLogService> {
|
||||
@Autowired
|
||||
private IShardingSysLogService shardingSysLogService;
|
||||
|
||||
/**
|
||||
* 单库分表
|
||||
* 单库分表 —— 添加
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/test1")
|
||||
@AutoLog(value = "单库分表")
|
||||
@ApiOperation(value = "单库分表", notes = "分库分表添加")
|
||||
@ApiOperation(value = "单库分表插入", notes = "单库分表")
|
||||
public Result<?> add() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ShardingSysLog shardingSysLog = new ShardingSysLog();
|
||||
@ -45,13 +44,23 @@ public class JeecgShardingDemoController extends JeecgController<ShardingSysLog,
|
||||
}
|
||||
return Result.OK();
|
||||
}
|
||||
|
||||
/**
|
||||
* 双库分表
|
||||
* 单库分表 —— 查询
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/list1")
|
||||
@ApiOperation(value = "单库分表查询", notes = "单库分表")
|
||||
public Result<?> list() {
|
||||
return Result.OK(shardingSysLogService.list());
|
||||
}
|
||||
|
||||
/**
|
||||
* 双库分表 - 插入
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/test2")
|
||||
@AutoLog(value = "双库分表")
|
||||
@ApiOperation(value = "双库分表", notes = "双库分表")
|
||||
@ApiOperation(value = "双库分表插入", notes = "双库分表")
|
||||
public Result<?> test2() {
|
||||
for (int i = 20; i <= 30; i++) {
|
||||
ShardingSysLog shardingSysLog = new ShardingSysLog();
|
||||
@ -63,4 +72,14 @@ public class JeecgShardingDemoController extends JeecgController<ShardingSysLog,
|
||||
return Result.OK();
|
||||
}
|
||||
|
||||
/**
|
||||
* 双库分表 - 查询
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/list2")
|
||||
@ApiOperation(value = "双库分表查询", notes = "双库分表")
|
||||
public Result<?> list2() {
|
||||
return Result.OK(shardingSysLogService.list());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
#单库分表配置
|
||||
spring:
|
||||
shardingsphere:
|
||||
props:
|
||||
@ -6,10 +7,10 @@ spring:
|
||||
#添加分库数据源
|
||||
ds0:
|
||||
driverClassName: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3300/jeecg-boot?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
url: jdbc:mysql://jeecg-boot-mysql:3306/jeecg-boot?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
username: root
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
password: root
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
names: ds0
|
||||
# 规则配置
|
||||
rules:
|
||||
|
||||
@ -6,13 +6,13 @@ spring:
|
||||
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
|
||||
url: jdbc:mysql://jeecg-boot-mysql:3306/jeecg-boot?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
username: root
|
||||
password: root
|
||||
ds1:
|
||||
driverClassName: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/jeecg-boot2?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
url: jdbc:mysql://jeecg-boot-mysql:3306/jeecg-boot2?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
username: root
|
||||
password: root
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-module</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@ -20,9 +20,9 @@
|
||||
</dependencies>
|
||||
|
||||
<modules>
|
||||
<module>jeecg-cloud-test-seata</module>
|
||||
<module>jeecg-cloud-test-shardingsphere</module>
|
||||
<module>jeecg-cloud-test-more</module>
|
||||
<module>jeecg-cloud-test-rabbitmq</module>
|
||||
<module>jeecg-cloud-test-seata</module>
|
||||
</modules>
|
||||
</project>
|
||||
Reference in New Issue
Block a user