demo commit .

This commit is contained in:
Administrator 2018-12-11 19:23:36 +08:00
parent 153121a11a
commit 7348a226ce
11 changed files with 80 additions and 136 deletions

View File

@ -73,14 +73,14 @@ public class AccountServiceImpl implements AccountService {
@Override @Override
@Hmily(confirmMethod = "confirm", cancelMethod = "cancel") @Hmily(confirmMethod = "confirm", cancelMethod = "cancel")
public void payment(AccountDTO accountDTO) { public void payment(AccountDTO accountDTO) {
//accountMapper.update(accountDTO); accountMapper.update(accountDTO);
/*final int i = trycount.incrementAndGet(); /*final int i = trycount.incrementAndGet();
System.out.println("调用了account try " + i + "");*/ System.out.println("调用了account try " + i + "");*/
} }
@Override @Override
public boolean testPayment(AccountDTO accountDTO) { public boolean testPayment(AccountDTO accountDTO) {
//accountMapper.update(accountDTO); accountMapper.update(accountDTO);
return Boolean.TRUE; return Boolean.TRUE;
} }

View File

@ -71,15 +71,15 @@ public class InventoryServiceImpl implements InventoryService {
@Override @Override
@Hmily(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod") @Hmily(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
public Boolean decrease(InventoryDTO inventoryDTO) { public Boolean decrease(InventoryDTO inventoryDTO) {
//inventoryMapper.decrease(inventoryDTO); inventoryMapper.decrease(inventoryDTO);
/* final int i = tryCount.incrementAndGet(); final int i = tryCount.incrementAndGet();
System.out.println("调用了inventory try " + i + "");*/ System.out.println("调用了inventory try " + i + "");
return true; return true;
} }
@Override @Override
public Boolean testDecrease(InventoryDTO inventoryDTO) { public Boolean testDecrease(InventoryDTO inventoryDTO) {
//inventoryMapper.decrease(inventoryDTO); inventoryMapper.decrease(inventoryDTO);
return true; return true;
} }
@ -182,7 +182,6 @@ public class InventoryServiceImpl implements InventoryService {
* @param inventoryDTO the inventory dto * @param inventoryDTO the inventory dto
* @return the boolean * @return the boolean
*/ */
@Transactional(rollbackFor = Exception.class)
public Boolean confirmMethod(InventoryDTO inventoryDTO) { public Boolean confirmMethod(InventoryDTO inventoryDTO) {
inventoryMapper.confirm(inventoryDTO); inventoryMapper.confirm(inventoryDTO);
final int i = confirmCount.incrementAndGet(); final int i = confirmCount.incrementAndGet();
@ -196,7 +195,6 @@ public class InventoryServiceImpl implements InventoryService {
* @param inventoryDTO the inventory dto * @param inventoryDTO the inventory dto
* @return the boolean * @return the boolean
*/ */
@Transactional(rollbackFor = Exception.class)
public Boolean cancelMethod(InventoryDTO inventoryDTO) { public Boolean cancelMethod(InventoryDTO inventoryDTO) {
LOGGER.info("==========调用扣减库存取消方法==========="); LOGGER.info("==========调用扣减库存取消方法===========");
inventoryMapper.cancel(inventoryDTO); inventoryMapper.cancel(inventoryDTO);

View File

@ -58,16 +58,16 @@ public class OrderServiceImpl implements OrderService {
@Override @Override
public String orderPay(Integer count, BigDecimal amount) { public String orderPay(Integer count, BigDecimal amount) {
/* final Order order = buildTestOrder(count, amount); final Order order = buildOrder(count, amount);
final int rows = orderMapper.save(order); final int rows = orderMapper.save(order);
if (rows > 0) { if (rows > 0) {
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
paymentService.makePayment(order); paymentService.makePayment(order);
System.out.println("切面耗时:" + (System.currentTimeMillis() - start)); System.out.println("切面耗时:" + (System.currentTimeMillis() - start));
}*/ }
final long start = System.currentTimeMillis(); /* final long start = System.currentTimeMillis();
paymentService.makePayment(new Order()); paymentService.makePayment(new Order());
System.out.println("切面耗时:" + (System.currentTimeMillis() - start)); System.out.println("切面耗时:" + (System.currentTimeMillis() - start));*/
return "success"; return "success";
} }

View File

@ -66,8 +66,8 @@ public class PaymentServiceImpl implements PaymentService {
@Override @Override
@Hmily(confirmMethod = "confirmOrderStatus", cancelMethod = "cancelOrderStatus") @Hmily(confirmMethod = "confirmOrderStatus", cancelMethod = "cancelOrderStatus")
public void makePayment(Order order) { public void makePayment(Order order) {
/* order.setStatus(OrderStatusEnum.PAYING.getCode()); order.setStatus(OrderStatusEnum.PAYING.getCode());
orderMapper.update(order);*/ orderMapper.update(order);
//做库存和资金账户的检验工作 这里只是demo //做库存和资金账户的检验工作 这里只是demo
/* final AccountDO accountDO = accountService.findByUserId(order.getUserId()); /* final AccountDO accountDO = accountService.findByUserId(order.getUserId());
if (accountDO.getBalance().compareTo(order.getTotalAmount()) <= 0) { if (accountDO.getBalance().compareTo(order.getTotalAmount()) <= 0) {

View File

@ -21,7 +21,7 @@ org:
scheduledThreadMax : 10 scheduledThreadMax : 10
repositorySupport : db repositorySupport : db
started: true started: true
async-threads: 500 async-threads: 200
hmilyDbConfig : hmilyDbConfig :
driverClassName : com.mysql.jdbc.Driver driverClassName : com.mysql.jdbc.Driver
url : jdbc:mysql://192.168.1.98:3306/tcc?useUnicode=true&amp;characterEncoding=utf8 url : jdbc:mysql://192.168.1.98:3306/tcc?useUnicode=true&amp;characterEncoding=utf8

View File

@ -17,58 +17,60 @@
package org.dromara.hmily.demo.springcloud.account.mapper; package org.dromara.hmily.demo.springcloud.account.mapper;
import org.dromara.hmily.demo.springcloud.account.entity.AccountDO;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Update;
import org.dromara.hmily.demo.springcloud.account.dto.AccountDTO;
import org.dromara.hmily.demo.springcloud.account.entity.AccountDO;
/** /**
* The interface Account mapper.
*
* @author xiaoyu * @author xiaoyu
*/ */
@SuppressWarnings("all") @SuppressWarnings("all")
public interface AccountMapper { public interface AccountMapper {
/** /**
* 扣减账户余额 * Update int.
* *
* @param accountDO 实体类 * @param accountDTO the account dto
* @return rows * @return the int
*/ */
@Update("update account set balance =#{balance}," + @Update("update account set balance = balance - #{amount}," +
" freeze_amount= #{freezeAmount} ,update_time = #{updateTime}" + " freeze_amount= freeze_amount + #{amount} ,update_time = now()" +
" where user_id =#{userId} and balance > 0 ") " where user_id =#{userId} and balance > 0 ")
int update(AccountDO accountDO); int update(AccountDTO accountDTO);
/** /**
* 确认扣减账户余额 * Confirm int.
* *
* @param accountDO 实体类 * @param accountDTO the account dto
* @return rows * @return the int
*/ */
@Update("update account set " + @Update("update account set " +
" freeze_amount= #{freezeAmount} ,update_time = #{updateTime}" + " freeze_amount= freeze_amount - #{amount}" +
" where user_id =#{userId} and freeze_amount >0 ") " where user_id =#{userId} and freeze_amount >0 ")
int confirm(AccountDO accountDO); int confirm(AccountDTO accountDTO);
/** /**
* 取消扣减账户余额 * Cancel int.
* *
* @param accountDO 实体类 * @param accountDO the account do
* @return rows * @return the int
*/ */
@Update("update account set balance =#{balance}," + @Update("update account set balance = balance + #{amount}," +
" freeze_amount= #{freezeAmount} ,update_time = #{updateTime}" + " freeze_amount= freeze_amount - #{amount} " +
" where user_id =#{userId} and freeze_amount >0") " where user_id =#{userId} and freeze_amount >0")
int cancel(AccountDO accountDO); int cancel(AccountDTO accountDTO);
/** /**
* 根据userId获取用户账户信息 * Find by user id account do.
* *
* @param userId 用户id * @param userId the user id
* @return AccountDO * @return the account do
*/ */
@Select("select * from account where user_id =#{userId}") @Select("select id,user_id,balance, freeze_amount from account where user_id =#{userId} limit 1")
AccountDO findByUserId(String userId); AccountDO findByUserId(String userId);
} }

View File

@ -19,14 +19,10 @@ package org.dromara.hmily.demo.springcloud.account.service.impl;
import org.dromara.hmily.annotation.Hmily; import org.dromara.hmily.annotation.Hmily;
import org.dromara.hmily.common.exception.HmilyRuntimeException; import org.dromara.hmily.common.exception.HmilyRuntimeException;
import org.dromara.hmily.demo.springcloud.account.dto.AccountDTO; import org.dromara.hmily.demo.springcloud.account.dto.AccountDTO;
import org.dromara.hmily.demo.springcloud.account.entity.AccountDO; import org.dromara.hmily.demo.springcloud.account.entity.AccountDO;
import org.dromara.hmily.demo.springcloud.account.mapper.AccountMapper; import org.dromara.hmily.demo.springcloud.account.mapper.AccountMapper;
import org.dromara.hmily.demo.springcloud.account.service.AccountService; import org.dromara.hmily.demo.springcloud.account.service.AccountService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -34,8 +30,6 @@ import org.springframework.stereotype.Service;
import java.util.Date; import java.util.Date;
import org.springframework.transaction.annotation.Transactional;
/** /**
* @author xiaoyu * @author xiaoyu
*/ */
@ -64,23 +58,9 @@ public class AccountServiceImpl implements AccountService {
*/ */
@Override @Override
@Hmily(confirmMethod = "confirm", cancelMethod = "cancel") @Hmily(confirmMethod = "confirm", cancelMethod = "cancel")
@Transactional
public boolean payment(AccountDTO accountDTO) { public boolean payment(AccountDTO accountDTO) {
LOGGER.debug("============springcloud执行try付款接口==============="); LOGGER.debug("============springcloud执行try付款接口===============");
final AccountDO accountDO = accountMapper.findByUserId(accountDTO.getUserId()); accountMapper.update(accountDTO);
accountDO.setBalance(accountDO.getBalance().subtract(accountDTO.getAmount()));
accountDO.setFreezeAmount(accountDO.getFreezeAmount().add(accountDTO.getAmount()));
accountDO.setUpdateTime(new Date());
final int update = accountMapper.update(accountDO);
if (update != 1) {
throw new HmilyRuntimeException("资金不足!");
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// throw new RuntimeException(""); // throw new RuntimeException("");
return Boolean.TRUE; return Boolean.TRUE;
} }
@ -97,28 +77,13 @@ public class AccountServiceImpl implements AccountService {
} }
public boolean confirm(AccountDTO accountDTO) { public boolean confirm(AccountDTO accountDTO) {
final int rows = accountMapper.confirm(accountDTO);
LOGGER.debug("============springcloud tcc 执行确认付款接口===============");
final AccountDO accountDO = accountMapper.findByUserId(accountDTO.getUserId());
accountDO.setFreezeAmount(accountDO.getFreezeAmount().subtract(accountDTO.getAmount()));
accountDO.setUpdateTime(new Date());
final int rows = accountMapper.confirm(accountDO);
if (rows != 1) {
throw new HmilyRuntimeException("确认扣减账户异常!");
}
return Boolean.TRUE; return Boolean.TRUE;
} }
public boolean cancel(AccountDTO accountDTO) { public boolean cancel(AccountDTO accountDTO) {
final int rows = accountMapper.cancel(accountDTO);
LOGGER.debug("============springcloud tcc 执行取消付款接口===============");
final AccountDO accountDO = accountMapper.findByUserId(accountDTO.getUserId());
accountDO.setBalance(accountDO.getBalance().add(accountDTO.getAmount()));
accountDO.setFreezeAmount(accountDO.getFreezeAmount().subtract(accountDTO.getAmount()));
accountDO.setUpdateTime(new Date());
final int rows = accountMapper.cancel(accountDO);
if (rows != 1) { if (rows != 1) {
throw new HmilyRuntimeException("取消扣减账户异常!"); throw new HmilyRuntimeException("取消扣减账户异常!");
} }

View File

@ -17,6 +17,7 @@
package org.dromara.hmily.demo.springcloud.inventory.mapper; package org.dromara.hmily.demo.springcloud.inventory.mapper;
import org.dromara.hmily.demo.springcloud.inventory.dto.InventoryDTO;
import org.dromara.hmily.demo.springcloud.inventory.entity.InventoryDO; import org.dromara.hmily.demo.springcloud.inventory.entity.InventoryDO;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Update;
@ -27,46 +28,49 @@ import org.apache.ibatis.annotations.Update;
@SuppressWarnings("all") @SuppressWarnings("all")
public interface InventoryMapper { public interface InventoryMapper {
/**
* 库存扣减.
*
* @param inventory 实体对象
* @return rows
*/
@Update("update inventory set total_inventory =#{totalInventory}," +
" lock_inventory= #{lockInventory} " +
" where product_id =#{productId} and total_inventory >0 ")
int decrease(InventoryDO inventory);
/** /**
* 库存扣减confirm. * Decrease int.
* *
* @param inventory 实体对象 * @param inventoryDTO the inventory dto
* @return rows * @return the int
*/
@Update("update inventory set total_inventory = total_inventory - #{count}," +
" lock_inventory= lock_inventory + #{count} " +
" where product_id =#{productId} and total_inventory >0 ")
int decrease(InventoryDTO inventoryDTO);
/**
* Confirm int.
*
* @param inventoryDTO the inventory dto
* @return the int
*/ */
@Update("update inventory set " + @Update("update inventory set " +
" lock_inventory= #{lockInventory} " + " lock_inventory= lock_inventory - #{count} " +
" where product_id =#{productId} and lock_inventory >0 ") " where product_id =#{productId} and lock_inventory >0 ")
int confirm(InventoryDO inventory); int confirm(InventoryDTO inventoryDTO);
/** /**
* 库存扣减 cancel. * Cancel int.
* *
* @param inventory 实体对象 * @param inventoryDTO the inventory dto
* @return rows * @return the int
*/ */
@Update("update inventory set total_inventory =#{totalInventory}," + @Update("update inventory set total_inventory = total_inventory + #{count}," +
" lock_inventory= #{lockInventory} " + " lock_inventory= lock_inventory - #{count} " +
" where product_id =#{productId} and lock_inventory >0 ") " where product_id =#{productId} and lock_inventory >0 ")
int cancel(InventoryDO inventory); int cancel(InventoryDTO inventoryDTO);
/** /**
* 根据商品id找到库存信息. * Find by product id inventory do.
* *
* @param productId 商品id * @param productId the product id
* @return Inventory * @return the inventory do
*/ */
@Select("select * from inventory where product_id =#{productId}") @Select("select id,product_id,total_inventory ,lock_inventory from inventory where product_id =#{productId}")
InventoryDO findByProductId(String productId); InventoryDO findByProductId(String productId);
} }

View File

@ -19,7 +19,6 @@ package org.dromara.hmily.demo.springcloud.inventory.service.impl;
import org.dromara.hmily.annotation.Hmily; import org.dromara.hmily.annotation.Hmily;
import org.dromara.hmily.common.exception.HmilyRuntimeException; import org.dromara.hmily.common.exception.HmilyRuntimeException;
import org.dromara.hmily.demo.springcloud.inventory.dto.InventoryDTO; import org.dromara.hmily.demo.springcloud.inventory.dto.InventoryDTO;
import org.dromara.hmily.demo.springcloud.inventory.entity.InventoryDO; import org.dromara.hmily.demo.springcloud.inventory.entity.InventoryDO;
import org.dromara.hmily.demo.springcloud.inventory.mapper.InventoryMapper; import org.dromara.hmily.demo.springcloud.inventory.mapper.InventoryMapper;
@ -36,7 +35,6 @@ import org.springframework.transaction.annotation.Transactional;
* @author xiaoyu * @author xiaoyu
*/ */
@Service("inventoryService") @Service("inventoryService")
@SuppressWarnings("all")
public class InventoryServiceImpl implements InventoryService { public class InventoryServiceImpl implements InventoryService {
/** /**
@ -60,17 +58,9 @@ public class InventoryServiceImpl implements InventoryService {
*/ */
@Override @Override
@Hmily(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod") @Hmily(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
@Transactional
public Boolean decrease(InventoryDTO inventoryDTO) { public Boolean decrease(InventoryDTO inventoryDTO) {
LOGGER.info("==========springcloud调用扣减库存decrease==========="); LOGGER.info("==========springcloud调用扣减库存decrease===========");
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId()); inventoryMapper.decrease(inventoryDTO);
entity.setTotalInventory(entity.getTotalInventory() - inventoryDTO.getCount());
entity.setLockInventory(entity.getLockInventory() + inventoryDTO.getCount());
final int decrease = inventoryMapper.decrease(entity);
if (decrease != 1) {
throw new HmilyRuntimeException("库存不足");
}
// throw new RuntimeException("测试");
return true; return true;
} }
@ -104,10 +94,7 @@ public class InventoryServiceImpl implements InventoryService {
e.printStackTrace(); e.printStackTrace();
} }
LOGGER.info("==========springcloud调用扣减库存mockWithTryTimeout==========="); LOGGER.info("==========springcloud调用扣减库存mockWithTryTimeout===========");
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId()); final int decrease = inventoryMapper.decrease(inventoryDTO);
entity.setTotalInventory(entity.getTotalInventory() - inventoryDTO.getCount());
entity.setLockInventory(entity.getLockInventory() + inventoryDTO.getCount());
final int decrease = inventoryMapper.decrease(entity);
if (decrease != 1) { if (decrease != 1) {
throw new HmilyRuntimeException("库存不足"); throw new HmilyRuntimeException("库存不足");
} }
@ -123,18 +110,14 @@ public class InventoryServiceImpl implements InventoryService {
e.printStackTrace(); e.printStackTrace();
} }
LOGGER.info("==========Springcloud调用扣减库存确认方法==========="); LOGGER.info("==========Springcloud调用扣减库存确认方法===========");
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId()); inventoryMapper.decrease(inventoryDTO);
entity.setLockInventory(entity.getLockInventory() - inventoryDTO.getCount());
inventoryMapper.decrease(entity);
return true; return true;
} }
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Boolean confirmMethodException(InventoryDTO inventoryDTO) { public Boolean confirmMethodException(InventoryDTO inventoryDTO) {
LOGGER.info("==========Springcloud调用扣减库存确认方法==========="); LOGGER.info("==========Springcloud调用扣减库存确认方法===========");
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId()); final int decrease = inventoryMapper.decrease(inventoryDTO);
entity.setLockInventory(entity.getLockInventory() - inventoryDTO.getCount());
final int decrease = inventoryMapper.decrease(entity);
if (decrease != 1) { if (decrease != 1) {
throw new HmilyRuntimeException("库存不足"); throw new HmilyRuntimeException("库存不足");
} }
@ -145,24 +128,13 @@ public class InventoryServiceImpl implements InventoryService {
public Boolean confirmMethod(InventoryDTO inventoryDTO) { public Boolean confirmMethod(InventoryDTO inventoryDTO) {
LOGGER.info("==========Springcloud调用扣减库存确认方法==========="); LOGGER.info("==========Springcloud调用扣减库存确认方法===========");
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId()); final int rows = inventoryMapper.confirm(inventoryDTO);
entity.setLockInventory(entity.getLockInventory() - inventoryDTO.getCount());
final int rows = inventoryMapper.confirm(entity);
if (rows != 1) {
throw new HmilyRuntimeException("确认库存操作失败!");
}
return true; return true;
} }
public Boolean cancelMethod(InventoryDTO inventoryDTO) { public Boolean cancelMethod(InventoryDTO inventoryDTO) {
LOGGER.info("==========Springcloud调用扣减库存取消方法==========="); LOGGER.info("==========Springcloud调用扣减库存取消方法===========");
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId()); int rows = inventoryMapper.cancel(inventoryDTO);
entity.setTotalInventory(entity.getTotalInventory() + inventoryDTO.getCount());
entity.setLockInventory(entity.getLockInventory() - inventoryDTO.getCount());
int rows = inventoryMapper.cancel(entity);
if (rows != 1) {
throw new HmilyRuntimeException("取消库存操作失败!");
}
return true; return true;
} }

View File

@ -39,6 +39,7 @@
<property name="scheduledDelay" value="120"/> <property name="scheduledDelay" value="120"/>
<property name="scheduledThreadMax" value="4"/> <property name="scheduledThreadMax" value="4"/>
<property name="repositorySupport" value="db"/> <property name="repositorySupport" value="db"/>
<property name="started" value="false"/>
<property name="hmilyDbConfig"> <property name="hmilyDbConfig">
<bean class="org.dromara.hmily.common.config.HmilyDbConfig"> <bean class="org.dromara.hmily.common.config.HmilyDbConfig">
<property name="url" <property name="url"

View File

@ -39,6 +39,8 @@
<property name="scheduledDelay" value="120"/> <property name="scheduledDelay" value="120"/>
<property name="scheduledThreadMax" value="4"/> <property name="scheduledThreadMax" value="4"/>
<property name="repositorySupport" value="db"/> <property name="repositorySupport" value="db"/>
<property name="asyncThreads" value="200"/>
<property name="started" value="true"/>
<property name="hmilyDbConfig"> <property name="hmilyDbConfig">
<bean class="org.dromara.hmily.common.config.HmilyDbConfig"> <bean class="org.dromara.hmily.common.config.HmilyDbConfig">
<property name="url" <property name="url"