Commit c828969f authored by 赵啸非's avatar 赵啸非

添加幂等测试类

parent 38e1d2c5
package com.mortals.xhx.common.pdu;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.web.BaseForm;
public class LoginForm extends BaseForm {
public class LoginForm {
private String loginName;
private String password;
......@@ -40,17 +39,4 @@ public class LoginForm extends BaseForm {
return "loginName:" + this.loginName + " password:" + this.password;
}
@Override
public boolean validate() throws AppException {
if (loginName == null || loginName.trim().length() == 0) {
throw new AppException("帐号不能为空!");
}
if (password == null || password.trim().length() == 0) {
throw new AppException("密码不能为空!");
}
// if (securityCode == null || securityCode.trim().length() == 0) {
// throw new AppException("验证码不能为空!");
// }
return super.validate();
}
}
......@@ -28,11 +28,6 @@
<profiles.nacos.server-addr>127.0.0.1:8848</profiles.nacos.server-addr>
<profiles.nacos.group>DEFAULT_GROUP</profiles.nacos.group>
<profiles.nacos.namespace>smart-gov</profiles.nacos.namespace>
<profiles.rabbitmq.host>192.168.0.251</profiles.rabbitmq.host>
<profiles.rabbitmq.port>5672</profiles.rabbitmq.port>
<profiles.rabbitmq.username>root_mq</profiles.rabbitmq.username>
<profiles.rabbitmq.password>xhx@2022</profiles.rabbitmq.password>
<profiles.rabbitmq.virtualhost>/</profiles.rabbitmq.virtualhost>
<profiles.log.path>/home/mortals/app/logs</profiles.log.path>
<profiles.log.level>info</profiles.log.level>
<profiles.publish.path>/home/publish</profiles.publish.path>
......
......@@ -17,8 +17,7 @@ import java.awt.image.BufferedImage;
@RestController
@RequestMapping("securitycode")
public class SecurityCodeController
extends BaseCRUDJsonController<ValidCodeService, ValidCodeForm, ValidCodeEntity, Long> {
public class SecurityCodeController extends BaseCRUDJsonController<ValidCodeService, ValidCodeForm, ValidCodeEntity, Long> {
@Autowired
private ValidCodeService validCodeService;
......
......@@ -21,7 +21,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......
......@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.model.ParamEntity;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.DataSatusEnum;
......
......@@ -17,7 +17,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.resource.model.ResourceEntity;
import com.mortals.xhx.base.system.resource.service.ResourceService;
import com.mortals.xhx.common.code.AuthType;
......
......@@ -7,7 +7,6 @@ import com.mortals.framework.common.code.UserType;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.framework.web.BasePhpCRUDJsonMappingController;
import com.mortals.xhx.base.system.role.model.RoleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
......
package com.mortals.xhx.busiz.controller;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.enhance.idempotent.demo.app.service.TestIdempotentAnnotationService;
import org.enhance.idempotent.demo.domain.entity.OrderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
/**
* 测试幂等注解
*
* @author wenpan 2023/01/07 10:13
*/
@Slf4j
@RestController("TestIdempotentAnnotationController.v1")
@RequestMapping("/v1/idempotent-annotation")
public class TestIdempotentAnnotationController {
@Autowired
private TestIdempotentAnnotationService testIdempotentAnnotationService;
/**
* 测试幂等注解
* 请求URL:localhost:12345/v1/idempotent-annotation/add-order?orderNumber=TB-202301070001&orderAmount=1000&platformCode=taobao
*/
@PostMapping("/add-order")
public OrderEntity addOrder(@RequestParam String orderNumber,
@RequestParam String orderAmount,
@RequestParam String platformCode) {
log.info("======>>>>>>addOrder orderNumber is {}, orderAmount is {}, platformCode is {}", orderNumber, orderAmount, platformCode);
// 构建待新增的订单信息
OrderEntity orderEntity = new OrderEntity();
orderEntity.setOrderNumber(orderNumber);
orderEntity.setOrderAmount(new BigDecimal(orderAmount));
orderEntity.setOrderStatus(1);
orderEntity.setPlatformCode(platformCode);
// 新增订单
orderEntity = testIdempotentAnnotationService.addOrder(orderEntity);
log.info("======>>>>>>addOrder result is {}", JSON.toJSONString(orderEntity));
return orderEntity;
}
/**
* 测试幂等注解
* <p>
* 请求URL:localhost:12345/v1/idempotent-annotation/modify-order
* 入参:
* {
* "platformCode": "tianmao",
* "orderNumber": "TM-202301070001",
* "orderAmount": "1000",
* "orderStatus": 1,
* "orderLineEntityList": [
* {
* "orderLineNumber": "line-202301070001",
* "skuCode": "skuCode202301070001",
* "unitPrice": "200"
* },
* {
* "orderLineNumber": "line-202301070002",
* "skuCode": "skuCode202301070001",
* "unitPrice": "200"
* }
* ]
* }
* </p>
*/
@PostMapping("/modify-order")
public OrderEntity modifyOrder(@RequestBody OrderEntity orderEntity) {
log.info("=====>>>>>modifyOrder input params is : {}", JSON.toJSONString(orderEntity));
// 修改
orderEntity = testIdempotentAnnotationService.modifyOrder(orderEntity);
log.info("=====>>>>>modifyOrder input result is : {}", JSON.toJSONString(orderEntity));
return orderEntity;
}
}
package com.mortals.xhx.busiz.controller;
import com.mortals.framework.idempotent.helper.IdempotentHelper;
import com.mortals.xhx.busiz.service.TestIdempotentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 测试基于redis的幂等
*
* @author wenpan 2023/01/04 22:56
*/
@Slf4j
@RestController("TestRedisIdempotentController.v1")
@RequestMapping("/v1/idempotent")
public class TestIdempotentController {
@Autowired
private IdempotentHelper idempotentHelper;
@Autowired
private TestIdempotentService testIdempotentService;
/**
* 测试idempotentHelper执行有返回值的方法
* url: localhost:12345/v1/idempotent/test-with-result?source=taobao&operationType=publish_product&businessKey=pd_20230105007&name=lisi
*/
@GetMapping("/test-with-result")
public String testWithResult(String source,
String operationType,
String businessKey,
String name) {
// 带返回值的幂等执行
String result = idempotentHelper.invoke(source, operationType, businessKey,
() -> testIdempotentService.testIdempotentHelper(name));
log.info("=============>>>>>>> TestRedisIdempotentController result {}", result);
return result;
}
/**
* 测试idempotentHelper执行没有返回值的方法
* url : localhost:12345/v1/idempotent/test-with-no-result?source=taobao&operationType=publish_product&businessKey=pd_20230105008&name=zhangsan
*/
@GetMapping("/test-with-no-result")
public String testWithNoResult(String source,
String operationType,
String businessKey,
String name) {
// 不带返回值的幂等执行
idempotentHelper.invokeWithNoResult(source, operationType, businessKey,
() -> testIdempotentService.testIdempotentWithNoResult(name));
log.info("=============>>>>>>> TestRedisIdempotentController testWithNoResult.");
return "success";
}
}
package com.mortals.xhx.busiz.entity;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
/**
* 订单entity
*
* @author wenpan 2023/1/7 10:19 上午
*/
@Data
public class OrderEntity {
/**
* 订单ID
*/
private Integer orderId;
/**
* 订单来源平台
*/
private String platformCode;
/**
* 订单名称
*/
private String orderName;
/**
* 订单编号
*/
private String orderNumber;
/**
* 订单总金额
*/
private BigDecimal orderAmount;
/**
* 订单状态
*/
private Integer orderStatus;
/**
* 创建日期
*/
private LocalDateTime createDate;
/**
* 创建日期
*/
private LocalDateTime lastModifyDate;
/**
* 订单行集合
*/
private List<OrderLineEntity> orderLineEntityList;
}
\ No newline at end of file
package com.mortals.xhx.busiz.entity;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 订单行entity
*
* @author wenpan 2023/01/07 10:19
*/
@Data
public class OrderLineEntity {
/**
* 订单行ID
*/
private Long orderLineId;
/**
* 订单行编号
*/
private String orderLineNumber;
/**
* 对应的sku编号
*/
private String skuCode;
/**
* 价格
*/
private BigDecimal unitPrice;
/**
* 创建日期
*/
private LocalDateTime createDate;
/**
* 创建日期
*/
private LocalDateTime lastModifyDate;
}
package com.mortals.xhx.busiz.service;
import com.mortals.xhx.busiz.entity.OrderEntity;
/**
* 测试幂等注解service
*
* @author wenpan 2023/01/07 10:14
*/
public interface TestIdempotentAnnotationService {
/**
* 新增订单
*
* @param orderEntity 订单实体
* @return OrderEntity
* @author wenpan 2023/1/7 10:16 上午
*/
OrderEntity addOrder(OrderEntity orderEntity);
/**
* 修改订单
*
* @param orderEntity 订单实体
* @return org.enhance.idempotent.demo.domain.entity.OrderEntity
* @author wenpan 2023/1/7 10:38 上午
*/
OrderEntity modifyOrder(OrderEntity orderEntity);
}
package com.mortals.xhx.busiz.service;
/**
* TestRedisIdempotentService
*
* @author wenpan 2023/01/04 23:02
*/
public interface TestIdempotentService {
/**
* 测试 IdempotentHelper
*
* @param name name
* @return java.lang.String
*/
String testIdempotentHelper(String name);
/**
* 测试不带返回值的幂等执行
*
* @param name name
*/
void testIdempotentWithNoResult(String name);
}
package com.mortals.xhx.busiz.service.impl;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.idempotent.annotation.Idempotent;
import com.mortals.xhx.busiz.entity.OrderEntity;
import com.mortals.xhx.busiz.service.TestIdempotentAnnotationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
/**
* TestIdempotentAnnotationService实现
*
* @author wenpan 2023/01/07 10:15
*/
@Slf4j
@Service
public class TestIdempotentAnnotationServiceImpl implements TestIdempotentAnnotationService {
/**
* 用orderEntity对象里的orderNumber作为业务幂等键
*/
@Idempotent(operationType = "'addOrder'", source = "#orderEntity.platformCode",
determineCurrentLookupKey = "mySqlIdempotentRepository", businessKey = "#orderEntity.orderNumber")
@Override
public OrderEntity addOrder(OrderEntity orderEntity) {
log.info("TestIdempotentAnnotationServiceImpl#addOrder orderEntity is : {}", JSON.toJSONString(orderEntity));
// 模拟修改了订单一些信息
orderEntity.setCreateDate(LocalDateTime.now());
orderEntity.setLastModifyDate(LocalDateTime.now());
orderEntity.setOrderStatus(orderEntity.getOrderStatus() + 1);
// 新增订单逻辑....
return orderEntity;
}
/**
* 用orderEntity对象里的orderLineEntityList集合里的第一个元素的skuCode作为业务幂等键
*/
@Idempotent(operationType = "'modifyOrder'",
source = "#orderEntity.platformCode",
businessKey = "#orderEntity.orderLineEntityList[0].skuCode")
@Override
public OrderEntity modifyOrder(OrderEntity orderEntity) {
log.info("TestIdempotentAnnotationServiceImpl#modifyOrder orderEntity is : {}", JSON.toJSONString(orderEntity));
// 模拟修改了订单一些信息
orderEntity.setCreateDate(LocalDateTime.now());
orderEntity.setLastModifyDate(LocalDateTime.now());
orderEntity.setOrderStatus(orderEntity.getOrderStatus() + 1);
// 修改订单逻辑....
return orderEntity;
}
}
package com.mortals.xhx.busiz.service.impl;
import com.mortals.xhx.busiz.service.TestIdempotentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* TestRedisIdempotentService 实现类
*
* @author wenpan 2023/01/04 23:03
*/
@Slf4j
@Service
public class TestIdempotentServiceImpl implements TestIdempotentService {
@Override
public String testIdempotentHelper(String name) {
log.info("========>>>>>>> hello {}", name);
return name + "-hello";
}
@Override
public void testIdempotentWithNoResult(String name) {
log.info("testIdempotentWithNoResult params is : {}", name);
}
}
......@@ -21,12 +21,6 @@ spring:
default-property-inclusion: NON_NULL
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
rabbitmq:
host: @profiles.rabbitmq.host@
port: @profiles.rabbitmq.port@
username: @profiles.rabbitmq.username@
password: @profiles.rabbitmq.password@
virtualHost: @profiles.rabbitmq.virtualhost@
dao:
exceptiontranslation:
enabled: false
......@@ -63,4 +57,19 @@ hystrix:
isolation:
thread:
timeoutInMilliseconds: 60000
enhance:
idempotent:
# 开启幂等组件
enable: true
namespace: idempotent-test
maxProcessTime: 10
# 开启多个实现时,指定哪个是primary
primaryRepository: redisIdempotentRepository
# 适配器选择(任选一种模式开启即可)
adapter:
redis:
# 开启基于Redis的幂等实现
enable: true
expireTime: 120
unit: SECONDS
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment